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 PlayerEntityView : BattleEntityView<PlayerEntityVM>, IAttackContext, ISkillContext, ISpineContext, IVfxContext
    {
        public void FindTarget(int skillId)
        {
            EventSingle.Instance.Notify(EventDefine.GameMainMapFindTarget, new FindTargetArgs()
            {
                entityId = Entity.EntityId,
                entityType = EEntityType.Monster,
                range = 6,
                skillId = skillId,
            });
        }

        #region IAttackContext
        Transform IAttackContext.ActionRoot => VM.ActionRoot;
        #endregion
        #region ISpineContext
        SkeletonAnimation ISpineContext.Skeleton => VM.PlayerSkeleton;
        #endregion
        #region IVfxContext
        Transform IVfxContext.WorldTr => null;
        Transform IVfxContext.CenterTr => VM.Tr;
        Transform IVfxContext.CastTr => VM.VfxRoot;
        #endregion
        protected override void OnStateChanged()
        {
            if (Entity.State == EEntityState.Idle)
                GetComponent<SpineComponent>().Play(ActionName.idle);
            VM.StateText.text = Entity.State.ToString();
        }
    }
    public class PlayerEntityVM : EntityViewModel
    {
        public TextMeshPro StateText { get; private set; }
        public Transform ActionRoot {  get; private set; }
        public SkeletonAnimation PlayerSkeleton { get; private set; }
        public Transform VfxRoot { get; private set; }
        protected override void OnInit(IObjectCollector collector)
        {
            StateText = collector.GetComponent<TextMeshPro>("StateText");
            ActionRoot = collector.GetGameObject("ActionRoot").transform;
            PlayerSkeleton = collector.GetComponent<SkeletonAnimation>("PlayerSkeleton");
            VfxRoot = collector.GetGameObject("VfxRoot").transform;
        }
    }
}