using System.Collections.Generic;

namespace WS
{
    ///<summary>状态机基类</summary>
    public class FSM : WSComponent
    {
        /// <summary>状态对象库,保存AI所有状态</summary>
        private List<FSMState> FSMStates = new List<FSMState>();
        /// <summary>当前状态</summary>
        private FSMState CurrentState;
        /// <summary>默认状态</summary>
        private FSMState DefaultState;
        /// <summary>默认状态ID</summary>
        public int DefaultStateId;

        public virtual void Awake()
        {
            RegisterState();
            if (FSMStates.Count == 0)
            {
                Log.LogError("没有状态加入链表!");
                return;
            }
            DefaultState = FSMStates.Find(p => p.StateId == DefaultStateId);
            CurrentState = DefaultState;
            CurrentState.EnterState(this);
        }

        /// <summary>注册状态</summary>
        public virtual void RegisterState()
        {

        }

        /// <summary>Update 在状态判断之后执行</summary>
        public virtual void Update()
        {
            if (CurrentState != null)
            {
                CurrentState.StayState(this);
                CurrentState.Reason(this);
            }
        }

        /// <summary>添加状态</summary>
        public void AddState(FSMState state)
        {
            if (!FSMStates.Exists(p => p.StateId == state.StateId))
            {
                FSMStates.Add(state);
            }
        }

        /// <summary>删除状态</summary> 
        public void DeleteState(FSMState state)
        {
            if (FSMStates.Exists(p => p == state))
            {
                FSMStates.Remove(state);
            }
        }

        /// <summary>根据条件判断自动切换状态</summary> 
        /// <param name="triggerid">条件Id</param>
        public void TriggerSwitchState(int triggerid)
        {
            if (CurrentState == null)
                return;
            //根据条件编号，在当前状态查找输出状态
            int stateid = CurrentState.GetOutPutState(triggerid);

            //可能得到的3个结果  
            //1.-1 : 不处理
            if (stateid == -1)
                return;
            //退出当前状态
            CurrentState.ExitState(this);
            //2.默认状态: 将原默认状态设为当前状态
            if (stateid == DefaultStateId)
                CurrentState = DefaultState;
            else //3.具体状态: 将具体状态设为当前状态
                CurrentState = FSMStates.Find(p => p.StateId == stateid);
            //进入新状态
            CurrentState.EnterState(this);
            if (CurrentState != null)
                Log.LogDebug($"切换到 {CurrentState.StateId} 状态");
        }

        ///<summary>根据状态类型手动切换状态</summary>
        ///<param name="stateid">状态Id</param>
        public void SwitchState(int stateid)
        {
            FSMState state = FSMStates.Find(s => s.StateId == stateid);
            if (state == null)
            {
                Log.LogDebug($"状态机{this.GetType().Name}没有{stateid}状态");
            }

            //1.None : 不处理
            if (stateid == -1)
                return;
            //退出当前状态
            CurrentState.ExitState(this);
            //2.默认状态: 将原默认状态设为当前状态
            if (stateid == DefaultStateId)
                CurrentState = DefaultState;
            else //3.具体状态: 将具体状态设为当前状态
                CurrentState = state;
            //进入新状态
            CurrentState.EnterState(this);
            if (CurrentState != null)
                Log.LogDebug($"切换到 {CurrentState.StateId} 状态");
        }

        /// <summary>退出状态机</summary>
        public void QuitFSM()
        {
            CurrentState = null;
            DefaultState = null;
            DefaultStateId = -1;
        }

        public override void Dispose()
        {
            base.Dispose();
            for (int i = 0; i < FSMStates.Count; i++)
            {
                FSMStates[i].Dispose();
            }
            FSMStates.Clear();
        }
    }
}