package com.example.generator;

import android.content.Intent;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.Spinner;

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_color);
        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;
        });

        Spinner spinnerP = (Spinner) findViewById(R.id.ColorSelector);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.colorOptions, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerP.setAdapter(adapter);

        Spinner spinnerS = (Spinner) findViewById(R.id.ColorSelector2nd);
        ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.colorOptions2nd, android.R.layout.simple_spinner_item);
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerS.setAdapter(adapter2);

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

        MaterialButton TeleExample1 = (MaterialButton) findViewById(R.id.button_tele_example);
        MaterialButton TeleExample2 = (MaterialButton) findViewById(R.id.button_tele_example2);
        MaterialButton AutoExample1 = (MaterialButton) findViewById(R.id.button_auto_example);
        MaterialButton AutoExample2 = (MaterialButton) findViewById(R.id.button_auto_example2);
        MaterialButton NavExample1 = (MaterialButton) findViewById(R.id.button_nav_example);
        MaterialButton NavExample2 = (MaterialButton) findViewById(R.id.button_nav_example2);
        RadioButton radioButton = (RadioButton) findViewById(R.id.radioButton);
        CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);

        spinnerP.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, android.view.View selectedItemView, int position, long id) {
                String selectedItem = parentView.getItemAtPosition(position).toString();

                int mainColorRes = 0;
                int bgTintRes = 0;
                String Color = "";

                switch (selectedItem) {
                    case "Primary Orange":
                        Color = "Orange";
                        mainColorRes = R.color.orangeMain;
                        bgTintRes = R.color.orangeMain_bg;
                        break;
                    case "Primary Green":
                        Color = "Green";
                        mainColorRes = R.color.greenMain;
                        bgTintRes = R.color.greenMain_bg;
                        break;
                    case "Primary Blue":
                        Color = "Blue";
                        mainColorRes = R.color.blueMain;
                        bgTintRes = R.color.blueMain_bg;
                        break;
                    case "Primary Pink":
                        Color = "Pink";
                        mainColorRes = R.color.pinkMain;
                        bgTintRes = R.color.pinkMain_bg;
                        break;
                    case "Primary Purple":
                        Color = "Purple";
                        mainColorRes = R.color.purpleMain;
                        bgTintRes = R.color.purpleMain_bg;
                        break;
                    case "Primary Red":
                        Color = "Red";
                        mainColorRes = R.color.redMain;
                        bgTintRes = R.color.redMain_bg;
                        break;
                    case "Primary Yellow":
                        Color = "Yellow";
                        mainColorRes = R.color.yellowMain;
                        bgTintRes = R.color.yellowMain_bg;
                        break;
                }

                if (mainColorRes != 0) {
                    ColorStateList mainColorStateList = ContextCompat.getColorStateList(ColorActivity.this, mainColorRes);
                    ColorStateList bgTintStateList = ContextCompat.getColorStateList(ColorActivity.this, bgTintRes);
                    int mainColorInt = ContextCompat.getColor(ColorActivity.this, mainColorRes);

                    EventsActivity.Data.setColorP(Color);

                    spinnerP.setBackgroundColor(mainColorInt);
                    radioButton.setButtonTintList(mainColorStateList);
                    checkBox.setButtonTintList(mainColorStateList);

                    TeleExample1.setStrokeColor(mainColorStateList);
                    TeleExample1.setBackgroundTintList(bgTintStateList);

                    AutoExample2.setStrokeColor(mainColorStateList);
                    AutoExample2.setBackgroundTintList(bgTintStateList);
                    NavExample2.setBackgroundColor(mainColorInt);
                    NavExample1.setBackgroundColor(mainColorInt);

                    backButton.setBackgroundColor(mainColorInt);
                    nextButton.setBackgroundColor(mainColorInt);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {}
        });

        spinnerS.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, android.view.View selectedItemView, int position, long id) {
                String selectedItem = parentView.getItemAtPosition(position).toString();

                int mainColorRes = 0;
                int bgTintRes = 0;
                String Color = "";

                switch (selectedItem) {
                    case "Secondary Orange":
                        Color = "Orange";
                        mainColorRes = R.color.orangeMain;
                        bgTintRes = R.color.orangeMain_bg;
                        break;
                    case "Secondary Green":
                        Color = "Green";
                        mainColorRes = R.color.greenMain;
                        bgTintRes = R.color.greenMain_bg;
                        break;
                    case "Secondary Blue":
                        Color = "Blue";
                        mainColorRes = R.color.blueMain;
                        bgTintRes = R.color.blueMain_bg;
                        break;
                    case "Secondary Pink":
                        Color = "Pink";
                        mainColorRes = R.color.pinkMain;
                        bgTintRes = R.color.pinkMain_bg;
                        break;
                    case "Secondary Purple":
                        Color = "Purple";
                        mainColorRes = R.color.purpleMain;
                        bgTintRes = R.color.purpleMain_bg;
                        break;
                    case "Secondary Red":
                        Color = "Red";
                        mainColorRes = R.color.redMain;
                        bgTintRes = R.color.redMain_bg;
                        break;
                    case "Secondary Yellow":
                        Color = "Yellow";
                        mainColorRes = R.color.yellowMain;
                        bgTintRes = R.color.yellowMain_bg;
                        break;
                }

                if (mainColorRes != 0) {
                    ColorStateList mainColorStateList = ContextCompat.getColorStateList(ColorActivity.this, mainColorRes);
                    ColorStateList bgTintStateList = ContextCompat.getColorStateList(ColorActivity.this, bgTintRes);
                    int mainColorInt = ContextCompat.getColor(ColorActivity.this, mainColorRes);

                    EventsActivity.Data.setColorS(Color);

                    spinnerS.setBackgroundColor(mainColorInt);

                    TeleExample2.setStrokeColor(mainColorStateList);
                    TeleExample2.setBackgroundTintList(bgTintStateList);

                    AutoExample1.setStrokeColor(mainColorStateList);
                    AutoExample1.setBackgroundTintList(bgTintStateList);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {}
        });
    }
}
