namespace Guru
{
    using System.Collections.Generic;
    using UnityEngine;
    
    /// 
    /// 打点管理
    /// 
    public partial class GuruSDK
    {
        /// 
        /// 主线关卡类型
        /// 只有传入此类型时才会进行 Blevel 的累加
        /// 
        public const string LevelCategoryMain = "main";
        
        //----------------- 关卡开始类型 ---------------------
        public const string EventLevelStartModePlay = "play";
        public const string EventLevelStartModeReplay = "replay";
        public const string EventLevelStartModeContinue= "continue";
        
        //----------------- 关卡结束类型 ---------------------
        public const string EventLevelEndSuccess = "success";
        public const string EventLevelEndFail = "fail";
        public const string EventLevelEndExit = "exit";
        public const string EventLevelEndTimeout = "timeout";
        #region 通用接口
        /// 
        /// 自定义事件打点
        /// 
        /// 
        /// 
        public static void LogEvent(string eventName, Dictionary data = null)
        {
            if (!IsInitialSuccess)
            {
                Debug.LogError($"{Tag} Please call  first, before you call .");
                return;
            }
            Analytics.Track(eventName, data);
        }
        public static void SetScreen(string screen, string extra = "")
        {
            if (!IsInitialSuccess)
            {
                Debug.LogError($"{Tag} Please call  first, before you call .");
                return;
            }
            Analytics.SetCurrentScreen(screen, extra);
        }
        #endregion
        
        #region 游戏打点
        /// 
        /// 游戏启动打点
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static void LogLevelStart(int level, string startType = EventLevelStartModePlay, 
            string levelCategory = LevelCategoryMain, string levelName = "", string levelID = "",
            bool isReplay = false)
        {
            if (!IsInitialSuccess)
            {
                Debug.LogError($"{Tag} Please call  first, before you call .");
                return;
            }
            Analytics.LogLevelStart(level, levelName, levelCategory, levelID, startType, isReplay);
        }
        
        /// 
        /// 游戏点击 Continue 重开始游戏
        /// 
        /// 
        /// 
        /// 
        /// 
        public static void LogLevelContinue(int level, string levelCategory = LevelCategoryMain,
            string levelName = "", string levelID = "")
        {
            LogLevelStart(level, EventLevelStartModeContinue, levelCategory, levelName, levelID,  true);
        }
        
        /// 
        /// 游戏点击 Continue 重开始游戏
        /// 
        /// 
        /// 
        /// 
        /// 
        public static void LogLevelReplay(int level, string levelCategory = LevelCategoryMain,
            string levelName = "", string levelID = "")
        {
            LogLevelStart(level, EventLevelStartModeReplay,levelCategory, levelName, levelID,  true);
        }
        
        /// 
        /// 游戏胜利打点
        /// 
        public static void LogLevelEnd(int level,  string result = EventLevelEndSuccess,
            string levelCategory = LevelCategoryMain, string levelName = "", string levelID = "",
            int? duration = null, int? step = null, int? score = null )
        {
            if (!IsInitialSuccess)
            {
                Debug.LogError($"{Tag} Please call  first, before you call .");
                return;
            }
            
            
            if (InitConfig.AutoRecordFinishedLevels)
            {
                if(result == EventLevelEndSuccess){
                    if(levelCategory == LevelCategoryMain) Model.SuccessLevelCount++; // 自动记录关卡完成次数
                    Model.TotalPlayedCount++; // 自动记录关卡总次数
                }
                
                Analytics.BLevel = Model.SuccessLevelCount; // 记录 BLevel
                Analytics.BPlay = Model.TotalPlayedCount; // 记录 BPlay
            }
            
            Analytics.LogLevelEnd(level, result, 
                levelName, levelCategory, levelCategory,
                duration, step, score);
        }
        
        
        /// 
        /// 游戏失败打点
        /// 需要为游戏记录详细的失败原因
        /// 
        public static void LogLevelFail(int level,
            string levelCategory = LevelCategoryMain, string levelName = "", string levelID = "",
            int? duration = null, int? step = null, int? score = null )
        {
            LogLevelEnd(level, EventLevelEndFail, levelCategory, levelName, levelID, duration, step, score);
        }
        /// 
        /// 因退出关卡导致游戏失败
        /// 
        public static void LogLevelFailExit(int level,
            string levelCategory = LevelCategoryMain, string levelName = "", string levelID = "",
            int? duration = null, int? step = null, int? score = null)
        {
            LogLevelEnd(level, EventLevelEndExit, levelCategory, levelName, levelID, duration, step, score);
        }
        /// 
        /// 因关卡超时导致游戏失败
        /// 
        public static void LogLevelFailTimeout(int level,
            string levelCategory = LevelCategoryMain, string levelName = "", string levelID = "",
            int? duration = null, int? step = null, int? score = null)
        {
            LogLevelEnd(level, EventLevelEndTimeout, levelCategory, levelName, levelID, duration, step, score);
        }
        /// 
        /// 玩家(角色)升级事件
        /// 
        /// 
        /// 
        public static void LogLevelUp(int playerLevel, string playerName)
        {
            if (!IsInitialSuccess)
            {
                Debug.LogError($"{Tag} Please call  first, before you call .");
                return;
            }
            Analytics.LevelUp(playerLevel, playerName);
        }
        /// 
        /// 玩家解锁成就
        /// 
        /// 
        public static void LogAchievement(string achievementName)
        {
            if (!IsInitialSuccess)
            {
                Debug.LogError($"{Tag} Please call  first, before you call .");
                return;
            }
            Analytics.UnlockAchievement(achievementName);
        }
        
        #endregion
        #region 用户属性
        /// 
        /// 设置用户属性
        /// 
        /// 
        /// 
        public static void SetUserProperty(string key, string value)
        {
            if (!IsInitialSuccess)
            {
                Debug.LogError($"{Tag} Please call  first, before you call .");
                return;
            }
            Analytics.SetUserProperty(key, value);
        }
        public static void SetUID(string uid)
        {
            SetUserProperty(Analytics.PropertyUserID, uid);
        }
        public static void SetUserBLevel(int blevel)
        {
            SetUserProperty(Analytics.PropertyLevel, $"{blevel}");
        }
        
        public static void SetUserBPlay(int bplay)
        {
            SetUserProperty(Analytics.PropertyPlay, $"{bplay}");
        }
        public static void SetUserTotalCoins(int totalCoins)
        {
            SetUserProperty(Analytics.PropertyCoin, $"{totalCoins}");        
        }
        
        public static void SetUserCoins(int coins)
        {
            SetUserProperty(Analytics.PropertyNonIAPCoin, $"{coins}");        
        }
        
        public static void SetUserPaidCoins(int paidCoins)
        {
            SetUserProperty(Analytics.PropertyIAPCoin, $"{paidCoins}");        
        }
        
        public static void SetUserExp(int exp)
        {
            SetUserProperty(Analytics.PropertyExp, $"{exp}");        
        }
        
        public static void SetUserHp(int hp)
        {
            SetUserProperty(Analytics.PropertyHp, $"{hp}");        
        }
        public static void SetUserGrade(int grade)
        {
            SetUserProperty(Analytics.PropertyGrade, $"{grade}");        
        }
        
        
        #endregion
    }
}