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

using FairyGUI;
using FL.Battle.Buffs;
using XGame.Database;
using XGame.Framework.UI;

namespace FL.FGUI
{
    /// <summary>
    /// UI逻辑处理类
    /// </summary>
    /// <typeparam name=""></typeparam>
    public partial class MainHomeBuffInfoNestedCtrl : UIController<MainHomeBuffInfoNestedVM>
    {
        protected override void OnEnable(object intent)
        {
            AddUIListenres();
            EventSingle.Instance.AddListener(EventDefine.GameMainMapPreviewBuff, OnSinglePreviewBuff);
            EventSingle.Instance.AddListener(EventDefine.GameMainMapPushPreviewBuffs, OnPreviewBuffs);
        }
        protected override void OnDisable()
        {
            RemoveUIListenres();
            EventSingle.Instance.RemoveListener(EventDefine.GameMainMapPreviewBuff, OnSinglePreviewBuff);
            EventSingle.Instance.RemoveListener(EventDefine.GameMainMapPushPreviewBuffs, OnPreviewBuffs);
        }
        #region UI事件
        private void AddUIListenres()
        {
            VM.BuffShowBtn.onClick.Add(OnClickBuffShowBtn);

        }
        private void RemoveUIListenres()
        {
            VM.BuffShowBtn.onClick.Remove(OnClickBuffShowBtn);

        }
        private void OnClickBuffShowBtn(EventContext context)
        {
            EventSingle.Instance.Notify(EventDefine.GameMainMapReqPreviewBuffs);
        }

        #endregion


        private void OnSinglePreviewBuff(int eventId, object args)
        {
            var dto = (BuffPreviewDto)args;
            var isShow = dto.buffTableId > 0;
            VM.ShowInfoCtrl.selectedIndex = isShow ? 1 : 0;
            if (isShow)
            {
                var buffTable = BuffTableRepo.Get(dto.buffTableId);
                VM.BuffIcon.url = buffTable.Icon;
                VM.BuffLayerPar.max = dto.layerLimit;
                VM.BuffLayerPar.value = dto.layer;
                string triggerName;
                EQualityLevel quality;
                if (dto.isSkill)
                {
                    var skill = SkillTableRepo.Get(dto.skillId);
                    if (skill.BuffTie.Length == 2)
                    {
                        var triBuff = BuffTableRepo.Get(skill.BuffTie[1]);
                        triggerName = triBuff.Name;
                    }
                    else
                    {
                        triggerName = skill.Name;
                    }
                    quality = (EQualityLevel)skill.Quality;
                }
                else
                {
                    var epigraph = EpigraphTableRepo.Get(dto.skillId);
                    triggerName = epigraph.Name;
                    quality = epigraph.Quality;
                }
                VM.TriggerNameLabel.text = string.Format(StringDefine.common_bracket, triggerName);
                VM.TriggerNameLabel.color = quality.ToEquipNameColor();
                VM.TipsLabel.text = dto.layer == dto.layerLimit ? StringDefine.buff_preview_tips_attack : StringDefine.buff_preview_tips_coming;
            }
        }
        private void OnPreviewBuffs(int eventId, object args)
        {
            Context.UI.OpenAsync(UIKeys.MainBuffPreviewPanel, args);
        }
    }
}