using BingoBrain.Core;
using BingoBrain.HotFix;
using DG.Tweening;
using FGUI.ACommon;
using Unity.VisualScripting;
using UnityEngine;

namespace BingoBrain
{
    public class BingoHalldUI : BaseUI
    {
        private BingoHalldUICtrl ctrl;
        private BingoHalldModel model;
        public com_maindi ui;

        private int mTabIndex;

        public BingoHalldUI(BingoHalldUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.BingoHalldUI;
            this.ctrl = ctrl;
        }

        protected override void SetUIInfo(UIInfo uiInfo)
        {
            uiInfo.packageName = "ACommon";
            uiInfo.assetName = "com_maindi";
            uiInfo.layerType = UILayerType.Top;
            uiInfo.isNeedOpenAnim = false;
            uiInfo.isNeedCloseAnim = false;
            uiInfo.isNeedUIMask = false;
        }

        #region 生命周期

        public static BingoHalldUI Instance;
        protected override void OnInit()
        {
            Instance = this;
        }

        protected override void OnClose()
        {
            ui?.FadeOut();
        }

        protected override void OnBind()
        {
            ui = baseUI as com_maindi;
        }

        protected override void OnOpenBefore(object args)
        {
            if (args != null)
            {
                mTabIndex = (int)args;
            }

            InitView();
        }

        protected override void OnOpen(object args)
        {
            ui?.FadeIn();
        }

        #endregion

        #region 消息

        protected override void AddListener()
        {
            //Hall.Instance.AddChangeGiftSwitch(InitView);
            GameDispatcher.Instance.AddListener(BingoInfo.MainTab, OnGameTab);
        }

        protected override void RemoveListener()
        {
            //Hall.Instance.RemoveChangeGiftSwitch(InitView);
            GameDispatcher.Instance.RemoveListener(BingoInfo.MainTab, OnGameTab);
        }

        #endregion

        private void InitView()
        {
            if (GameHelper.IsGiftSwitch()) ui.com_di.gift.selectedIndex = 1;
            // ui.visible = false;
            //OnGameTab();

            ui.com_di.btn_main.SetClickDownEffect(0.8f, 1);
            ui.com_di.btn_tab_redem.SetClickDownEffect(0.8f, 1);
            ui.com_di.btn_hall.SetClickDownEffect(0.8f, 1);
            ui.com_di.btn_shop.SetClickDownEffect(0.8f, 1);
            ui.com_di.btn_fight.SetClickDownEffect(0.8f, 1);
            ui.com_di.btn_shop.SetClick(OnClickMallTab);
            ui.com_di.btn_main.SetClick(OnClickMainTab);
            ui.com_di.btn_tab_redem.SetClick(OnClickRedeemTab);
            ui.com_di.btn_hall.SetClick(OnClickHall);
            ui.com_di.btn_fight.SetClick(OnClickfight);
            if (GameHelper.IsGiftSwitch() && Random.Range(0, 100) < ConfigSystem.GetConfig<CommonModel>().loginhallrate && (PlayerPrefs.GetInt("first_", 0) == 1))
            {
                OnClickHall();
            }
            else
            {
                OnClickMainTab();
            }



        }

        public void OnClickMainTab()
        {
            OnGameTab(0);
        }

        private void OnClickMallTab()
        {
            OnGameTab(1);
        }

        private void OnClickRedeemTab()
        {
            OnGameTab(2);

        }
        public void OnClickHall()
        {
            OnGameTab(3);

        }
        public void OnClickfight()
        {
            OnGameTab(4);

        }
        public void OnGameTab(object obj)
        {

            mTabIndex = (int)obj;
            if (mTabIndex == 0) GameDispatcher.Instance.Dispatch(BingoInfo.showBroadCast);
            else GameDispatcher.Instance.Dispatch(BingoInfo.hideBroadCast); //guangbo
            OnChangeTab(mTabIndex);
            OnClickTab(mTabIndex);
        }

        private void OnChangeTab(int tabIndex)
        {
            ui.com_di.btn_main.cont_select.selectedIndex = tabIndex == 0 ? btn_main.Select_select : btn_main.Select_none;
            ui.com_di.btn_shop.cont_select.selectedIndex = tabIndex == 1 ? btn_h5.Select_select : btn_h5.Select_none;
            ui.com_di.btn_tab_redem.cont_select.selectedIndex = tabIndex == 2 ? btn_todo.Select_select : btn_todo.Select_none;
            ui.com_di.btn_fight.cont_select.selectedIndex = tabIndex == 4 ? btn_todo.Select_select : btn_todo.Select_none;
        }

        private void OnClickTab(int tabIndex)
        {
            if (GameHelper.IsGiftSwitch()) UICtrlDispatcher.Instance.Dispatch(tabIndex == 1 ? SkinInfo.ExchangeUI_Open : SkinInfo.ExchangeUI_Close);
            else UICtrlDispatcher.Instance.Dispatch(tabIndex == 1 ? SkinInfo.JMallUI_Open : SkinInfo.JMallUI_Close);
            UICtrlDispatcher.Instance.Dispatch(tabIndex == 0 ? SkinInfo.BingoHallUI_Open : SkinInfo.BingoHallUI_Close);
            UICtrlDispatcher.Instance.Dispatch(tabIndex == 2 ? SkinInfo.BasptUI_Open : SkinInfo.BasptUI_Close);
            UICtrlDispatcher.Instance.Dispatch(tabIndex == 3 ? SkinInfo.H5UI_Open : SkinInfo.H5UI_Close);
            ui.show_fight.selectedIndex = tabIndex == 4 ? 1 : 0;
            UICtrlDispatcher.Instance.Dispatch(SkinInfo.SmailUI_Open);
            UICtrlDispatcher.Instance.Dispatch(SkinInfo.BottomUI_Open);
            Battle.Instance.SetBg(true);
        }
    }
}