using UnityEngine;
using UnityEngine.SceneManagement;
using TMPro;
using UnityEngine.UI;

public class MainMenuController : MonoBehaviour
{
    public enum MenuMode {
		MainMenu,
		Options,
		Help,
        Leaderboard
	}

    [SerializeField]
	TMP_Text titleLabel;

    [SerializeField]
	Transform startMenuParent;

	[SerializeField]
	Transform optionsParent;
	[SerializeField]
	Transform helpParent;

    MenuMode? lastMode;
    MenuMode mode;

    [Header("Sliders")]
    [SerializeField] private Slider masterVolumeSlider;
    [SerializeField] private Slider musicVolumeSlider;
    [SerializeField] private Slider sfxVolumeSlider;

    [Header("Audio Sources")]
    [SerializeField] private AudioSource menuHover;
    [SerializeField] private AudioSource menuClick;
    
    private void Start() {
        SetMenuMode(MenuMode.MainMenu);
        SetMenuSliders();
    }
    public void SetMenuMode(MenuMode mode) {
		//meh
		titleLabel.gameObject.SetActive(mode == MenuMode.MainMenu);
		startMenuParent.gameObject.SetActive(mode == MenuMode.MainMenu);
		optionsParent.gameObject.SetActive(mode == MenuMode.Options);
		helpParent.gameObject.SetActive(mode == MenuMode.Help);
		if(this.mode == MenuMode.MainMenu)
			lastMode = this.mode;

		this.mode = mode;
		// ResetMenuState();
	}

    public void OnPlay(){
        SceneManager.LoadScene("Game");
        
    }

    public void OnOptions(){
        SetMenuMode(MenuMode.Options);
    }

    public void OnHelp(){
        SetMenuMode(MenuMode.Help);
    }

    public void OnLeaderboard(){
        SetMenuMode(MenuMode.Leaderboard);
    }

    public void OnBack(){
        if (lastMode != null)
			SetMenuMode(lastMode.Value);
    }

    public void OnQuit(){
        Application.Quit();
    }

    private void SetMenuSliders()
    {
	    masterVolumeSlider.value = SoundManager.Instance.masterVolume;
	    musicVolumeSlider.value = SoundManager.Instance.musicVolume;
	    sfxVolumeSlider.value = SoundManager.Instance.sfxVolume;
    }

    public void OnMasterVolumeChanged()
    {
	    SoundManager.Instance.OnMasterVolumeChanged(masterVolumeSlider.value);
    }
    
    public void OnMusicVolumeChanged()
    {
	    SoundManager.Instance.OnMusicVolumeChanged(musicVolumeSlider.value);
    }
    
    public void OnSfxVolumeChanged()
    {
	    SoundManager.Instance.OnSfxVolumeChanged(sfxVolumeSlider.value);
    }

    public void MenuHover()
    {
	    SoundManager.Instance.PlaySfx(menuHover);
    }

    public void MenuClick()
    {
	    SoundManager.Instance.PlaySfx(menuClick);
    }
}
