using UnityEngine;

namespace BingoBrain.Core
{



    public abstract class SingletonUnity<T> : MonoBehaviour, ISingleton where T : SingletonUnity<T>
    {
        private static T mInstance;

        public static T Instance
        {
            get
            {
                if (mInstance != null)
                {
                    return mInstance;
                }

                if (ApplicationIsQuitting)
                {
                    return null;
                }

                GameObject instanceGO = new GameObject(typeof(T).Name);
                mInstance = instanceGO.AddComponent<T>();
                SetSelfParentRoot(instanceGO, mInstance.ParentRootName);
                mInstance.OnSingletonInit();
                return mInstance;
            }
        }

        private static bool ApplicationIsQuitting;
        protected virtual string ParentRootName { get; private set; }

        private static void SetSelfParentRoot(GameObject go, string parentRootName)
        {
            if (!OCConst.EngineSingletonGo) return;

            if (parentRootName == null)
            {
                go.transform.SetParent(OCConst.EngineSingletonGo.transform, false);
            }
            else
            {
                Transform sinleonRoot = OCConst.EngineSingletonGo.transform;
                Transform rootTF = sinleonRoot.Find(parentRootName);
                if (rootTF == null)
                {
                    GameObject rootGo = new GameObject(parentRootName);
                    switch (parentRootName)
                    {
                        case OCConst.MonoManagerGoName:
                            OCConst.MonoManagerGo = rootGo;
                            break;
                        case OCConst.DispatcherGoName:
                            OCConst.DispatcherGo = rootGo;
                            break;
                        case OCConst.LoaderGoName:
                            OCConst.LoaderGo = rootGo;
                            break;
                    }

                    rootTF = rootGo.transform;
                    rootTF.transform.SetParent(sinleonRoot, false);
                }

                go.transform.SetParent(rootTF.transform, false);
            }
        }

        protected virtual void New()
        {
        }

        public virtual void Dispose()
        {
            mInstance = null;
        }

        protected virtual void OnDestroy()
        {
            mInstance = null;
        }

        private void OnApplicationQuit()
        {
            ApplicationIsQuitting = true;
            if (mInstance == null)
            {
                return;
            }

            Destroy(mInstance.gameObject);
        }

        public void OnSingletonInit()
        {
            mInstance.New();
        }
    }
}