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

namespace FL.Map
{
    public class PartnerEntityView : BattleEntityView<PartnerEntityVM>, ISkillContext, IAnimatorContext, IVfxContext, IStatesContext, IEpigraphicsContext
    {
        /// <summary>
        /// 随从没有血条等UI资源
        /// </summary>
        public override Transform UIPoint => null;
        protected override EEntityType DefaultTargetType => EEntityType.Monster;
        //public void FindTarget(int skillId)
        //{
        //    Selector.FindAsync(this, EEntityType.Monster, (target) =>
        //    {
        //        SelectTarget(target, skillId);
        //    });
        //}
        public IAnimator Animator => GetComponent<AnimatorComponent>();
        #region ISkillContext
        Transform ISkillContext.ActionRoot => VM.ActionRoot;
        #endregion
        //#region ISpineContext
        //SkeletonAnimation ISpineContext.Skeleton => VM.Skeleton;
        //#endregion
        #region IAnimatorContext
        Animator IAnimatorContext.Animator => VM.Animator;
        #endregion
        #region IVfxContext
        Transform IVfxContext.WorldTr => null;
        Transform IVfxContext.CenterTr => VM.Tr;
        Transform IVfxContext.CastTr => VM.VfxRoot;

        #endregion
        int IEpigraphicsContext.EpigraphId => (Entity as PartnerEntity).EpigraphId;
        public override EpigraphicsComponent Epigraphics => GetComponent<EpigraphicsComponent>();
        /// <summary>
        /// 随从的身体镜像，所有武器默认向左，玩家左边的武器镜像翻转朝右
        /// </summary>
        public void BodyMirror()
        {
            //var scale = Vector3.one * PartnerTableRepo.Get(Entity.TableId).Size;
            //scale.z = 1;
            //if (LocalPosition.x < 0)
            //{
            //    scale.x *= -1;
            //}
            //VM.Skeleton.transform.localScale = scale;
        }
    }

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