using UnityEngine;

namespace _Core.Storage
{
    public static class UserPrefs
    {
        private const int StorageFalse = 0;
        private const int StorageTrue = 1;

        #region bool
        public static void SetBool(string key, bool value, bool saveImmediately = true)
        {
            int intValue = value ? StorageTrue : StorageFalse;
            UnityEngine.PlayerPrefs.SetInt(key, intValue);
            if (saveImmediately) { Save(); }
        }

        public static bool GetBool(string key)
        {
            int intValue = UnityEngine.PlayerPrefs.GetInt(key);
            return intValue == StorageTrue;
        }
        #endregion

        #region int
        public static void SetInt(string key, int value, bool saveImmediately = true)
        {
            UnityEngine.PlayerPrefs.SetInt(key, value);
            if (saveImmediately) { Save(); }
        }

        public static int GetInt(string key)
        {
            return UnityEngine.PlayerPrefs.GetInt(key);
        }

        public static int GetInt(string key, int defaultValue)
        {
            return UnityEngine.PlayerPrefs.GetInt(key, defaultValue);
        }
        #endregion

        #region float
        public static void SetFloat(string key, float value, bool saveImmediately = true)
        {
            UnityEngine.PlayerPrefs.SetFloat(key, value);
            if (saveImmediately) { Save(); }
        }

        public static float GetFloat(string key)
        {
            return UnityEngine.PlayerPrefs.GetFloat(key);
        }

        public static float GetFloat(string key, float defaultValue)
        {
            return UnityEngine.PlayerPrefs.GetFloat(key, defaultValue);
        }
        #endregion

        #region string
        public static void SetString(string key, string value, bool saveImmediately = true)
        {
            UnityEngine.PlayerPrefs.SetString(key, value);

            if (saveImmediately) { Save(); }
        }

        public static string GetString(string key)
        {
            return UnityEngine.PlayerPrefs.GetString(key);
        }

        public static string GetString(string key, string defaultValue)
        {
            return UnityEngine.PlayerPrefs.GetString(key, defaultValue);
        }
        #endregion

        public static bool HasKey(string key)
        {
            return UnityEngine.PlayerPrefs.HasKey(key);
        }

        public static void DeleteAll()
        {
            UnityEngine.PlayerPrefs.DeleteAll();
        }

        public static void DeleteKey(string key)
        {
            UnityEngine.PlayerPrefs.DeleteKey(key);
        }

        #region private
        private static void Save()
        {
            UnityEngine.PlayerPrefs.Save();
        }
        #endregion
    }
}
