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


public class SliderHandler : MonoBehaviour
{
    //First element is the text (shows Percent), Second element is the slider to change the value, Third element is the Gameobject with Audio Source
    public GameObject[] GameplayVolume;
    public GameObject[] UIVolume;
    public GameObject[] MusicVolume;
    public GameObject[] SFXVolume;
    public GameObject[] ControllerSensitivity;

    private GameSettings gameSettings;

    //Array of Audios, First element is Audio for Gameplay, Second for UI, Third for Music, Fourth for SFX
    public static AudioSource[] audioSources;

    void OnEnable(){
        // Call the static LoadGameSettings function from SettingsHandler
        gameSettings = SettingsHandler.LoadGameSettings();

        audioSources = new AudioSource[4]; //4 total elements
        audioSources[0] = GameplayVolume[2].GetComponent<AudioSource>(); //Gameplay Volume
        audioSources[1] = UIVolume[2].GetComponent<AudioSource>(); //UI Volume
        audioSources[2] = MusicVolume[2].GetComponent<AudioSource>(); //Music Volume
        audioSources[3] = SFXVolume[2].GetComponent<AudioSource>(); //SFX Volume


        (GameplayVolume[1].GetComponent<Slider>()).value = gameSettings.GameplayVolume_value;
        (UIVolume[1].GetComponent<Slider>()).value = gameSettings.UIVolume_value;
        (MusicVolume[1].GetComponent<Slider>()).value = gameSettings.MusicVolume_value;
        (SFXVolume[1].GetComponent<Slider>()).value = gameSettings.SFXVolume_value;

        StopAllAudios();
    }


    // Start is called before the first frame update
    void Start()
    {
        AttachSliderListener(GameplayVolume);
        AttachSliderListener(UIVolume);
        AttachSliderListener(MusicVolume);
        AttachSliderListener(SFXVolume);
        // AttachSliderListener(ControllerSensitivity);
    }


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


    void AttachSliderListener(GameObject[] sliderArray){
        // Get the TextMeshProUGUI and Slider components
        TextMeshProUGUI textMeshPro = sliderArray[0].GetComponent<TextMeshProUGUI>();
        Slider slider = sliderArray[1].GetComponent<Slider>();

        // Add listener for the slider value change
        // if (sliderArray != ControllerSensitivity){
        //     AudioSource audio = sliderArray[2].GetComponent<AudioSource>();
        // }

        AudioSource audio = sliderArray[2].GetComponent<AudioSource>();
        slider.onValueChanged.AddListener(value => OnSliderValueChanged(value, textMeshPro, audio, (sliderArray[1]).name));
    }


    // Method to handle slider value change
    GameSettings settings;
    void OnSliderValueChanged(float value, TextMeshProUGUI textMeshPro, AudioSource audio, string sliderName){
        float normalizedValue = value * 100f;
        textMeshPro.text = $"{normalizedValue:F0}%";

        settings = SettingsHandler.LoadGameSettings();

        switch(sliderName){
            case "GameplayVolume_value":
                settings.GameplayVolume_value = value;
                settings.audioVolumes[0] = textMeshPro.text; //Update Audio value to Audio Settings
                break; 

            case "UIVolume_value":
                settings.UIVolume_value = value;
                settings.audioVolumes[1] = textMeshPro.text; //Update Audio value to Audio Settings
                break;

            case "MusicVolume_value":
                settings.MusicVolume_value = value;
                settings.audioVolumes[2] = textMeshPro.text; //Update Audio value to Audio Settings
                break;

            case "SFXVolume_value":
                settings.SFXVolume_value = value;
                settings.audioVolumes[3] = textMeshPro.text; //Update Audio value to Audio Settings
                break;
        }

        // settings.sliderName = value;
        SettingsHandler.SaveGameSettings(settings);

        StopAllAudios();

        // Set the audio volume based on the slider value
        audio.volume = value;
        audio.Play();

        // Start the coroutine to play the audio for 1 second
        // StartCoroutine(PlayAudioForOneSecond(audio));

        // Play the audio if it's not already playing
        // if (!audio.isPlaying)
        // {
        //     audio.Play();
        // }
    }


    // Function to Stop all audio in the UI
    public void StopAllAudios(){
        foreach (AudioSource aud in audioSources){
            aud.time = 0f;
            aud.Stop();
        }
    }   


    // Coroutine to play the audio for 1 second
    IEnumerator PlayAudioForOneSecond(AudioSource audio)
    {

        // Play the audio
        audio.Play();

        // Wait for 1 second
        yield return new WaitForSeconds(3f);

        // Stop the audio
        // audio.Stop();
        StopAllAudios();
    }
}
