using System;
using MrWu.Debug;

namespace Server.Core
{
    public abstract class ASingleton : IDisposable
    {
        public abstract void Register();
        
        public virtual void Dispose()
        {
        }
    }

    public abstract class Singleton<T> : ASingleton where T : Singleton<T>
    {
        private bool isDisposed;

        private static T instance;

        public static T Instance
        {
            get
            {
                return instance;
            }
            private set
            {
                instance = value;
            }
        }

        public override void Register()
        {
            Instance = (T)this;
            Instance.Awake();
        }
        
        public bool IsDisposed()
        {
            return this.isDisposed;
        }

        protected virtual void Awake()
        {
            
        }

        protected virtual void Destroy()
        {
            
        }

        public override void Dispose()
        {
            if (this.isDisposed)
            {
                return;
            }

            this.isDisposed = true;
            this.Destroy();

            Instance = null;
        }
    }
}