namespace RedHotRoast
{
    using System;
    using System.Collections.Generic;
    using FGUI.ZM_Tab_19;
    using Newtonsoft.Json;
    using UnityEngine;

    public class MainTabUI : BaseUI
    {
        private MainTabUICtrl ctrl;
        private MainTabModel model;
        private FGUI.ZM_Tab_19.com_main_tab ui;

        private Action closeCallBack;

        private static int mTabIndex = 1;

        public static int mLastIndex = 1;

        private bool exitH5Task;



        public MainTabUI(MainTabUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.MainTabUI;
            this.ctrl = ctrl;
        }

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

        #region 生命周期

        protected override void OnInit()
        {
        }

        protected override void OnClose()
        {
            CommonHelper.FadeOut(ui);
            mLastIndex = 1;
            mTabIndex = 1;
            CloseAllTabView();
        }

        protected override void OnBind()
        {
            ui = baseUI as FGUI.ZM_Tab_19.com_main_tab;
        }

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

            CheckH5Task();
            InitView();
        }

        private void CloseAllTabView()
        {
            uiCtrlDispatcher.Dispatch(UICtrlMsg.MenuUI_Close);
            uiCtrlDispatcher.Dispatch(UICtrlMsg.MainUI_Close);
            uiCtrlDispatcher.Dispatch(UICtrlMsg.MailUI_Close);
            uiCtrlDispatcher.Dispatch(UICtrlMsg.SignInUI_Close);
            
        }

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

        #endregion

        #region 消息

        protected override void AddListener()
        {
            // GameDispatcher.Instance.AddListener(GameMsg.MainTab, OnGameTab);
        }

        protected override void RemoveListener()
        {
            // GameDispatcher.Instance.RemoveListener(GameMsg.MainTab, OnGameTab);
        }

        #endregion

        private void CheckH5Task(object obj = null)
        {
            exitH5Task = false;
        }

        private void InitView()
        {

            ui.com_tab.btn_signin.SetClickDownEffect(0.8f, 1);
            ui.com_tab.btn_main.SetClickDownEffect(0.8f, 1);
            ui.com_tab.btn_mail.SetClickDownEffect(0.8f, 1);
            ui.com_tab.btn_options.SetClickDownEffect(0.8f, 1);

            ui.com_tab.btn_signin.SetClick(OnClickSignInTab);
            ui.com_tab.btn_main.SetClick(OnClickMainTab);
            ui.com_tab.btn_mail.SetClick(OnClickMailTab);
            ui.com_tab.btn_options.SetClick(OnClickSettingTab);

            OnGameTab(mTabIndex);
        }

        private void OnClickSignInTab()
        {
            OnGameTab(1);
        }
        private void OnClickMainTab()
        {
            OnGameTab(2);
        }

        private void OnClickMailTab()
        {
            OnGameTab(3);
        }

        private void OnClickSettingTab()
        {
            OnGameTab(4);
        }

        private void OnGameTab(object obj = null)
        {

            if (obj != null)
            {
                mTabIndex = (int)obj;
            }

            OnChangeTab(mTabIndex);
            OnClickTab(mTabIndex);
        }

        private void OnChangeTab(int tabIndex)
        {
            // ui.com_tab.cont_show.selectedIndex = tabIndex == 0 ? com_bottom.Show_mian : com_bottom.Show_show;
            ui.com_tab.btn_signin.cont_tab.selectedIndex = tabIndex == 1 ? btn_sign.Tab_select : btn_sign.Tab_normal;
            ui.com_tab.btn_main.cont_tab.selectedIndex = tabIndex == 2 ? btn_main.Tab_select : btn_main.Tab_normal;
            ui.com_tab.btn_mail.cont_tab.selectedIndex = tabIndex == 3 ? btn_mail.Tab_select : btn_mail.Tab_normal;
            ui.com_tab.btn_options.cont_tab.selectedIndex = tabIndex == 4 ? btn_options.Tab_select : btn_options.Tab_normal;


            // ui.com_tab.btn_sign.cont_tab.selectedIndex = tabIndex == 2 ? btn_todos.Tab_select : btn_todos.Tab_normal;
            // // ui.com_tab.btn_spin.cont_tab.selectedIndex = tabIndex == 1 ? btn_todos.Tab_select : btn_todos.Tab_normal;
            // ui.com_tab.btn_setting.cont_tab.selectedIndex = tabIndex == 5 ? btn_todos.Tab_select : btn_todos.Tab_normal;

        }


        public static void SwapElements<T>(List<T> list, int index1, int index2)
        {
            if (index1 >= 0 && index1 < list.Count && index2 >= 0 && index2 < list.Count)
            {
                (list[index2], list[index1]) = (list[index1], list[index2]);
            }
            else
            {
                throw new ArgumentOutOfRangeException("One or both of the provided indices are out of range.");
            }
        }


        private void OnClickTab(int tabIndex)
        {
            if (mLastIndex == tabIndex)
            {
                return;
            }
            var isH5Tab = tabIndex == 5;
            if (mLastIndex == 1)
            {
                uiCtrlDispatcher.Dispatch(UICtrlMsg.SignInUI_Close, false);
                uiCtrlDispatcher.Dispatch(tabIndex == 2 ? UICtrlMsg.MainUI_Open : UICtrlMsg.MainUI_Close, tabIndex == 2 ? true : false);
                uiCtrlDispatcher.Dispatch(tabIndex == 3 ? UICtrlMsg.MailUI_Open : UICtrlMsg.MailUI_Close, tabIndex == 3 ? true : false);
                uiCtrlDispatcher.Dispatch(tabIndex == 4 ? UICtrlMsg.MenuUI_Open : UICtrlMsg.MenuUI_Close, tabIndex == 4 ? true : false);


            }
            else if (mLastIndex == 2)
            {
                uiCtrlDispatcher.Dispatch(UICtrlMsg.MainUI_Close, tabIndex < 2 ? true : false);
                uiCtrlDispatcher.Dispatch(tabIndex == 1 ? UICtrlMsg.SignInUI_Open : UICtrlMsg.SignInUI_Close, false);
                uiCtrlDispatcher.Dispatch(tabIndex == 3 ? UICtrlMsg.MailUI_Open : UICtrlMsg.MailUI_Close, tabIndex == 3 ? true : false);
                uiCtrlDispatcher.Dispatch(tabIndex == 4 ? UICtrlMsg.MenuUI_Open : UICtrlMsg.MenuUI_Close, tabIndex == 4 ? true : false);
            }
            else if (mLastIndex == 3)
            {
                uiCtrlDispatcher.Dispatch(UICtrlMsg.MailUI_Close, tabIndex < 3 ? true : false);
                uiCtrlDispatcher.Dispatch(tabIndex == 1 ? UICtrlMsg.SignInUI_Open : UICtrlMsg.SignInUI_Close, false);
                uiCtrlDispatcher.Dispatch(tabIndex == 2 ? UICtrlMsg.MainUI_Open : UICtrlMsg.MainUI_Close, tabIndex == 2 ? false : true);
                uiCtrlDispatcher.Dispatch(tabIndex == 4 ? UICtrlMsg.MenuUI_Open : UICtrlMsg.MenuUI_Close, tabIndex == 4 ? true : false);
            }
            else if (mLastIndex == 4)
            {
                uiCtrlDispatcher.Dispatch(UICtrlMsg.MenuUI_Close, true);
                uiCtrlDispatcher.Dispatch(tabIndex == 1 ? UICtrlMsg.SignInUI_Open : UICtrlMsg.SignInUI_Close, false);
                uiCtrlDispatcher.Dispatch(tabIndex == 2 ? UICtrlMsg.MainUI_Open : UICtrlMsg.MainUI_Close, tabIndex == 2 ? false : true);
                uiCtrlDispatcher.Dispatch(tabIndex == 3 ? UICtrlMsg.MailUI_Open : UICtrlMsg.MailUI_Close, tabIndex == 3 ? false : true);

            }

            // uiCtrlDispatcher.Dispatch(tabIndex == 2 ? UICtrlMsg.MainUI_Open : UICtrlMsg.MainUI_Close, tabIndex < 2 ? true : false);

            // uiCtrlDispatcher.Dispatch(isH5Tab ? UICtrlMsg.BackgroundUI_Close : UICtrlMsg.BackgroundUI_Open);

            mLastIndex = tabIndex;


            if (isH5Tab)
            {
                // AudioManager.Instance.StopBGM();
            }
            else
            {
                //背景音乐
                AudioManager.Instance.PlayBGM("hall_bgm");
            }

            if (DataMgr.IsLastH5Tab.Value != isH5Tab)
            {
                DataMgr.IsLastH5Tab.Value = isH5Tab;
            }


        }
    }
}