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

using FairyGUI;
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 PlayerMainPanelCtrl : UIController<PlayerMainPanelVM>
    {
        private List<CommonItemBaseView> _equipItemList;
        private Dictionary<EAttributeType, string> _attributeDescMap;

        protected override void OnEnable(object intent)
        {
            AddUIListenres();
            EventSingle.Instance.AddListener(EventDefine.OnChangeEquip, OnChangeEquip);
            EventSingle.Instance.AddListener(EventDefine.AttributeChange, OnChangeAttribute); // 角色属性变化
            EventSingle.Instance.AddListener(EventDefine.PlayerExpAdd, AddPlayerExp);
            Init();
            ShowUI();
        }
        protected override void OnDisable()
        {
            RemoveUIListenres();
            EventSingle.Instance.RemoveListener(EventDefine.OnChangeEquip, OnChangeEquip);
            EventSingle.Instance.RemoveListener(EventDefine.AttributeChange, OnChangeAttribute); // 角色属性变化
            EventSingle.Instance.RemoveListener(EventDefine.PlayerExpAdd, AddPlayerExp);

            if (_equipItemList != null)
            {
                _equipItemList.Clear();
                _equipItemList = null;
            }

            if (_attributeDescMap != null)
            {
                _attributeDescMap.Clear();
                _attributeDescMap = null;
            }
            VM.PlayerSpine.url = string.Empty;
        }
        #region UI事件
        private void AddUIListenres()
        {
            VM.AttrInfoBtn.onClick.Add(OnClickAttrInfoBtn);
            VM.SkillBtn.onClick.Add(OnClickSkillBtn);
            VM.ChangeBtn.onClick.Add(OnClickChangeBtn);
            VM.MountBtn.onClick.Add(OnClickMountBtn);
            VM.ArtifactBtn.onClick.Add(OnClickArtifactBtn);
            VM.ContractBtn.onClick.Add(OnClickContractBtn);
            VM.JewelryBtn.onClick.Add(OnClickJewelryBtn);
            VM.NotOpenBtn1.onClick.Add(OnClickNotOpenBtn1);
            VM.NotOpenBtn2.onClick.Add(OnClickNotOpenBtn2);
        }
        private void RemoveUIListenres()
        {
            VM.AttrInfoBtn.onClick.Remove(OnClickAttrInfoBtn);
            VM.SkillBtn.onClick.Remove(OnClickSkillBtn);
            VM.ChangeBtn.onClick.Remove(OnClickChangeBtn);
            VM.MountBtn.onClick.Remove(OnClickMountBtn);
            VM.ArtifactBtn.onClick.Remove(OnClickArtifactBtn);
            VM.ContractBtn.onClick.Remove(OnClickContractBtn);
            VM.JewelryBtn.onClick.Remove(OnClickJewelryBtn);
            VM.NotOpenBtn1.onClick.Remove(OnClickNotOpenBtn1);
            VM.NotOpenBtn2.onClick.Remove(OnClickNotOpenBtn2);

        }

        private void OnClickAttrInfoBtn(EventContext context)
        {
            Context.UI.OpenAsync(UIKeys.PlayerDetailsAttribute);
        }
        private void OnClickSkillBtn(EventContext context)
        {
            EventSingle.Instance.Notify(EventDefine.ShowTips, "显示技能详情界面");
        }
        private void OnClickChangeBtn(EventContext context)
        {
            EventSingle.Instance.Notify(EventDefine.ShowTips, "更换契约技能");
        }
        private void OnClickMountBtn(EventContext context)
        {
            EventSingle.Instance.Notify(EventDefine.ShowTips, "打开坐骑界面");
        }
        private void OnClickArtifactBtn(EventContext context)
        {
            EventSingle.Instance.Notify(EventDefine.ShowTips, "打开神器界面");
        }
        private void OnClickContractBtn(EventContext context)
        {
            EventSingle.Instance.Notify(EventDefine.ShowTips, "打开锲约界面");
        }
        private void OnClickJewelryBtn(EventContext context)
        {
            EventSingle.Instance.Notify(EventDefine.ShowTips, "打开饰品装扮界面");
        }
        private void OnClickNotOpenBtn1(EventContext context)
        {
            EventSingle.Instance.Notify(EventDefine.ShowTips, StringDefine.notOpen);
        }
        private void OnClickNotOpenBtn2(EventContext context)
        {
            EventSingle.Instance.Notify(EventDefine.ShowTips, StringDefine.notOpen);
        }

        #endregion

        private void Init()
        {
            if (_equipItemList == null)
            {
                _equipItemList = new List<CommonItemBaseView>()
                {
                    VM.Weapon,VM.Helmet,VM.Clothes,VM.Hand,VM.Pants,VM.Boots
                };
            }
            if (_attributeDescMap == null)
            {
                _attributeDescMap = new Dictionary<EAttributeType, string>()
                {
                    {EAttributeType.Atk, GetAttributeDesc(EAttributeType.Atk)},
                    {EAttributeType.Hp, GetAttributeDesc(EAttributeType.Hp)},
                    {EAttributeType.Def, GetAttributeDesc(EAttributeType.Def)},
                    { EAttributeType.AtkSpeed, GetAttributeDesc(EAttributeType.AtkSpeed)}
                };
            }
        }

        private void ShowUI()
        {
            ShowEquipUI();
            ShowTitleIcon(PlayerData.Instance.Title);
            ShowAttributeUI();
            ShowContractSkills(201711);
            LoadPlayerSpine("info_job_1001_1_SkeletonData");
        }

        /// <summary>
        /// 加载主角的spine
        /// </summary>
        /// <param name="spineName"></param>
        private void LoadPlayerSpine(string spineName)
        {
            VM.PlayerSpine.LoadSpine(spineName, "stand", true);
        }

        /// <summary>
        /// 角色称号
        /// </summary>
        /// <param name="title"></param>
        private void ShowTitleIcon(string title)
        {
            VM.TitleIcon.url = title;
        }

        /// <summary>
        /// 锲约的技能
        /// </summary>
        /// <param name="skillName"></param>
        private void ShowContractSkills(int skillId)
        {
            var skillInfo = SkillTableRepo.Get(skillId);
            if (skillInfo == null) return;

            VM.SkillNameLabl.text = skillInfo.Name;
            VM.SkillDescLabl.text = skillInfo.Desc;
            (VM.SkillBtn.GetChild("QualityIcon") as GLoader).url = AddressableDefine.ItemFrame((EQualityLevel)skillInfo.Quality);
            VM.SkillBtn.icon = $"skillicon_{skillInfo.Icon}";
        }

        #region 装备UI
        /// <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);
            }
        }
        #endregion

        #region 角色属性信息
        private void ShowAttributeUI()
        {
            VM.NameLabel.text = PlayerData.Instance.Name;
            ShowPlayerLevel();
            ShowExp(false);
            ShowAtrributeVal(VM.AtkLabel, EAttributeType.Atk);
            ShowAtrributeVal(VM.HpLabel, EAttributeType.Hp);
            ShowAtrributeVal(VM.DefLabel, EAttributeType.Def);
            ShowAtrributeVal(VM.AtkSpeedLabel, EAttributeType.AtkSpeed);
        }
        private void ShowPlayerLevel()
        {
            VM.LvLabl.text = $"LV.{PlayerData.Instance.Level}";
        }

        private void ShowExp(bool bShowAni = true)
        {
            var expInfo = LevelTableRepo.Get(PlayerData.Instance.Level + 1);
            if (expInfo == null)
            {
                // 等级上限
                expInfo = LevelTableRepo.Get(PlayerData.Instance.Level);
            }
            VM.ExpBar.max = expInfo.Exp;
            if (bShowAni)
                VM.ExpBar.TweenValue(PlayerData.Instance.Exp, 0.5f);
            else
                VM.ExpBar.value = PlayerData.Instance.Exp;
        }

        /// <summary>
        /// 角色经验值增加
        /// </summary>
        /// <param name="eventId"></param>
        /// <param name="args"></param>
        private void AddPlayerExp(int eventId, object args)
        {
            ShowExp();
        }

        private string GetAttributeDesc(EAttributeType attrType)
        {
            var attrInfo = AttrDescTableRepo.Get((int)attrType);
            return attrInfo?.ShowName ?? string.Empty;
        }

        /// <summary>
        /// 角色属性变化
        /// </summary>
        /// <param name="eventId"></param>
        /// <param name="args"></param>
        private void OnChangeAttribute(int eventId, object args)
        {
            EAttributeType attrType = (EAttributeType)args;
            if (attrType == EAttributeType.Atk)
            {
                ShowAtrributeVal(VM.AtkLabel, EAttributeType.Atk);
            }
            else if (attrType == EAttributeType.Hp)
            {
                ShowAtrributeVal(VM.HpLabel, EAttributeType.Hp);
            }
            else if (attrType == EAttributeType.Def)
            {
                ShowAtrributeVal(VM.DefLabel, EAttributeType.Def);
            }
            else if (attrType == EAttributeType.AtkSpeed)
            {
                ShowAtrributeVal(VM.AtkSpeedLabel, EAttributeType.AtkSpeed);
            }
        }
        private void ShowAtrributeVal(GTextField attrLabel, EAttributeType attrType)
        {
            attrLabel.text = $"{_attributeDescMap[attrType]}:{PlayerData.Instance.Attr.GetValue(attrType)}";
        }

        #endregion
    }
}