using FL.Battle;
using FL.Battle.Vfxs;
using Spine.Unity;
using TMPro;
using UnityEngine;
using XGame.Framework.Map;
using XGame.Framework.ObjectCollection;

namespace FL.Map
{
    public class MonsterEntityView : BattleEntityView<MonsterEntityVM>, IAttackContext, ISkillContext, ISpineContext, IVfxContext
    {
        public void FindTarget(int skillId)
        {
            EventSingle.Instance.Notify(EventDefine.GameMainMapFindTarget, new FindTargetArgs()
            {
                entityId = Entity.EntityId,
                entityType = EEntityType.Player,
                range = Entity.Attr.AtkRange,
                skillId = skillId,
            });
        }

        #region IAttackContext
        Transform IAttackContext.ActionRoot => VM.ActionRoot;
        #endregion
        #region ISpineContext
        SkeletonAnimation ISpineContext.Skeleton => VM.Skeleton;
        #endregion
        #region IVfxContext
        Transform IVfxContext.WorldTr => null;
        Transform IVfxContext.CenterTr => VM.Tr;
        Transform IVfxContext.CastTr => VM.Tr;
        #endregion
        protected override void OnStateChanged()
        {
            if (Entity.State == EEntityState.Idle)
                GetComponent<SpineComponent>().Play(ActionName.idle);
            VM.StateText.text = Entity.State.ToString();
        }

    }
    public class MonsterEntityVM : EntityViewModel
    {
        public TextMeshPro StateText { get; private set; }
        public Transform ActionRoot { get; private set; }
        public SkeletonAnimation Skeleton { get; private set; }
        protected override void OnInit(IObjectCollector collector)
        {
            StateText = collector.GetComponent<TextMeshPro>("StateText");
            ActionRoot = collector.GetGameObject("ActionRoot").transform;
            Skeleton = collector.GetComponent<SkeletonAnimation>("Skeleton");
        }
    }
}