using System;
using Animation;
using UnityEngine;

interface IManager
{
    void Init() { }
    void OnUpdate() { }
    void Clear() { }
}

public class Manager : MonoBehaviour, IManager
{
    static Manager s_instance;

    static Manager Instance
    {
        get
        {
            if (s_instance == null)
            {
                GameObject obj = GameObject.Find("@Manager");
                if (obj == null)
                {
                    obj = new GameObject { name = "@Manager" };
                    obj.AddComponent<Manager>();
                }
                s_instance = obj.GetComponent<Manager>();
                DontDestroyOnLoad(obj);
                s_instance.Init();
            }
            return s_instance;
        }
    }

    private DataManager _data;
    private InputManager _input;
    private PoolManager _pool;
    private ResourceManager _resource;
    private SystemManager _system;
    private UIManager _ui;
    
    public static DataManager Data { get { return Instance._data; } }
    public static InputManager Input { get { return Instance._input; } }
    public static PoolManager Pool { get { return Instance._pool; } }
    public static ResourceManager Resource { get { return Instance._resource; } }

    public static SystemManager System { get { return Instance._system; } }

    public static UIManager UI { get { return Instance._ui; } }


    private void Init()
    {
        s_instance._input = new InputManager();
        s_instance._data = new DataManager();
        s_instance._pool = new PoolManager();
        s_instance._resource = new ResourceManager();
        s_instance._system = new SystemManager();
        s_instance._ui = new UIManager();
                
        s_instance._input.Init();
        s_instance._pool.Init();
        s_instance._system.Init();
        s_instance._ui.Init();
    }


    void Update()
    {
        _input.OnUpdate();
    }

    private void OnDestroy()
    {
        Clear();
    }

    void OnApplicationQuit()
    {
        Clear();
    }

    // 클리어는 모든 것을 초기화 하는 역할로 자주 사용 되기에 그냥 전역적으로 사용 가능하게 함
    public static void Clear()
    {
        Input.Clear();
        Resource.Clear();
        Pool.Clear();
        Data.Clear();
    }
}
