package com.example.generator;

import android.content.Intent;
import android.content.res.ColorStateList;
import android.os.Bundle;
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 androidx.fragment.app.Fragment;

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

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


        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragmentContainer, new Tab1())
                    .commit();
        }

        TabLayout tabLayout = findViewById(R.id.tabLayout);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                // Pass tab position dynamically
                Fragment targetFragment = Tab1.newInstance(tab.getPosition());

                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.fragmentContainer, targetFragment)
                        .commit();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {}

            @Override
            public void onTabReselected(TabLayout.Tab tab) {}
        });

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