using FL.Battle;
using FL.Battle.Components;
using FL.Battle.Components.AI;
using System;
using UnityEngine;
using XGame.Framework.Map;
using XGame.Framework.Time;

namespace FL.Map
{
    /// <summary>
    /// 玩家、随从、怪物等有IEntity的战斗对象基类
    /// </summary>
    /// <typeparam name="TViewModel"></typeparam>
    public abstract class BattleEntityView<TViewModel> : EntityView<TViewModel>, IMoveContext, IBuffsContext, ICombatContext, IBattleEntityAIContext, ITarget where TViewModel : EntityViewModel
    {
        public IMapAssetModule Asset => Context.Asset;
        public ITimeModule Time => Context.Time;
        private IEntity _entity;
        public IEntity Entity
        {
            get => _entity;
            set
            {
                _entity = value;
                LocalPosition = value?.BirthPosition ?? Vector3.zero;
                States.Reset();
            }
        }
        public bool IsDead => _entity.IsDead;
        #region 状态
        //private ITimer _deadTimer;
        //public EEntityState State
        //{
        //    get => _entity.State;
        //    set
        //    {
        //        if (value != EEntityState.Idle && value == _entity.State)
        //        {
        //            return;
        //        }
        //        if (_entity.State == EEntityState.Dead)
        //        { // 已经死亡，不能再修改状态
        //            return;
        //        }
        //        _entity.State = value;
        //        //VM.StateText.text = value.ToString();
        //        //Animation
        //        if (value == EEntityState.Dead)
        //        {
        //            //TODO 死亡动作  停止所有组件
        //            _deadTimer = Context.Time.AddDelayTimer(500, () =>
        //            { // 死亡事件通知
        //                _deadTimer = null;
        //                EventSingle.Instance.Notify(EventDefine.GameMainMapRemoveEntity, _entity.EntityId);
        //            });
        //        }
        //        OnStateChanged();
        //    }
        //}
        //protected abstract void OnStateChanged();

        #endregion

        #region IMoveContext
        public bool IsMoving => Move.IsMoving;
        public Transform OwnerTr => VM.Tr;

        #endregion

        #region 目标选择
        public abstract Transform UIPoint { get; }
        /// <summary>
        /// 默认的目标类型
        /// </summary>
        protected abstract EEntityType DefaultTargetType { get; }
        public ITargetSelector Selector { get; set; }

        public void FindTargetAsync(Action<ITarget> onSelected)
        {
            Selector.FindAsync(this, DefaultTargetType, ETargetFindType.Nearest, onSelected);
        }
        #endregion

        #region 组件
        public VfxComponent Vfx => GetComponent<VfxComponent>();
        public MoveComponent Move => GetComponent<MoveComponent>();
        public SkillComponent Skill => GetComponent<SkillComponent>();
        public BuffsComponent Buffs => GetComponent<BuffsComponent>();
        public BattleEntityAI CommonAI => GetComponent<BattleEntityAI>();
        public StatesComponent States => GetComponent<StatesComponent>();
        public ICombatCalculation Calculation => GetComponent<CombatComponent>();
        public abstract EpigraphicsComponent Epigraphics { get; }
        #endregion
        protected override void OnEnable(object intent)
        {
        }
        protected override void OnDisable()
        {
            Selector = null;
        }

        /// <summary>
        /// 重置状态
        /// </summary>
        /// <param name="isOnlyComponent">是否只重置Component</param>
        public void Reset(bool isOnlyComponent = false)
        {
            Group.Reset();
            //States.State = EEntityState.Idle;
            if (isOnlyComponent)
                return;
            LocalPosition = _entity.BirthPosition;
        }
    }
}
