using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System;


public class SettingsNavigation : MonoBehaviour
{
    public GameObject TabContent_Parent;

    //4 total Main tabs in the Settings UI
    // public GameObject Gameplay_Content;
    public GameObject Controls_Content;
    public GameObject GraphicsAudio_Content;
    // public GameObject Controller_Content;
    public GameObject Language_Content;
    public GameObject PrivacySocials_Content;
    public Color ButtonSelected_Color = new Color(0.149f, 0.682f, 0.373f); // Color for "#26AE5F"
    public Color WhiteColor = new Color(1f, 1f, 1f); // Color for "#FFFFFF", or White

    //Controls Navigation
    public GameObject[] ControlsNavigation; //0. Controller Button, 1. Controller GameObject, 2. Mobile Button, 3. Mobile GameObject, 4. Save button, 5. Reset button

    // Assuming you have multiple buttons as child objects of this GameObject
    private Button[] settings_navigation_buttons;


    void PlayClickAudio(){
        // audioSource.Play();
    }


    void OnEnable(){
        HandleControlSettings();

        settings_navigation_buttons = GetComponentsInChildren<Button>();

        foreach (Button button in settings_navigation_buttons){
            button.onClick.AddListener(() => SettingsClickHandler(button));
        }

        ActivateUI(GraphicsAudio_Content, "GraphicsAudio");
    }


    //For Control Settings, Handle Mobile and Controller Settings, so that Controller settings are the first shown when Controls tab is selected
    #region
        void HandleControlSettings(){
            GameObject gameobject;
            for (int i = 0; i < ControlsNavigation.Length; i++) {
                gameobject = ControlsNavigation[i];

                //Buttons
                if (i == 0 || i == 2) {

                    // Create a local copy of i
                    int index = i;

                    //Adding Listeners
                    Button button = gameobject.GetComponent<Button>();
                    button.onClick.AddListener(() => HandleControlView(index));

                    continue; // Skip index 0 and 2
                }

                //Default to view
                if (i == 1){
                    gameobject.SetActive(true);

                }else{
                    gameobject.SetActive(false);
                }
            }
        }

        void HandleControlView(int i){
            // Debug.Log($"{i} I hate it");
            GameObject controllerGameobject = ControlsNavigation[1];
            GameObject mobileGameobject = ControlsNavigation[3];
            if (i == 0){
                mobileGameobject.SetActive(false);
                controllerGameobject.SetActive(true);

                (ControlsNavigation[4]).SetActive(false);
                (ControlsNavigation[5]).SetActive(false);
                (ControlsNavigation[6]).SetActive(false);
            }

            if (i == 2){
                controllerGameobject.SetActive(false);
                mobileGameobject.SetActive(true);

                (ControlsNavigation[4]).SetActive(true);
                (ControlsNavigation[5]).SetActive(true);
                (ControlsNavigation[6]).SetActive(true);

            }
        }
    #endregion


    // Method to handle button clicks
    void SettingsClickHandler(Button clickedButton){

        // Determine which button is clicked
        if (clickedButton != null){
            string buttonName = clickedButton.gameObject.name;

            //Perform actions based on the button clicked
            switch (buttonName){
                // case "GamePlay":
                //     ActivateUI(Gameplay_Content, buttonName);
                //     break;

                case "Controls":
                    StopSounds();
                    ActivateUI(Controls_Content, buttonName);
                    break;

                case "GraphicsAudio":
                    StopSounds();
                    ActivateUI(GraphicsAudio_Content, buttonName);
                    break;

                // case "Controller":
                //     ActivateUI(Controller_Content, buttonName);
                //     break;

                case "Language":
                    StopSounds();
                    ActivateUI(Language_Content, buttonName);
                    break;

                case "PrivacySocials":
                    StopSounds();
                    ActivateUI(PrivacySocials_Content, buttonName);
                    break;
            }
        }
    }


    // Function to stop all audio in the Graphics & Audio tab content from playing
    void StopSounds(){
        SliderHandler sld = gameObject.GetComponent<SliderHandler>();
        sld.StopAllAudios();
    }


    // Function to activate a UI based on user interaction
    void ActivateUI(GameObject ActivegameObject, string buttonName){
        PlayClickAudio();

        //Change text size
        TextMeshProUGUI Selectedbutton_txt = (gameObject.transform.Find(buttonName).gameObject).GetComponent<TextMeshProUGUI>();
        Selectedbutton_txt.color = ButtonSelected_Color;
        Selectedbutton_txt.fontSize = 180;

        foreach(Transform child in (gameObject.transform)){

            if ((child.gameObject.name != "Dividers") && (child.gameObject.name != buttonName)){

                // Find the child with the name "hello"
                TextMeshProUGUI child_txt = child.GetComponent<TextMeshProUGUI>();
                child_txt.color = WhiteColor;  //Set to white
                child_txt.fontSize = 160;  //Set to white

            }
        }


        foreach (Transform child in (TabContent_Parent.transform)){
            string currentGameObject = child.gameObject.name;
            child.gameObject.SetActive(false);
        }
        ActivegameObject.SetActive(true);
    }


    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
