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

public class ToggleHandler : MonoBehaviour
{
    //Each Toggle Array takes in values associated with each other, such as ON and OFF
    //Audio & Graphics Settings
    public GameObject[] GraphicsToggles;
    public GameObject[] FrameRateToggles;
    public GameObject[] ScreenVibrationToggles;

    //Controller Settings
    public GameObject[] ControllerSupportToggles;

    //Language Settings
    public GameObject[] LanguageToggles;

    // Start is called before the first frame update
    void Start()
    {
        AttachToggleListener(GraphicsToggles); //Attach listener to GraphicsToggles
        AttachToggleListener(FrameRateToggles); //Attach listener to FrameRateToggles
        AttachToggleListener(ScreenVibrationToggles); //Attach listener to ScreenVibrationToggles
        AttachToggleListener(ControllerSupportToggles); //Attach listener to ControllerSupportToggles
        AttachToggleListener(LanguageToggles); //Attach listener to LanguageToggles
        
    }

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

    // Function called to keep track of toggle interaction
    void AttachToggleListener(GameObject[] toggleArray){
        // Loop through each toggle to add a listener to its Toggle component
        foreach (GameObject toggleObj in toggleArray){
            Toggle toggle = toggleObj.GetComponent<Toggle>();
            if (toggle != null)
            {
                toggle.onValueChanged.AddListener((isSelected) => OnToggleValueChanged(toggleArray, toggle));
            }
            else
            {
                Debug.LogError("GameObject does not have a Toggle component!");
            }
        }
    }


    // Called when any toggle's value changes
    void OnToggleValueChanged(GameObject[] toggleArray, Toggle selectedToggle)
    {
        if (selectedToggle.isOn)
        {
            // Deselect all other toggles
            foreach (GameObject toggleObj in toggleArray)
            {
                Toggle toggle = toggleObj.GetComponent<Toggle>();
                if (toggle != null && toggle != selectedToggle)
                {
                    toggle.isOn = false;
                }
            }
        }
    }
}
