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

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

namespace FL.FGUI
{
    /// <summary>
    /// UI逻辑处理类
    /// </summary>
    /// <typeparam name=""></typeparam>
    public partial class DragonEggEquipmentUICtrl : UIController<DragonEggEquipmentUIVM>
    {
        private List<CommonItemBaseView> _equipItemList;
        protected override void OnEnable(object intent)
        {
            AddUIListenres();
            EventSingle.Instance.AddListener(EventDefine.OnChangeEquip, OnChangeEquip);
            Init();

            ShowEquipUI();
        }
        protected override void OnDisable()
        {
            RemoveUIListenres();
            EventSingle.Instance.RemoveListener(EventDefine.OnChangeEquip, OnChangeEquip);
            if (_equipItemList != null)
            {
                _equipItemList.Clear();
                _equipItemList = null;
            }
        }
        #region UI事件
        private void AddUIListenres()
        {

        }
        private void RemoveUIListenres()
        {

        }

        #endregion

        private void Init()
        {
            if (_equipItemList == null)
            {
                _equipItemList = new List<CommonItemBaseView>()
                {
                    VM.Weapon,VM.Helmet,VM.Clothes,VM.Hand,VM.Pants,VM.Boots
                };
            }
        }

        /// <summary>
        /// 显示装备UI
        /// </summary>
        private void ShowEquipUI()
        {
            UiParam _uiParam = new UiParam();
            _uiParam.bShowBtn = true;
            _uiParam.bShowLevel = true;
            int index = 1;
            _equipItemList.ForEach((item) =>
            {
                EquipItem equipData = ItemData.Instance.GetEquipData((EEquipType)index);
                if (equipData != null)
                {
                    item.Ctrl.ShowUI(ItemData.Instance.GetEquipData((EEquipType)index), _uiParam);
                }
                index++;
            });
        }

        /// <summary>
        /// 更换装备
        /// </summary>
        /// <param name="type"></param>
        /// <param name="id"></param>
        private void OnChangeEquip(int eventId, object args)
        {
            if (args == null)
            {
                return;
            }
            ChangeEquipParam param = (ChangeEquipParam)args;
            CommonItemBaseView equipItem = _equipItemList[(int)param.equipType - 1];
            if (equipItem != null)
            {
                UiParam _uiParam = new UiParam();
                _uiParam.bShowBtn = true;
                _uiParam.bShowLevel = true;
                equipItem.Ctrl.ShowUI(ItemData.Instance.GetEquipData(param.equipType), _uiParam);
            }
        }
    }
}