package com.example.generator;

import android.content.Intent;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.google.android.material.button.MaterialButton;


public class MakeAutoActivity extends AppCompatActivity {

    private EditText ScoreObjects;
    private EditText Ob1;
    private EditText Ob2;
    private EditText Ob3;
    private EditText Ob4;
    private EditText Ob5;
    private EditText Ob6;
    private EditText Ob7;
    private EditText Ob8;
    private EditText Ob9;

    private String ColorP = "";
    private String ColorS = "";
    private int mainColorResP = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_make_auto);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        EditText ScoreObjects = (EditText) findViewById(R.id.NumScorObject);
        EditText Ob1 = (EditText) findViewById(R.id.NumOfButton1);
        EditText Ob2 = (EditText) findViewById(R.id.scoreOb2);
        EditText Ob3 = (EditText) findViewById(R.id.scoreOb3);
        EditText Ob4 = (EditText) findViewById(R.id.scoreOb4);
        EditText Ob5 = (EditText) findViewById(R.id.scoreOb5);
        EditText Ob6 = (EditText) findViewById(R.id.scoreOb6);
        EditText Ob7 = (EditText) findViewById(R.id.scoreOb7);
        EditText Ob8 = (EditText) findViewById(R.id.scoreOb8);
        EditText Ob9 = (EditText) findViewById(R.id.scoreOb9);
        Ob1.setVisibility(View.GONE);
        Ob2.setVisibility(View.GONE);
        Ob3.setVisibility(View.GONE);
        Ob4.setVisibility(View.GONE);
        Ob5.setVisibility(View.GONE);
        Ob6.setVisibility(View.GONE);
        Ob7.setVisibility(View.GONE);
        Ob8.setVisibility(View.GONE);
        Ob9.setVisibility(View.GONE);



        MaterialButton backButton = findViewById(R.id.backButton);
        MaterialButton nextButton = findViewById(R.id.nextButton);
        backButton.setOnClickListener((v) -> {
            Intent intent = new Intent(this, StartingPreview.class);
            startActivity(intent);
        });
        if (EventsActivity.Data.getColorS() != null) {
            ColorS = EventsActivity.Data.getColorS();
        }
        if (EventsActivity.Data.getColorP() != null) {
            ColorP = EventsActivity.Data.getColorP();
        }

        switch (ColorP) {
            case "Green": mainColorResP = R.color.greenMain; break;
            case "Blue": mainColorResP = R.color.blueMain; break;
            case "Pink": mainColorResP = R.color.pinkMain; break;
            case "Purple": mainColorResP = R.color.purpleMain; break;
            case "Red": mainColorResP = R.color.redMain; break;
            case "Yellow": mainColorResP = R.color.yellowMain; break;
            default: mainColorResP = R.color.orangeMain; break;
        }


        if (mainColorResP != 0) {
            int actualColor = ContextCompat.getColor(this, mainColorResP);
            ColorStateList tintList = ColorStateList.valueOf(actualColor);

            ViewCompat.setBackgroundTintList(nextButton, tintList);
            ViewCompat.setBackgroundTintList(backButton, tintList);

            nextButton.setStrokeWidth(0);
            backButton.setStrokeWidth(0);
        }

        backButton.setOnClickListener((v) -> {
            Intent intent = new Intent(this, StartingPreview.class);
            startActivity(intent);
        });

        nextButton.setOnClickListener((v) -> {
            EventsActivity.Data.setScoreOb1(Ob1.getText().toString());
            EventsActivity.Data.setScoreOb2(Ob2.getText().toString());
            EventsActivity.Data.setScoreOb3(Ob3.getText().toString());
            EventsActivity.Data.setScoreOb4(Ob4.getText().toString());
            EventsActivity.Data.setScoreOb5(Ob5.getText().toString());
            EventsActivity.Data.setScoreOb6(Ob6.getText().toString());
            EventsActivity.Data.setScoreOb7(Ob7.getText().toString());
            EventsActivity.Data.setScoreOb8(Ob8.getText().toString());
            EventsActivity.Data.setScoreOb9(Ob9.getText().toString());
            EventsActivity.Data.setNumScoreOb(ScoreObjects.getText().toString());
            Intent intent = new Intent(this, MakeAutoActivity2.class);
            startActivity(intent);
        });
        ScoreObjects.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                boolean handled = false;
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    String ScoreNum = ScoreObjects.getText().toString();
                    View[] objectives = {
                            Ob1, Ob2, Ob3, Ob4, Ob5,
                            Ob6, Ob7, Ob8, Ob9
                    };

                    int score = ScoreNum.isEmpty() ? 0 : Integer.parseInt(ScoreNum);

                    for (int i = 0; i < objectives.length; i++) {
                        objectives[i].setVisibility(i < score ? View.VISIBLE : View.GONE);
                    }
                        handled = true;
                }
                return handled;
            }
        });
    }
}