package com.example.generator;

import android.content.Intent;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

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 MakeAutoActivity2 extends AppCompatActivity {

    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_auto2);
        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 Ob1 = (EditText) findViewById(R.id.NumOfButton1);
        EditText Ob2 = (EditText) findViewById(R.id.NumOfButton2);
        EditText Ob3 = (EditText) findViewById(R.id.NumOfButton3);
        EditText Ob4 = (EditText) findViewById(R.id.NumOfButton4);
        EditText Ob5 = (EditText) findViewById(R.id.NumOfButton5);
        EditText Ob6 = (EditText) findViewById(R.id.NumOfButton6);
        EditText Ob7 = (EditText) findViewById(R.id.NumOfButton7);
        EditText Ob8 = (EditText) findViewById(R.id.NumOfButton8);
        EditText Ob9 = (EditText) findViewById(R.id.NumOfButton9);

        String ScoreNum = EventsActivity.Data.getNumScoreOb();
        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);
        }

        Ob1.setHint(EventsActivity.Data.getScoreOb1());
        Ob2.setHint(EventsActivity.Data.getScoreOb2());
        Ob3.setHint(EventsActivity.Data.getScoreOb3());
        Ob4.setHint(EventsActivity.Data.getScoreOb4());
        Ob5.setHint(EventsActivity.Data.getScoreOb5());
        Ob6.setHint(EventsActivity.Data.getScoreOb6());
        Ob7.setHint(EventsActivity.Data.getScoreOb7());
        Ob8.setHint(EventsActivity.Data.getScoreOb8());
        Ob9.setHint(EventsActivity.Data.getScoreOb9());

        if (EventsActivity.Data.getColorS() != null) {
            ColorS = EventsActivity.Data.getColorS();
        }
        if (EventsActivity.Data.getColorP() != null) {
            ColorP = EventsActivity.Data.getColorP();
        }

        MaterialButton backButton = findViewById(R.id.backButton);
        MaterialButton nextButton = findViewById(R.id.nextButton);
        backButton.setOnClickListener((v) -> {
            Intent intent = new Intent(this, MakeAutoActivity.class);
            startActivity(intent);
        });
        nextButton.setOnClickListener((v) -> {
            Intent intent = new Intent(this, MakeAutoActivity.class);
            startActivity(intent);
        });


        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);
        }
        if (EventsActivity.Data.getColorS() != null) {
            ColorS = EventsActivity.Data.getColorS();
        }
        if (EventsActivity.Data.getColorP() != null) {
            ColorP = EventsActivity.Data.getColorP();
        }
    }
}