
namespace Guru
{
    using System;
    using System.Globalization;
    using UnityEngine;
    using System.IO;
    
    /// <summary>
    /// 应用的全局标准属性
    /// </summary>
    public static class StandardProperties
    {

        #region 初始化

        /// <summary>
        /// 全局属性初始化 需要在应用启动时调用一次
        /// </summary>
        public static void Init()
        {
            // 首次安装初始化各种参数
            if (IsFirstInstall)
            {
                string key = "";
                FirstInstallDate = DateTime.Now;
                
                key = nameof(SoundEffectEnabled);
                if (!HasKey(key)) SoundEffectEnabled = true;

                key = nameof(VibrationEnabled);
                if (!HasKey(key)) VibrationEnabled = true;

                key = nameof(FirstInstallVersion);
                if (!HasKey(key)) FirstInstallVersion = FullVersion;
                
                Save();
            }
        }

        private static bool IsFirstInstall => HasKey(nameof(FirstInstallDate));

        private static bool HasKey(string key) => PlayerPrefs.HasKey(key);

        private static void Save() => PlayerPrefs.Save();

        #endregion

        #region 标准属性值
        
        /// <summary>
        ///  FirebaseId
        /// </summary>
        public static string FirebaseId
        {
            get => PlayerPrefs.GetString(nameof(FirebaseId), "");
            set {
                if (!string.IsNullOrEmpty(value))
                {
                    PlayerPrefs.SetString(nameof(FirebaseId), value);
                    GuruAnalytics.SetFirebaseId(value);
                }
            }
        }
        
        /// <summary>
        /// Adjust ADID
        /// </summary>
        public static string AdjustId
        {
            get => PlayerPrefs.GetString(nameof(AdjustId), "");
            set {
                if (!string.IsNullOrEmpty(value))
                {
                    PlayerPrefs.SetString(nameof(AdjustId), value);
                    GuruAnalytics.SetAdjustId(value);
                }
            }
        }
        
        /// <summary>
        /// Google ADID 
        /// </summary>
        public static string GoogleAdId
        {
            get => PlayerPrefs.GetString(nameof(GoogleAdId), "");
            set {
                if (!string.IsNullOrEmpty(value))
                {
                    PlayerPrefs.SetString(nameof(GoogleAdId), value);
                    GuruAnalytics.SetAdId(value);
                }
            }
        }
        
        /// <summary>
        /// 免费金币资源
        /// </summary>
        public static int Coin
        {
            get => PlayerPrefs.GetInt(nameof(Coin), 0);
            set => PlayerPrefs.SetInt(nameof(Coin), value);
        }
        
        /// <summary>
        /// 付费金币资源
        /// </summary>
        public static int IAPCoin
        {
            get => PlayerPrefs.GetInt(nameof(IAPCoin), 0);
            set => PlayerPrefs.SetInt(nameof(IAPCoin), value);
        }
        
        /// <summary>
        /// 用户累计购买次数
        /// </summary>
        public static int PurchaseCount
        {
            get => PlayerPrefs.GetInt(nameof(PurchaseCount), 0);
            set => PlayerPrefs.SetInt(nameof(PurchaseCount), value);
        }
        
        /// <summary>
        /// 首次安装日期
        /// </summary>
        public static DateTime FirstInstallDate
        {
            get
            {
                var key = nameof(FirstInstallDate);
                if (PlayerPrefs.HasKey(key))
                {
                    var value = PlayerPrefs.GetString(key, "");
                    if (!string.IsNullOrEmpty(value)) return DateTime.Parse(value);
                }
                var now = DateTime.Now.ToUniversalTime();
                FirstInstallDate = now;
                return now;
            }

            set => PlayerPrefs.SetString(nameof(FirstInstallDate), 
                value.ToUniversalTime().ToString(CultureInfo.InvariantCulture));
        }

        /// <summary>
        /// 首次安装版本号
        /// </summary>
        public static string FirstInstallVersion
        {
            get => PlayerPrefs.GetString(nameof(FirstInstallVersion), "");
            set => PlayerPrefs.SetString(nameof(FirstInstallVersion), value);
        }

        /// <summary>
        /// 当前应用的版本
        /// </summary>
        public static string AppVersion => Application.version;
        
        /// <summary>
        /// 当前应用的版本Code
        /// </summary>
        public static string VersionCode
        {
            get
            {
                var path = $"{Application.streamingAssetsPath}/{GuruCore.VERSION_CODE_FILE}";
                if (File.Exists(path)) return File.ReadAllText(path);
                return "";
            }
        }

        /// <summary>
        /// 完整版本号
        /// </summary>
        public static string FullVersion
        {
            get
            {
                var code = VersionCode;
                if (string.IsNullOrEmpty(code)) code = "unknown";
                return $"{AppVersion}-{code}";
            }
        }
        
        /// <summary>
        /// 音效开关
        /// </summary>
        public static bool SoundEffectEnabled
        {
            get => PlayerPrefs.GetInt(nameof(SoundEffectEnabled), 0) == 1;
            set => PlayerPrefs.SetInt(nameof(SoundEffectEnabled), value? 1: 0);
        }
        
        /// <summary>
        /// 震动开关
        /// </summary>
        public static bool VibrationEnabled
        {
            get => PlayerPrefs.GetInt(nameof(VibrationEnabled), 0) == 1;
            set => PlayerPrefs.SetInt(nameof(VibrationEnabled), value? 1: 0);
        }
        
        #endregion

    }
}