using UnityEngine;

namespace Pursuit.Singletons
{
    public abstract class SingletonSO<T> : ScriptableObject where T : SingletonSO<T>
    {
        private static T _instance;

        // only really needed for the editor when Domain Reload
        // is disabled.

        public static bool InstanceExists => _instance != null;

        public static T Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = Resources.Load<T>("SingletonSO" + "/" + typeof(T).Name);
                    _instance.OnLoad();
                }

                return _instance;
            }
        }

        public static void ClearInstance()
        {
            _instance = null;
        }

        protected virtual void OnLoad()
        {
        }
    }
}