/*
 *Copyright(C) 2023 by Chief All rights reserved.
 *Unity版本：2023.2.5f1c1 
 *作者:Chief  
 *创建日期: 2024-01-19 
 *模块说明：通用单例模块
 *版本: 1.0
*/

using Sirenix.OdinInspector;
using System;
using System.Collections.Generic;

namespace Aster
{
    /// <summary>
    /// 单例管理，本身也是个全局单例；
    /// 管理其他的非Mono限制单例（LimitSingleton）；
    /// </summary>
    public class LimitSingletonManager : LimitGlobalMonoSingleton<LimitSingletonManager>
    {

        [LabelText("当前使用的单例"), ReadOnly]
        [NonSerialized, ShowInInspector]
        public List<string> List_SingletonNames = new List<string>();

        private List<I_AsterObject> m_ListSingletons = new List<I_AsterObject>();

        public T FindLimitSigleton<T>() where T : LimitSingleton<T>, I_AsterObject, new()
        {
            int length = m_ListSingletons.Count;
            for (int i = 0; i < length; i++)
            {
                var singleton = m_ListSingletons[i];
                if (singleton is T)
                    return singleton as T;
            }
            return null;
        }

        public void AddLimitSingleton(I_AsterObject singleton)
        {
            m_ListSingletons.Add(singleton);
            List_SingletonNames.Add(singleton.GetType().ToString());
        }

        public void RemoveLimitSingleton(I_AsterObject singleton)
        {
            List_SingletonNames.Remove(singleton.GetType().ToString());
            m_ListSingletons.Remove(singleton);
        }

        private void Update()
        {
            int length = m_ListSingletons.Count;
            for (int i = 0; i < length; i++)
                m_ListSingletons[i]?.Update();
        }

        private void LateUpdate()
        {
            int length = m_ListSingletons.Count;
            for (int i = 0; i < length; i++)
                m_ListSingletons[i]?.LateUpdate();
        }

        protected override void Do_Release()
        {
            base.Do_Release();
            int length = m_ListSingletons.Count;
            for (int i = length - 1; i >= 0; i--)
                m_ListSingletons[i]?.Destroy();
            m_ListSingletons.Clear();
        }

    }
}
