using System;
using UnityEngine;
using XGame.Framework.Components;

namespace FL.Battle.Components.AI
{
    public interface IBattleEntityAIContext
    {
        IEntity Entity { get; }
        StatesComponent States { get; }
        MoveComponent Move { get; }
        SkillComponent Skill { get; }
    }
    /// <summary>
    /// 战斗对象的通用AI
    /// AI组件和具体的EntityView绑定，其他Component最好不要尝试获取AI组件
    /// </summary>
    public class BattleEntityAI : Component<IBattleEntityAIContext>
    {
        protected override void OnDispose()
        {
        }
        public void MoveTo(MoveArgs args)
        {
            args.onComplete += () =>
            {
                Context.States.RemoveState(EEntityState.Moving);
            };
            Context.States.AddState(EEntityState.Moving);
            Context.Move.MoveTo(args);
        }
        public void MoveToBattle(Vector3 position, int delay = 0, Action callback = null)
        {
            var entity = Context.Entity;
            var args = new MoveArgs
            {
                target = position,
                speed = entity.Attr.MoveSpeed,
                timeScale = entity.Attr.MoveSpeedScale,
                delay = delay,
                onComplete = () =>
                {
                    Context.States.RemoveState(EEntityState.Moving);
                    PrepareBattle();
                    callback.SafeInvoke();
                }
            };
            Context.States.AddState(EEntityState.Moving);
            // 打断技能并重置cd
            //Context.Skill.Stop(false);
            Context.Move.MoveTo(args);
        }
        public void PrepareBattle()
        {
            Context.Skill.Prepare();
        }
    }
}
