using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameSceneManager : Singleton<GameSceneManager>
{
    #region Fields
    private WaveManager _waveManager = null;
    private UIManager _uiManager = null;
    private BuildingManager _buildingManager = null;
    #endregion Fields

    #region Properties
    public UIManager UiManager { get => _uiManager; }
    public WaveManager WaveManager { get => _waveManager; }
    public BuildingManager BuildingManager { get => _buildingManager; }
    #endregion Properties

    #region Methods
    public override void Awake()
    {
        base.Awake();
        OnSceneLoaded(SceneManager.GetActiveScene(), LoadSceneMode.Single);
        SceneManager.sceneLoaded += OnSceneLoaded;
        SceneManager.sceneUnloaded += OnSceneUnLoaded;
    }

    private void OnDestroy()
    {
        SceneManager.sceneLoaded -= OnSceneLoaded;
        SceneManager.sceneUnloaded -= OnSceneUnLoaded;
    }

    private void OnSceneLoaded(Scene scene, LoadSceneMode loadSceneMode)
    {
        UnityEngine.Debug.Log("OnSceneLoaded");
        _uiManager = FindObjectOfType<UIManager>();
        _waveManager = FindObjectOfType<WaveManager>();
        _buildingManager = FindObjectOfType<BuildingManager>();
    }

    private void OnSceneUnLoaded(Scene scene)
    {
        _uiManager = null;
        _waveManager = null;
        _buildingManager = null;
    }

    public void RestartGame()
    {
        // Reload current scene.
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }
    #endregion Methods
}
