using DG.Tweening;
using FGUI.ACommon;
using BingoBrain.Core;
using BingoBrain.HotFix;

using UnityEngine;
using DontConfuse;
using FairyGUI;

namespace BingoBrain
{
    public class EnterBingoUI : BaseUI
    {
        private EnterBingoUICtrl ctrl;
        private EnterBingoModel model;
        private FGUI.G002_login.com_login ui;

        public EnterBingoUI(EnterBingoUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.EnterBingoUI;
            this.ctrl = ctrl;
        }

        protected override void SetUIInfo(UIInfo uiInfo)
        {
            uiInfo.packageName = "G002_login";
            uiInfo.assetName = "com_login";
            uiInfo.layerType = UILayerType.Normal;
            uiInfo.isNeedCloseAnim = false;
        }

        #region 生命周期

        protected override void OnInit()
        {
        }

        protected override void OnClose()
        {
            UICtrlDispatcher.Instance.Dispatch(SkinInfo.ReadyBingoyUI_Close);
        }

        protected override void OnBind()
        {
            ui = baseUI as FGUI.G002_login.com_login;
        }

        protected override void OnOpenBefore(object args)
        {
            InitView();
        }

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

        #endregion

        #region 消息

        protected override void AddListener()
        {
            CtrlDispatcher.Instance.AddListener(CtrlMsg.Login_ReloginSucceed, OnInitCompleted);
        }

        protected override void RemoveListener()
        {
            CtrlDispatcher.Instance.RemoveListener(CtrlMsg.Login_ReloginSucceed, OnInitCompleted);
        }

        protected override void AddServerListener()
        {
        }

        #endregion

        private void InitView()
        {
            ui.btn_play.SetClick(OnClickBtn, true);
            // ((com_loadingBlacklist)ui.com_blklist1).text_privacy.SetClick(OnClickPrivacy);
            ((com_loadingBlacklist)ui.com_blklist1).onClickLink.Add((content) =>
            {
                switch (content.data)
                {
                    case "link_href":
                        OnClickTerms();
                        break;
                    case "link_href1":
                        OnClickPrivacy();
                        break;
                }
            });
            Pva.HideLoadingUI();
            ui.btn_showmax.SetClick(() =>
            {
                openDebug();
            });

            // ui.btn_debug.SetClick(() =>
            // {
            //     OnclickDebug();
            // });
#if BingoBrainRelease
            ui.btn_showmax.visible = false;
#endif
        }
        int debugTimes = 0;
        private void OnclickDebug()
        {
            debugTimes++;
            GameHelper.ShowTips($"时间已设置为{debugTimes}天后。");
            DateTimeBoardk.Instance.SetTimeOffset(debugTimes * 86400);
        }
        private int numbers = 0;
        void openDebug()
        {
            numbers++;
            if (numbers >= 5)
            {
                MaxSdk.ShowMediationDebugger();
            }
        }
        public bool isFacebookLoginAndGetData;

        private void OnInitCompleted(object obj = null)
        {
            // if (isFacebookLoginAndGetData)
            // {
            //     DOVirtual.DelayedCall(ctrl.netIdleTime, () =>
            //     {
            //         isFacebookLoginAndGetData = false;
            //         PreferencesMgr.Instance.InitPreferences();
            //         PreferencesMgr.Instance.PlayerAvatarId = 0;
            //         uiCtrlDispatcher.Dispatch(SkinInfo.CheckReadyUI_Close);
            //         OnClickBtn();
            //     });
            // }
        }

        public void OnClickBtn()
        {
            // WebviewManager.Instance.SetFullScreen();
            // int flyswitch = ConfigSystem.GetConfig<CommonModel>().flyswitch;
            // int propswitch = ConfigSystem.GetConfig<CommonModel>().propswitch;


            // float top_offset = 170;//fgui中的顶部信息的高度
            // float buttom_offset = 178;
            // if (Screen.safeArea.y != 0)
            // {//刘海屏
            //     top_offset += Screen.safeArea.y;
            // }
            // int offset_y = ConfigSystem.GetConfig<CommonModel>().WVOffset[0];
            // int offset_y1 = ConfigSystem.GetConfig<CommonModel>().WVOffset[1];
            // WebviewManager.Instance.SetPadding(0, top_offset / GRoot.inst.height, 0, buttom_offset / GRoot.inst.height);
            // WebviewManager.Instance.SetOffset(offset_y, offset_y1);


            // WebviewManager.Instance.RefreshUrl();
            // Debug.Log($"flyswitch==1 ------ {flyswitch == 1}");
            // Debug.Log($"propswitch==1 ------ {propswitch == 1}");
            // WebviewManager.Instance.setFlyBtnTag(flyswitch == 1);
            // WebviewManager.Instance.setRewardBtnTag(propswitch == 1);
            // WebviewManager.Instance.SetDarkThough(true);
            // WebviewManager.Instance.ShowH5View(false);
            // WebviewManager.Instance.SetBtn(ConfigSystem.GetConfig<CommonModel>().propCoord[0], ConfigSystem.GetConfig<CommonModel>().propCoord[1], 60, 60);
            GameHelper.PostFunnelLogin("enterButtonClick");
            ctrlDispatcher.Dispatch(CtrlMsg.Game_Start);
            if (GameHelper.IsGiftSwitch()) UICtrlDispatcher.Instance.Dispatch(SkinInfo.PalUI_Open);
            CtrlCloseUI();
        }

        private void OnClickPrivacy()
        {
            // Debug.Log("打开隐私权保护政策界面");
            UICtrlDispatcher.Instance.Dispatch(SkinInfo.JTermUI_Open);
        }

        private void OnClickTerms()
        {
            // Debug.Log("打开隐私权保护政策界面");
            UICtrlDispatcher.Instance.Dispatch(SkinInfo.JTermUI_Open, true);
        }
    }
}