using System.Collections.Generic;
using UnityEngine;

namespace Stary.Evo
{
    public interface IFsmSystem
    {
        FSMIState CurState { get; set; }
        void AddState(FSMIState state);
        void RemoveState(FSMIState state);
        void SetCurState(string name);
        FSMIState GetStateWithName(string name);
        HashMap<string, FSMIState> States { get; set; }
    }

    public class FsmSystem : IFsmSystem
    {
        public FSMIState CurState { get; set; }
        public HashMap<string, FSMIState> States { get; set; }

        public FsmSystem()
        {
            States = new HashMap<string, FSMIState>();
        }

        public void AddState(FSMIState state)
        {
            Debug.Log(state.Name);
            if (!States.ContainsKey(state.Name))
            {
                States.Add(state.Name, state);
            }
            else
            {
                Debug.LogErrorFormat("States状态机容器里已存在名字为--【{0}】--的状态", state.Name.ToString());
            }
        }

        public void RemoveState(FSMIState state)
        {
            if (States.ContainsKey(state.Name))
            {
                States.Remove(state.Name);
            }
            else
            {
                Debug.LogErrorFormat("States状态机容器里不存在名字为--【{0}】--的状态", state.Name.ToString());
            }
        }

        public void SetCurState(string name)
        {
            if (CurState != null)
                CurState.OnExit();
            FSMIState state = GetStateWithName(name);
            CurState = state;
            CurState.OnEnter();
        }

        public FSMIState GetStateWithName(string name)
        {
            if (States.ContainsKey(name))
            {
                return States[name];
            }

            return null;
        }
    }
}