using FL.Battle;
using UnityEngine;
using UnityEngine.UI;
using XGame;
using XGame.Framework.Map;
using XGame.Framework.ObjectCollection;

namespace FL.Map.UI
{
    public class EntityBattleInfoView : EntityView<EntityBattleInfoVM>
    {
        private ITarget _master;
        public ITarget Master
        {
            get => _master; 
            set
            {
                _master = value;
                RefreshHp();
            }
        }
        private Vector2 _hpImgSize;

        protected override void OnInited()
        {
            //if (_hpImgSize == Vector2.zero)
            _hpImgSize = VM.HpImg.rectTransform.sizeDelta;
            //Log.Debug($"EntityBattleInfoView size:{_hpImgSize}");
        }
        protected override void OnEnable(object intent)
        {
        }
        protected override void OnDisable()
        {

        }
        protected override void OnDispose()
        {
            VM.HpImg.rectTransform.sizeDelta = _hpImgSize;
            _master = null;
        }
        public void RefreshHp()
        {
            var hpImg = VM.HpImg;
            if (hpImg.type == Image.Type.Filled)
            {
                hpImg.fillAmount = (float)_master.Entity.Attr.Hp / _master.Entity.Attr.HpLimit;
            }
            else
            {
                var size = _hpImgSize;
                size.x = size.x * _master.Entity.Attr.Hp / _master.Entity.Attr.HpLimit;
                hpImg.rectTransform.sizeDelta = size;
            }
            
        }
        public void AddHp(int addVal)
        {
            //TODO 动画
            RefreshHp();
        }
    }
    public class EntityBattleInfoVM : EntityViewModel
    {
        public Image HpImg { get; private set; }
        protected override void OnInit(IObjectCollector collector)
        {
            HpImg = collector.GetComponent<Image>("HpImg");
        }
    }
}
