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

public class UIManager : MonoBehaviour
{
    #region Fields
    [SerializeField] private GamePanel _gamePanel = null;
    [SerializeField] private GameObject _startPanel = null;
    [SerializeField] private GameObject _endPanel = null;
    [SerializeField] private TextMeshProUGUI _endScore = null;
    [SerializeField, TextArea] private string _endDisplayScore = "Wave Reached : {0} \nEnemy Killed : {1}";
    #endregion Fields

    #region Properties
    public GamePanel GamePanel { get { return _gamePanel; } }
	
	[Header("UI Sounds")]
	public AudioSource menuMusicAudio;
	public AudioSource gameMusicAudio;
	public AudioSource buttonClickAudio;
    #endregion Properties
	
    #region Methods
    private void Start()
    {
        _startPanel.SetActive(true);
        _gamePanel.gameObject.SetActive(false);
        _endPanel.SetActive(false);
        GameManager.Instance.GameStateChanged += OnGameSceneChanged;
		//menuMusicAudio.Play();
    }

    private void OnDestroy()
    {
        GameManager.Instance.GameStateChanged -= OnGameSceneChanged;
    }

    public void OnGameSceneChanged(GameState gameState)
    {
        if (gameState == GameState.StartMenu)
        {
            _startPanel.SetActive(true);
            _gamePanel.gameObject.SetActive(false);
            _endPanel.SetActive(false);
        }
        else if (gameState == GameState.EndMenu)
        {
            _startPanel.SetActive(false);
            _gamePanel.gameObject.SetActive(false);
            _endPanel.SetActive(true);
            _endScore.text = string.Format(_endDisplayScore, GameSceneManager.Instance.WaveManager.WaveCount, GameSceneManager.Instance.WaveManager.MonsterSpawner.MonsterKilled);
        }
        else
        {
            _startPanel.SetActive(false);
            _gamePanel.gameObject.SetActive(true);
            _endPanel.SetActive(false);
        }
    }

    public void RestartButtonClicked()
    {
        StartCoroutine(RestartAfterDelay());
    }

    IEnumerator RestartAfterDelay()
    {
        yield return new WaitForSeconds(0.5f);
        GameSceneManager.Instance.RestartGame();
    }

    public void StarButtonClicked()
    {
		//menuMusicAudio.Stop();
		gameMusicAudio.Play();
        StartCoroutine(LaunchAfterDelay());
    }

    IEnumerator LaunchAfterDelay()
    {
        yield return new WaitForSeconds(0.5f);
        GameManager.Instance.LaunchGame();
    }

    public void PlayButtonSound()
    {
        buttonClickAudio.Play();
    }
    #endregion Methods
}
