using Axibug;
using Axibug.Event;
using System;
using System.Collections.Generic;
using System.Configuration;
using UnityEngine;

namespace Game
{
    public enum E_MOTION_TYPE
    {
        None,
        Attack_1,
        Attack_2,
        Attack_3,
    }

    public class MontionkeyClass
    {
        public int Frame;
        public E_MOTION_TYPE MontionType;
    }

    //主控玩家角色操作数据
    public class InputMotionData
    {
        /// <summary>
        /// 释放出来的行为历史
        /// </summary>
        List<MontionkeyClass> KeyHistory = new List<MontionkeyClass>();
        static Queue<MontionkeyClass> tempHistoryQueue = new Queue<MontionkeyClass>();
        const int MontionHistoryLimit = 10;

        bool mNeedClear = false;
        const float ClearCD = 0.3f;
        //剩余清理时间
        float flagTime = 0;
        bool NeedClear { get { return mNeedClear; }
             set {
                if (value)
                {
                    flagTime = ClearCD;
                }
                else
                {
                    flagTime = 0;
                }
                mNeedClear = value;
            } 
        }

        public void Init()
        {
            ClearHistoryMotion();
        }

        public void OnEnable()
        {

            AppEntry.Event.Subscribe(MainPlayerOnceAttackEventArgs.EventId, OnMainPlayerInAttackEventArgs);
        }

        public void OnDestory()
        {
            AppEntry.Event.Unsubscribe(MainPlayerOnceAttackEventArgs.EventId, OnMainPlayerInAttackEventArgs);
        }

        private void OnMainPlayerInAttackEventArgs(object sender, LogicEventArgs e)
        {
            MainPlayerOnceAttackEventArgs msg = (MainPlayerOnceAttackEventArgs)e;
            if (msg == null) throw new GameException("MainPlayerOnceAttackEventArgs is null");
            if (msg.RoleID == GamePlayEntry.MainPlayer.Player.RoleID)
            {
                if (msg.Step == E_ONCEATTACK_STEP.InAttack)
                    NeedClear = false;
                else if(msg.Step == E_ONCEATTACK_STEP.None)
                    NeedClear = true;
            }
        }

        MontionkeyClass EnqueueOneMotionHistory()
        {
            if (tempHistoryQueue.Count > 0)
                return tempHistoryQueue.Dequeue();

            return new MontionkeyClass();
        }

        public void AddMontionKey(E_MOTION_TYPE type)
        {
            while (KeyHistory.Count > MontionHistoryLimit)
            {
                tempHistoryQueue.Enqueue(KeyHistory[0]);
                KeyHistory.RemoveAt(0);
            }

            MontionkeyClass motion = EnqueueOneMotionHistory();

            motion.Frame = Time.frameCount;
            motion.MontionType = type;
            KeyHistory.Add(motion);
        }

        public void ClearHistoryMotion()
        {
            for (int i = 0; i < KeyHistory.Count; i++)
            {
                tempHistoryQueue.Enqueue(KeyHistory[i]);
            }
            KeyHistory.Clear();
            NeedClear = false;
            AxibugLog.Debug("清理MotionHistory");
        }

        public bool CheckHistoryLastMotion(E_MOTION_TYPE type)
        {
            if (KeyHistory.Count == 0) 
                return false;
            return KeyHistory[KeyHistory.Count - 1].MontionType == type;
        }

        /// <summary>
        /// 检查连续行为是否匹配
        /// </summary>
        /// <param name="types"></param>
        /// <returns></returns>
        public bool CheckHistoryLastMotion(E_MOTION_TYPE[] types)
        {
            if (KeyHistory.Count < types.Length)
                return false;

            for (int i = 0; i < types.Length; i++)
            {
                if (KeyHistory[KeyHistory.Count - i - 1].MontionType != types[types.Length - i - 1])
                {
                    return false;
                }
            }

            return true;
        }

        public void Update_Logic()
        {
            if (NeedClear)
            {
                flagTime -= Time.deltaTime;
                if (flagTime <= 0)
                {
                    ClearHistoryMotion();
                    NeedClear = false;
                }
            }
        }
    }
}