using UnityEngine;
using UnityEngine.EventSystems;
using TMPro;
using UnityEngine.UI;

public class ControlScaler : MonoBehaviour{
    public static ControlScaler instance;
    public float leftControlScale = 1f, rightControlScale = 1f, punchControlScale = 1f, kickControlScale = 1f;

    public Slider buttonScaler; //Slider used to alter button size, scaling up or down
    public Button[] buttonsArray; //Buttons considered to alter size

    private Button selectedButton; //Reference to the current selected button
    private Color defaultButtonColor = new Color(1f, 1f, 1f); //Button default color
    public Color selectedButtonColor = new Color(0.9411765f, 0.6196079f, 0.03137255f); // Color when the button is selected

    private GameSettings gameSettings;  //Reference to GameSettings
    private bool isUpdatingSlider = false; //Flag for slider value change

    private const string GameSettingsKey = "GameSettings";


    void OnEnable(){
        foreach (Button button in buttonsArray)
        {
            SetButtonColor(button, defaultButtonColor);
        }
        selectedButton = null;


        if (PlayerPrefs.HasKey(GameSettingsKey))
        {
            gameSettings = SettingsHandler.LoadGameSettings();

            leftControlScale = gameSettings.leftControlScaleValue;
            rightControlScale = gameSettings.rightControlScaleValue;
            punchControlScale = gameSettings.punchControlScaleValue;
            kickControlScale = gameSettings.kickControlScaleValue;
        }
    }


    void Awake()
    {
        if (instance == null)
        {
            instance = this;
        }
        else
        {
            Destroy(gameObject);
        }
    }


    void Start()
    {
        // Initialize the default button color
        if (buttonsArray.Length > 0)
        {
            defaultButtonColor = buttonsArray[0].colors.normalColor;
        }

        // Add listeners to the buttons
        foreach (Button button in buttonsArray)
        {
            button.onClick.AddListener(() => ControlButtonClicked(button));
            // button.onClick.AddListener(ControlButtonClicked);
        }

        // Add a listener to the slider
        buttonScaler.onValueChanged.AddListener(OnSliderValueChanged);
    }

    void Update(){
        if (selectedButton != null)
        {
            // Debug.Log(selectedButton.name);
            SetButtonColor(selectedButton, selectedButtonColor); //Set color of current selected button
        }
    }


    // Function called when slider value is changed, which should change button size based on the slider value
    void OnSliderValueChanged(float value)
    {
        if (isUpdatingSlider) return;

        // For now, it just ensures the selected button color is maintained
        if (selectedButton != null)
        {
            SetButtonColor(selectedButton, selectedButtonColor);

            string clickedGO = (selectedButton.gameObject).name;
            (float originalWidth, float originalHeight) = SettingsHandler.OriginalScales(clickedGO);
            SetButtonSize(value, originalHeight, originalWidth, clickedGO);
        }
    }


    // Function to change current selected button's color
    private void SetButtonColor(Button button, Color color){
        ColorBlock colorBlock = button.colors;
        colorBlock.normalColor = color;
        button.colors = colorBlock;
    }


    // Function to set the size of the button based on the slider value, adjusting the button's size
    void SetButtonSize(float value, float originalHeight, float originalWidth, string clickedGO){
        if (selectedButton != null){
            // Calculate the new width and height based on the slider value
            float newWidth = originalWidth * value;
            float newHeight = originalHeight * value;

            RectTransform buttonRectTransform = selectedButton.GetComponent<RectTransform>();
            buttonRectTransform.sizeDelta = new Vector2(newWidth, newHeight);


            //leftControlScale = 1f, rightControlScale = 1f, punchControlScale = 1f, kickControlScale = 1f;
            switch(clickedGO){
                case "left_control":
                    leftControlScale = value;
                    Debug.Log($"Left {leftControlScale}");
                    break;

                case "right_control":
                    rightControlScale = value;
                    Debug.Log($"Right {rightControlScale}");
                    break;

                case "kick":
                    kickControlScale = value;
                    Debug.Log($"Kick {kickControlScale}");
                    break;

                case "punch":
                    punchControlScale = value;
                    Debug.Log($"Punch {punchControlScale}");
                    break;
            }
        }
    }


    // Function called when a button is clicked, the button is then to be scaled
    void ControlButtonClicked(Button button){
        gameSettings = SettingsHandler.LoadGameSettings();
        string clickedGO = (button.gameObject).name;

        Debug.Log($"{SettingsHandler.OriginalScales(clickedGO)}");

        (float originalWidth, float originalHeight) = SettingsHandler.OriginalScales(clickedGO);
        (float currentControlWidth, float currentControlHeight) = SettingsHandler.LoadedScale(clickedGO);
        SetScalerSlider(originalWidth, originalHeight, currentControlWidth, currentControlHeight);

        selectedButton = button;
        (buttonScaler.gameObject).SetActive(true);

        foreach (Button btn in buttonsArray){
            if (btn.name == button.name){
                continue;

            }else{
                ColorBlock colorBlock = btn.colors;
                colorBlock.normalColor = defaultButtonColor;
                btn.colors = colorBlock;
            }
        }
    }


    //Sets the Slider value to show value of the current button clicked, which is to be scaled
    void SetScalerSlider(float originalWidth, float originalHeight, float currentControlWidth, float currentControlHeight){
        // Calculate the normalized values
        float normalizedWidth = currentControlWidth / originalWidth;

        // Temporarily disable the slider's onValueChanged listener
        isUpdatingSlider = true;
        buttonScaler.value = normalizedWidth;
        isUpdatingSlider = false;
    }

}
