using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StorageBase<T> where T : StorageBase<T>,new ()
{
    private static T mInstance;
    public static T Instance
    {
        get
        {
            if (mInstance == null)
            {
                Load();
            }

            return mInstance;
        }
    }

    protected virtual void InitFirstDefault()
    {

    }

    protected virtual void InitBuildDefault()
    {

    }

    public static void Load()
    {
        if (ES3.FileExists() && ES3.KeyExists(typeof(T).Name))
        {
            mInstance = ES3.Load<T>(typeof(T).Name);
        }
        else
        {
            mInstance = new T();

            string tFirstDefaultKey = "FirstDefault_" + typeof(T).Name;

            if (PlayerPrefs.GetInt(tFirstDefaultKey, 0) != 1)
            {
                PlayerPrefs.SetInt(tFirstDefaultKey, 1);
                mInstance.InitFirstDefault();
            }
        }

        string tBuildDefaultKey = "BuildDefault_" + typeof(T).Name + Application.buildGUID;

        if (PlayerPrefs.GetInt(tBuildDefaultKey, 0) != 1)
        {
            PlayerPrefs.SetInt(tBuildDefaultKey, 1);
            mInstance.InitBuildDefault();
        }

        Save();
    }

    public static void Save()
    {
        ES3.Save<T>(typeof(T).Name, mInstance);
    }
}