/// #pkgName FGUI包名
/// #panelName UIPanel名字
/// #UIName = $"{#pkgName}{#panelName}" UIKey名字
/// 该脚本由模板创建
/// created by cb 2024

using FL.Data.Items;
using FL.Nodes.DragonEgg;
using System.Collections.Generic;
using XGame.Database;
using XGame.Framework.UI;


namespace FL.FGUI
{
    /// <summary>
    /// UI逻辑处理类
    /// </summary>
    /// <typeparam name=""></typeparam>
    public partial class DragonEggEquipItemCtrl : UIController<DragonEggEquipItemVM>
    {
        private Dictionary<EAttributeType, DragonEggAttrItemView> _basicAttrItemMap;
        private List<EAttributeType> _allAttrTypeList; // 装备全部属性类型列表
        private List<EAttributeType> _specialAttrTypeList;

        protected override void OnEnable(object intent)
        {
            AddUIListenres();
        }
        protected override void OnDisable()
        {
            RemoveUIListenres();
            _basicAttrItemMap.Clear();
            _basicAttrItemMap = null;
            _allAttrTypeList.Clear();
            _allAttrTypeList = null;
            _specialAttrTypeList.Clear();
            _specialAttrTypeList = null;
        }
        #region UI事件
        private void AddUIListenres()
        {

        }
        private void RemoveUIListenres()
        {

        }

        #endregion


        private void InitUI()
        {
            if (_basicAttrItemMap == null)
            {
                _basicAttrItemMap = new Dictionary<EAttributeType, DragonEggAttrItemView>();
            }
            else
            {
                _basicAttrItemMap.Clear();
            }
            _basicAttrItemMap.Add(EAttributeType.Hp, VM.HpAttrItem);
            _basicAttrItemMap.Add(EAttributeType.Atk, VM.AtkAttrItem);
            _basicAttrItemMap.Add(EAttributeType.Def, VM.DefAttrItem);
            _basicAttrItemMap.Add(EAttributeType.AtkSpeed, VM.SpeedAttrItem);

            VM.SpecialAttrItem1.Ctrl.ResetUI();
            VM.SpecialAttrItem2.Ctrl.ResetUI();

            if (_allAttrTypeList == null)
                _allAttrTypeList = new List<EAttributeType>();
            else
                _allAttrTypeList.Clear();

            if (_specialAttrTypeList == null)
                _specialAttrTypeList = new List<EAttributeType>();
            else
                _specialAttrTypeList.Clear();
        }

        public void ShowUI(EquipItem newEquip, EquipItem wearEquip, bool bWear = false)
        {
            VM.State.selectedIndex = bWear ? 0 : 1;
            EquipItem equipData = bWear ? wearEquip : newEquip;
            ShowEquipItemUI(equipData);
            ShowAttrUi(equipData, wearEquip, bWear);
        }

        private void ShowEquipItemUI(EquipItem equipData)
        {
            UiParam _uiParam = new UiParam();
            _uiParam.bShowLevel = true;
            VM.ItemBtn.Ctrl.ShowUI(equipData, _uiParam);

            VM.NameLabel.text = equipData.Name;
            VM.NameLabel.color = equipData.Quality.ToEquipNameColor();
        }

        /// <summary>
        /// 装备的属性UI
        /// </summary>
        /// <param name="equipData"></param>
        /// <param name="wearEquip"></param>
        /// <param name="bWear"></param>
        private void ShowAttrUi(EquipItem equipData, EquipItem wearEquip, bool bWear)
        {
            InitUI();
            //装备的属性列表
            equipData.Attributes.GetAll(ref _allAttrTypeList, 0);
            equipData.Attributes.GetAll(ref _specialAttrTypeList, 2);
            if (_specialAttrTypeList.Count > 0)
            {
                _basicAttrItemMap.Add(_specialAttrTypeList[0], VM.SpecialAttrItem1);
            }
            if (_specialAttrTypeList.Count > 1)
            {
                _basicAttrItemMap.Add(_specialAttrTypeList[1], VM.SpecialAttrItem2);
            }

            foreach (var item in _basicAttrItemMap)
            {
                bool bShowAttrItem = _allAttrTypeList.FindIndex(attrType => attrType == item.Key) != -1;
                if (bShowAttrItem)
                {
                    long attributeVal = equipData.Attributes.GetValue(item.Key);
                    var compareType = bWear ? eCompareType.None : GetCompareType(item.Key, attributeVal, wearEquip);
                    item.Value.Ctrl.ShowUI(item.Key, attributeVal, compareType, true);
                }
                else
                {
                    item.Value.Ctrl.ShowUI(item.Key, 0, eCompareType.None, false);
                }
            }
        }

        /// <summary>
        /// 新装备属性的比较标识
        /// </summary>
        /// <param name="attributeType"></param>
        /// <param name="atrrVal"></param>
        /// <param name="wearEquip"></param>
        /// <returns></returns>
        private eCompareType GetCompareType(EAttributeType attributeType, long atrrVal, EquipItem wearEquip)
        {
            if (wearEquip == null) return eCompareType.None;
            long val = wearEquip.Attributes.GetValue(attributeType);
            return atrrVal == val ? eCompareType.None : (atrrVal > val ? eCompareType.Higth : eCompareType.Low);
        }

    }
}