using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using TMPro;
using System;
using System.Reflection;
using Unity.VisualScripting;
using UnityEngine.InputSystem;

public class CharacterSelection : MonoBehaviour
{
    private int currentSelection = 0;

    public Button primaryButton;
    public Button selectCharacterButton; // New button for character selection
    public Sprite[] buttonImages;
    public Sprite[] characterImages;
    public string[] characterNames;
    public string[] characterDescriptions;
    public Sprite[] selectButtons;
    public Sprite[] characterCards;
    public Sprite[] characterVsCardsLeft;
    public Sprite[] characterVsCardsRight;
    public Image backgroundImage;
    public Image characterImage;
    public TextMeshProUGUI characterName;
    public TextMeshProUGUI characterDescription;
    public Button[] buttons;
    private int currentIndex = 0;

    public GameObject modeScreen; // Reference to the ModeScreen object to activate
    public ModeSelection modeSelection;
    public HomeSrceen homeScreen;
    public Button backButton;

    // Start is called before the first frame update
    void Start()
    {
        primaryButton.Select();
        //buttons = selectButtons;
        SetBackgroundImage(currentIndex);
        EventSystem.current.SetSelectedGameObject(buttons[0].gameObject);
        
        //backButton.onClick.AddListener(ModeBackButton);
    }

    // OPTIMIZATION: Cache last selected index to avoid redundant updates
    private int lastSelectedIndex = -1;
    private GameObject lastSelectedObject = null;

    // Update is called once per frame
    void Update()
    {
        // Cache the current selected object
        GameObject currentSelected = EventSystem.current?.currentSelectedGameObject;
        
        // Early exit if nothing changed
        if (currentSelected == lastSelectedObject) return;
        lastSelectedObject = currentSelected;
        
        if (currentSelected == null) return;
        
        // Only GetComponent if the selected object changed
        Button selectedButton = currentSelected.GetComponent<Button>();
        if (selectedButton == null) return;
        
        int selectedIndex = System.Array.IndexOf(buttons, selectedButton);
        
        // Only update UI if index actually changed
        if (selectedIndex != lastSelectedIndex && selectedIndex >= 0)
        {
            lastSelectedIndex = selectedIndex;
            currentIndex = selectedIndex;
            try
            {
                SetButtonImage(selectedButton, selectedIndex);
                SetBackgroundImage(selectedIndex);
            }
            catch { }
        }
    }

    private void SetButtonImage(Button button, int index)
    {
        if (button != null && buttonImages.Length > index)
        {
            button.GetComponent<Image>().sprite = buttonImages[index];
        }
    }

    private void SetBackgroundImage(int index)
    {
        if (backgroundImage != null && buttonImages.Length > index)
        {
            backgroundImage.sprite = buttonImages[index];
            characterImage.sprite = characterImages[index];
            characterName.text = characterNames[index];
            characterDescription.text = characterDescriptions[index];
           
            if (characterNames[index] == "Windham" || characterNames[index] == "Amira" || characterNames[index] == "Ziggy" || characterNames[index] == "Bahman" || characterNames[index] == "Amon" || characterNames[index] == "Imani")
            {
                selectCharacterButton.GetComponent<Button>().enabled = true;
                selectCharacterButton.GetComponent<Image>().sprite = selectButtons[index];
                PlayerPrefs.SetString("selectedcharactername", characterNames[index]);
            }
            else
            {
                selectCharacterButton.GetComponent <Button>().enabled = false;
                selectCharacterButton.GetComponent<Image>().sprite = selectCharacterButton.transform.GetChild(1).GetComponent<Image>().sprite;
                PlayerPrefs.SetString("selectedcharactername", characterNames[index]);
            }
        }
    }

    private void OnEnable()
    {
        primaryButton.Select();
    }
}
