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

public class UIManager : Singleton<UIManager>
{
    Dictionary<Type, UIBaseScreen> screens = new Dictionary<Type, UIBaseScreen>();

    UIBaseScreen currentScreen;

    protected override void OnAwake()
    {
        DontDestroyOnLoad(gameObject);
        SetScreens();

    }

    void SetScreens()
    {
        foreach (UIBaseScreen screen in GetComponentsInChildren<UIBaseScreen>())
        {
            screens.Add(screen.GetType(), screen);
            screen.gameObject.SetActive(false);
        }
    }

    public void Show<T>() where T : UIBaseScreen
    {
        if (currentScreen != null)
        {
            currentScreen.Close();
        }

        if (!screens.ContainsKey(typeof(T)))
        {
            return;
        }

        currentScreen = screens[typeof(T)];
        currentScreen.Open();

    }

    void Start()
    {
        int index = SceneManager.GetActiveScene().buildIndex;
        switch (index)
        {
            case 0:
                Show<UIMainMenu>();
                break;

            case 1:
                Show<UIGame>();
                break;

            default:
                break;
        }


        
    }
}
