using UnityEngine;
using UnityEngine.UI;

public class AudioSourceController : MonoBehaviour
{
    public AudioSource[] audioSources;
    public Scrollbar VolScroll;
    public GameObject OnButton;
    public GameObject OffButton;

    private float lastSavedVolume;
    private bool lastSavedMusicState;

    private void Awake()
    {
        audioSourceGetter();
        Invoke(nameof(audioSourceGetter), 2); // Ensures delayed fetching in second scene

        // Ensure default music is ON if running for the first time
        if (!PlayerPrefs.HasKey("MusicBool"))
        {
            PlayerPrefs.SetInt("MusicBool", 1); // Default to ON
            PlayerPrefs.Save();
        }

        // Load saved settings
        lastSavedMusicState = PlayerPrefs.GetInt("MusicBool") == 1;
        lastSavedVolume = PlayerPrefs.GetFloat("Volume", 1);

        // Apply the saved state
        Set_Value(lastSavedVolume);
        ApplyAudioState(lastSavedMusicState);
    }

    public void audioSourceGetter()
    {
        audioSources = FindObjectsOfType<AudioSource>();
        Set_Value(PlayerPrefs.GetFloat("Volume", 1));
    }

    public void PlayerPrefSaver()
    {
        if (VolScroll != null)
        {
            PlayerPrefs.SetFloat("Volume", VolScroll.value);
        }

        PlayerPrefs.SetInt("MusicBool", OffButton.activeSelf ? 1 : 0);
        PlayerPrefs.Save(); // Ensure immediate save

        // Update last saved state
        lastSavedVolume = VolScroll.value;
        lastSavedMusicState = OffButton.activeSelf;
    }

    public void Delayed_Starter()
    {
        Invoke(nameof(Awake), 0.5f);
    }

    public void Set_Value(float value)
    {
        if (audioSources == null || audioSources.Length == 0)
            audioSourceGetter(); // Ensures sources are updated if needed

        foreach (var source in audioSources)
        {
            source.volume = value;
        }

        if (VolScroll != null && VolScroll.value != value)
        {
            VolScroll.value = value;
        }
    }

    // **New Functions for On/Off Button Assignment in Inspector**
    public void ToggleAudioOn()
    {
        ApplyAudioState(true);
    }

    public void ToggleAudioOff()
    {
        ApplyAudioState(false);
    }

    // **Handles audio state & button switching**
    private void ApplyAudioState(bool isOn)
    {
        if (isOn)
        {
            OnButton.SetActive(false);
            OffButton.SetActive(true);
            PlayAudio();
        }
        else
        {
            OffButton.SetActive(false);
            OnButton.SetActive(true);
            StopAudio();
        }
    }

    private void PlayAudio()
    {
        if (audioSources == null || audioSources.Length == 0)
            audioSourceGetter();

        foreach (var source in audioSources)
        {
            source.Play();
        }
    }

    private void StopAudio()
    {
        if (audioSources == null || audioSources.Length == 0)
            audioSourceGetter();

        foreach (var source in audioSources)
        {
            source.Stop();
        }
    }

    // **Cancel Button - Resets to last saved settings**
    public void CancelSettings()
    {
        Set_Value(lastSavedVolume);
        ApplyAudioState(lastSavedMusicState);
    }
}