using FL.Battle;
using FL.Battle.Buffs;
using UnityEngine;
using UnityEngine.UI;
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;
            var shieldImg = VM.ShieldImg;
            var totalShield = Master.Buffs.TotalShieldVal;
            shieldImg.gameObject.SetActive(totalShield > 0);
            var totalHp = _master.Entity.Attr.HpLimit + totalShield;
            var curHp = _master.Entity.Attr.CurrentHp;
            if (hpImg.type == Image.Type.Filled)
            {
                hpImg.fillAmount = (float)curHp / totalHp;
                if (totalShield > 0)
                {
                    shieldImg.fillAmount = (float)(curHp + totalShield) / totalHp;
                }
            }
            else
            {
                var size = _hpImgSize;
                size.x = size.x * curHp / totalHp;
                hpImg.rectTransform.sizeDelta = size;
                if (totalShield > 0)
                {
                    size = _hpImgSize;
                    size.x = size.x * (curHp + totalShield) / totalHp;
                    shieldImg.rectTransform.sizeDelta = size;
                }
            }

        }
        public void AddHp(int addVal)
        {
            //TODO 动画
            RefreshHp();
        }
        public void RefreshBuffs()
        {
            if (Master.Buffs.IsShieldDirty)
            {
                Master.Buffs.IsShieldDirty = false;
                RefreshHp();
            }
            if (VM.BuffList == null || Master.Buffs.IsDirty == false)
                return;
            Master.Buffs.IsDirty = false;
            var buffs = XGame.Framework.ListPool.Acquire<IBuff>();
            Master.Buffs.GetBuffs(ref buffs);
            VM.BuffList.Refresh(buffs, Context);
            XGame.Framework.ListPool.Recycle(buffs);
        }
    }
    public class EntityBattleInfoVM : EntityViewModel
    {
        public Image HpImg { get; private set; }
        public Image ShieldImg { get; private set; }
        internal BattleInfoBuffList BuffList { get; private set; }
        protected override void OnInit(IObjectCollector collector)
        {
            HpImg = collector.GetComponent<Image>("HpImg");
            ShieldImg = collector.GetComponent<Image>("ShieldImg");
            BuffList = collector.GetComponent<BattleInfoBuffList>("BuffList");
        }
    }
}
