package com.example.generator;

import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

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 StartingPreview 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_starting_preview);
        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;
        });

        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;
        }


        ImageView imageView2 = findViewById(R.id.imageView2);
        String imageBase64 = EventsActivity.Data.getBase64();

        if (imageBase64 != null && !imageBase64.isEmpty()) {
            try {
                byte[] decodedString = Base64.decode(imageBase64, Base64.DEFAULT);
                Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
                imageView2.setImageBitmap(decodedByte);
            } catch (Exception e) {
                e.printStackTrace();
                Log.e("PreviewError", "Failed to decode base64 image asset");
            }
        }


        RadioGroup radioGroup = findViewById(R.id.radioGroup);
        int totalPositions = EventsActivity.Data.getNumPos();
        int whiteColor = ContextCompat.getColor(this, R.color.white);
        int activeMainColor = ContextCompat.getColor(this, mainColorResP);
        ColorStateList buttonTintList = ColorStateList.valueOf(activeMainColor);

        for (int i = 1; i <= totalPositions; i++) {
            RadioButton radioButton = new RadioButton(this);
            radioButton.setId(View.generateViewId());
            radioButton.setText("Starting Position " + i);
            radioButton.setTextSize(20);
            radioButton.setTextColor(whiteColor);
            radioButton.setButtonTintList(buttonTintList);
            radioGroup.addView(radioButton);
        }

        MaterialButton backButton = findViewById(R.id.backButton);
        MaterialButton nextButton = findViewById(R.id.buttonNext);
        MaterialButton backButtonFake = findViewById(R.id.BackButtonFake);
        MaterialButton nextButtonFake = findViewById(R.id.ButtonNextFake);

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

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

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

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

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