using System;
using System.Collections.Generic;
using Excel2Json;
using Fort23.Core;
using Fort23.UTool;
using UnityEngine.Device;
namespace Fort23.Mono
{
    public delegate CTask CustomAction(UnityEngine.Object o);
    
    public abstract class IGuideLogic
    {
        // /// 
        // /// 是否是软性引导(还需要等待一些特殊的条件才触发的引导)
        // /// 
        // public virtual bool issoftGuide => false;
        /// 
        /// 引导ID
        /// 
        public int guideID;
        public int guideIdx = 0;
        protected List actionList = new List();
        protected int step;
        public UnlockConfig uLockConfig;
        public object ActionObject;
        public PlayerGuideManager pgm;
        // public IGuideLogic(PlayerGuideManager guideManager)
        // {
        //     this.pgm = guideManager;
        // }
        /// 
        /// 新手,引导吧...
        /// 
        public virtual async CTask Guide()
        {
        }
        public void Redo(int idx)
        {
            actionList[idx](null);
        }
        public void ClearList()
        {
            actionList.Clear();
        }
        /// 
        /// 是否全面屏
        /// 
        /// 
        public bool IsFullScreen()
        {
            float bl = Screen.width * 1.0f / Screen.height;
            if (bl <= 0.5f)
            {
                return true;
            }
            return false;
        }
        /// 
        /// 之前引导是否被中断?
        /// 
        /// 引导长度
        /// 当前步骤
        /// 
        protected bool IsPhaseBreak(int length, int key)
        {
            step = GetPhaseStep(key);
            //new do
            if (step == -1)
            {
                return false;
            }
            else if (step != length - 1)
            {
                LogTool.Log("上次引导中断了: " + key + "-" + step);
                return true;
            }
            else
            {
                return false;
            }
        }
        protected bool IsPhaseDone(int length, int key)
        {
            step = GetPhaseStep(key);
            if (step != -1)
            {
                FinishTriggerGuide(key);
                return true;
            }
            return false;
        }
        protected void FinishTriggerGuide(int key)
        {
            PlayerGuideManager.Instance.ResetPlayerGuide();
            PlayerGuideManager.Instance.FinishATriggerGuide();
            LogTool.Log("已完成引导:" + key);
        }
        public int GetPhaseStep(int key)
        {
            if (AccountFileInfo.Instance.playerData.phaseKey.Contains(key))
            {
                int idx = AccountFileInfo.Instance.playerData.phaseKey.IndexOf(key);
                return AccountFileInfo.Instance.playerData.phaseValue[idx];
            }
            else
            {
                return -1;
            }
        }
        protected void SaveStep(int s)
        {
            //影子引导不记录.
            if (PlayerGuideManager.Instance.curPhase == 66)
            {
                return;
            }
            PlayerGuideManager.Instance.curStep = s;
            var phase = PlayerGuideManager.Instance.curPhase;
            AccountFileInfo.Instance.playerData.curPhase = phase;
            AccountFileInfo.Instance.playerData.curStep = s;
            if (!AccountFileInfo.Instance.playerData.phaseKey.Contains(phase))
            {
                AccountFileInfo.Instance.playerData.phaseKey.Add(phase);
                AccountFileInfo.Instance.playerData.phaseValue.Add(s);
            }
            else
            {
                var idx = AccountFileInfo.Instance.playerData.phaseKey.IndexOf(phase);
                if (AccountFileInfo.Instance.playerData.phaseValue[idx] < s)
                {
                    AccountFileInfo.Instance.playerData.phaseValue[idx] = s;
                }
            }
            LogTool.Log("guide message:" + "Phase=" + phase + "  Step=" + s);
            AccountFileInfo.Instance.SavePlayerData();
        }
        public void SavePhase(int phase, int step = -1)
        {
            AccountFileInfo.Instance.playerData.curPhase = phase;
            AccountFileInfo.Instance.playerData.curStep = step;
            if (!AccountFileInfo.Instance.playerData.phaseKey.Contains(phase))
            {
                AccountFileInfo.Instance.playerData.phaseKey.Add(phase);
                AccountFileInfo.Instance.playerData.phaseValue.Add(step);
            }
            else
            {
//            var idx = AccountFileInfo.Instance.playerSettingInfo.phaseKey.IndexOf(phase);
//            AccountFileInfo.Instance.playerSettingInfo.phaseValue[idx] = step;
                LogTool.Warning("已保存过该步骤:" + phase);
            }
            AccountFileInfo.Instance.SavePlayerData();
        }
        public void SaveForceDone()
        {
            AccountFileInfo.Instance.playerData.isForceDone = true;
            AccountFileInfo.Instance.SavePlayerData();
        }
        public void SaveTriggerDone()
        {
            AccountFileInfo.Instance.playerData.isTriggerDone = true;
            AccountFileInfo.Instance.SavePlayerData();
        }
        /// 
        /// 激活
        /// 
        public virtual void Active()
        {
            isSkipGuide = false;
        }
        /// 
        /// 是否跳过本引导(一般是引导过期了)
        /// 
        public bool isSkipGuide = false;
        /// 
        /// 开始
        /// 
        public virtual void Begin()
        {
            // if (uLockConfig.ulockCondition == 1)
            // {
            //     if (PlayerManager.Instance.curGuideGroupId != uLockConfig.ulockValue)
            //     {
            //         isSkipGuide = true;
            //     }
            // }
            // else if (uLockConfig.ulockCondition == 2)
            // {
            //     if (PlayerManager.Instance.Level > uLockConfig.ulockValue)
            //     {
            //         isSkipGuide = true;
            //     }
            // }
            //
            // if (!isSkipGuide)
            // {
            //     if (IsPhaseDone(actionList.Count, guideID))
            //     {
            //         isSkipGuide = true;
            //     }
            // }
        }
        /// 
        /// 更新循环
        /// 
        public virtual void LogicUpdate()
        {
        }
        /// 
        /// 结束
        /// 
        public virtual void End()
        {
            
        }
        /// 
        /// 释放逻辑,关闭
        /// 
        public virtual void LogicRelase()
        {
            PlayerGuideManager.Instance.guideLogic = null;
        }
        
        public virtual void SkipGuide()
        {
            PlayerGuideManager.Instance.CloseForceGuide();
        }
    }
}