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

using FairyGUI;
using FL.Data;
using FL.Data.Items;
using XGame.Database;
using XGame.Framework.UI;

namespace FL.FGUI
{
    public class EquipParam
    {
        public long id; // 装备唯一id
        public EEquipType equipType;
    }

    /// <summary>
    /// UI逻辑处理类
    /// </summary>
    /// <typeparam name=""></typeparam>
    public partial class DragonEggEquipCtrl : UIController<DragonEggEquipVM>
    {
        private EquipItem _newEquip;
        private EquipItem _wearEquip;
        protected override void OnEnable(object intent)
        {
            AddUIListenres();
            ShowUI(intent as EquipParam);
        }
        protected override void OnDisable()
        {
            _newEquip = null;
            _wearEquip = null;
            RemoveUIListenres();
        }
        #region UI事件
        private void AddUIListenres()
        {
            VM.SellBtn.onClick.Add(OnClickSellBtn);
            VM.WearBtn.onClick.Add(OnClickWearBtn);
            VM.DecomposeBtn.onClick.Add(OnClickDecomposeBtn);
        }
        private void RemoveUIListenres()
        {
            VM.SellBtn.onClick.Remove(OnClickSellBtn);
            VM.WearBtn.onClick.Remove(OnClickWearBtn);
            VM.DecomposeBtn.onClick.Remove(OnClickDecomposeBtn);
        }

        #endregion

        /// <summary>
        /// 出售按钮事件
        /// </summary>
        /// <param name="context"></param>
        private void OnClickSellBtn(EventContext context)
        {
            if (_newEquip == null) return;
            if (_newEquip.FightingPower > _wearEquip.FightingPower)
            {
                XGame.Log.Warn($"当前要出售的装备{_newEquip.FightingPower}战力高于身上穿戴的{_wearEquip.FightingPower}");
                ConfirmationBoxParam dialogParam = new ConfirmationBoxParam()
                {
                    cnt = StringDefine.sellHightPowerEquip,
                    tipsKey = EDayTipsKey.SellEquip,
                    onPromiseCallback = () => { SellEquip(); },
                };
                Context.UI.OpenAsync(UIKeys.CommonConfirmationBox, dialogParam);
                return;
            }
            SellEquip();
        }
        private void SellEquip()
        {
            DragonEggService.Instance.SellEquip(_newEquip);
            ClosePanel();
        }

        /// <summary>
        /// 穿戴|替换按钮事件
        /// </summary>
        /// <param name="context"></param>
        private void OnClickWearBtn(EventContext context)
        {
            if (_newEquip == null) return;
            DragonEggService.Instance.OnChangeEquip(_newEquip.EquipType, _newEquip);
            ClosePanel();
        }

        /// <summary>
        /// 分解按钮事件
        /// </summary>
        /// <param name="context"></param>
        private void OnClickDecomposeBtn(EventContext context)
        {
            ClosePanel();
        }

        private void ClosePanel()
        {
            Context.ClosePanel();
        }

        private void ShowUI(EquipParam param)
        {
            if (param == null) return;
            var id = param.id;
            if (param?.equipType != null)
            {
                var wearPartEquip = ItemData.Instance.GetEquipData(param.equipType);
                if (wearPartEquip != null && wearPartEquip.Id == id)
                {
                    ShowEquipInfo(wearPartEquip);
                    return;
                }
            }

            _newEquip = ItemData.Instance.GetNewEquip(param.id);
            if (_newEquip == null) return;

            _wearEquip = ItemData.Instance.GetEquipData(_newEquip.EquipType);

            bool bShowWear = _wearEquip != null;
            if (bShowWear)
            {
                VM.WearEquipItem.Ctrl.ShowUI(_newEquip, _wearEquip, true);
            }

            VM.NewEquipItem.Ctrl.ShowUI(_newEquip, _wearEquip);
            ShowBtnUI(bShowWear);
        }

        private void ShowBtnUI(bool bWear)
        {
            if (bWear)
            {
                //1_替换和出售   2_【替换】、【出售】、【分解】三个按钮
                VM.BtnState.selectedIndex = DragonEggData.Instance.IsUnLockDecompose ? 2 : 1;
            }
            else
            {
                // 当前没有同位置的装备,可使用的功能按钮只有【穿戴】
                VM.BtnState.selectedIndex = 0;
            }
        }

        private void ShowEquipInfo(EquipItem equip)
        {
            // 展示装备属性详情
            VM.BtnState.selectedIndex = 3;
            VM.NewEquipItem.Ctrl.ShowUI(equip, equip, true);
        }
    }
}