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

using FairyGUI;
using XGame.Framework;
using XGame.Framework.UI;

namespace FL.FGUI
{
    /// <summary>
    /// UI逻辑处理类
    /// </summary>
    /// <typeparam name=""></typeparam>
    public partial class MainHomeBottomPanelCtrl : UIController<MainHomeBottomPanelVM>
    {
        private UIKey _tempKey;
        protected override void OnEnable(object intent)
        {
            //AddUIListenres();
            VM.BtnCtrl.onChanged.Add(OnBtnCtrlChanged);
            FrameworkEvent.Instance.AddListener(XGame.Framework.EventDefine.UI_OPENED, OnUIOpened);
            FrameworkEvent.Instance.AddListener(XGame.Framework.EventDefine.UI_CLOSED, OnUIClosed);
        }
        protected override void OnDisable()
        {
            //RemoveUIListenres();
            VM.BtnCtrl.onChanged.Remove(OnBtnCtrlChanged);
            FrameworkEvent.Instance.RemoveListener(XGame.Framework.EventDefine.UI_OPENED, OnUIOpened);
            FrameworkEvent.Instance.RemoveListener(XGame.Framework.EventDefine.UI_CLOSED, OnUIClosed);
            _tempKey = null;
        }
        #region UI事件
        //private void AddUIListenres()
        //{
        //    VM.RoleBtn.onClick.Add(OnClickRoleBtn);
        //    VM.FubenBtn.onClick.Add(OnClickFubenBtn);
        //    VM.ShopBtn.onClick.Add(OnClickShopBtn);
        //    VM.HomeBtn.onClick.Add(OnClickHomeBtn);
        //    VM.GonghuiBtn.onClick.Add(OnClickGonghuiBtn);
        //    VM.JianlingBtn.onClick.Add(OnClickJianlingBtn);

        //}
        //private void RemoveUIListenres()
        //{
        //    VM.RoleBtn.onClick.Remove(OnClickRoleBtn);
        //    VM.FubenBtn.onClick.Remove(OnClickFubenBtn);
        //    VM.ShopBtn.onClick.Remove(OnClickShopBtn);
        //    VM.HomeBtn.onClick.Remove(OnClickHomeBtn);
        //    VM.GonghuiBtn.onClick.Remove(OnClickGonghuiBtn);
        //    VM.JianlingBtn.onClick.Remove(OnClickJianlingBtn);

        //}
        //private void OnClickRoleBtn(EventContext context)
        //{
        //    XGame.Log.Debug($"OnClickRoleBtn");
        //}
        //private void OnClickFubenBtn(EventContext context)
        //{
        //    XGame.Log.Debug($"OnClickFubenBtn");
        //}
        //private void OnClickShopBtn(EventContext context)
        //{
        //    XGame.Log.Debug($"OnClickShopBtn");
        //    Context.UI.OpenAsync(UIKeys.BattleMapBattleMapTest);
        //}
        //private void OnClickHomeBtn(EventContext context)
        //{
        //    XGame.Log.Debug($"OnClickHomeBtn");
        //}
        //private void OnClickGonghuiBtn(EventContext context)
        //{
        //    XGame.Log.Debug($"OnClickGonghuiBtn");
        //}
        //private void OnClickJianlingBtn(EventContext context)
        //{
        //    XGame.Log.Debug($"OnClickJianlingBtn");
        //}

        #endregion

        #region UI开启/关闭监听事件
        private UIKey IndexToKey(int index)
        {
            return index switch
            {
                1 => UIKeys.PlayerMainPanel,
                2 => UIKeys.PartnerMainPanel,
                6 => UIKeys.DragonEggBox,
                _ => null,
            };
        }
        private int KeyToIndex(UIKey uiKey)
        {
            if (uiKey == UIKeys.PlayerMainPanel) return 1;
            if (uiKey == UIKeys.PartnerMainPanel) return 2;
            if (uiKey == UIKeys.DragonEggBox) return 6;
            return -1;
        }
        private void OnBtnCtrlChanged(EventContext context)
        {
            XGame.Log.Debug($"OnBtnCtrlChanged. previousIndex:{VM.BtnCtrl.previousIndex} selectedIndex:{VM.BtnCtrl.selectedIndex}");
            var lastUIKey = IndexToKey(VM.BtnCtrl.previousIndex);
            if (lastUIKey != null)
            {
                _tempKey = lastUIKey;
                Context.UI.Close(lastUIKey);
            }
            _tempKey = null;
            var nextUIKey = IndexToKey(VM.BtnCtrl.selectedIndex);
            if (nextUIKey != null)
            {
                _tempKey = nextUIKey;
                var async = Context.UI.OpenAsync(nextUIKey);
                async.On(_ =>
                {
                    _tempKey = null;
                });
            }
        }
        private void OnUIOpened(int eventId, object args)
        {
            var uiKey = args as UIKey;
            XGame.Log.Debug($"收到UI开启事件 UIKey:{uiKey} selectedIndex:{VM.BtnCtrl.selectedIndex}");
            if (uiKey == _tempKey) return; // 避免重复监听
            var index = KeyToIndex(uiKey);
            if (index != -1)
            { // 是控制器关联的UI
                if (index != VM.BtnCtrl.selectedIndex)
                { // 控制器索引不一致
                    VM.BtnCtrl.selectedIndex = index;
                }
            }
        }
        private void OnUIClosed(int eventId, object args)
        {
            var uiKey = args as UIKey;
            XGame.Log.Debug($"收到UI关闭事件 UIKey:{uiKey} selectedIndex:{VM.BtnCtrl.selectedIndex}");
            if (uiKey == _tempKey) return; // 避免重复监听
            var index = KeyToIndex(uiKey);
            if (index != -1)
            { // 是控制器关联的UI
                if (index == VM.BtnCtrl.selectedIndex)
                { // 控制器索引是当前关闭的UI
                    VM.BtnCtrl.selectedIndex = 0;
                }
            }
        }
        #endregion
    }
}