using System;
using BingoBrain.Core;
using UnityEngine;
using BingoBrain.HotFix;
using DG.Tweening;
using DontConfuse;

namespace BingoBrain
{
    public class Hall : BaseUnity<Hall>
    {
        public event Action UpdateSecondEvent;
        public event Action UpdateEvent;
        private float _secondTime;
        //  private LoginModel loginModel;
        private bool isGameStart;
        private bool isInH5;
        private bool isGaming = false;
        private BingoDataSystem _bingoDataSys;
        private TomorrowSystem _tomorrowSys;
        private UsaSystem _usaSys;
        private TuSystem _tuSys;
        private DialogSystem _dialogSys;
        private BingoSystem bingoSystem;
        public override void Init()
        {
            InitSystem();
            AddListener();
        }

        private void AddListener()
        {
            CtrlDispatcher.Instance.AddPriorityListener(CtrlMsg.Game_Start, OnGameStart);
            GameDispatcher.Instance.AddListener(BingoInfo.OpenGame, EnterGame);
            GameDispatcher.Instance.AddListener(BingoInfo.EnterMain, EnterHall);
            AppDispatcher.Instance.AddListener(GinInfoC.App_Focus_True, FocusToGame);

        }

        public void AddChangeGiftSwitch(Action action)
        {
            _bingoDataSys?.AddChangeGiftSwitch(action);
        }

        public void RemoveChangeGiftSwitch(Action action)
        {
            _bingoDataSys?.RemoveChangeGiftSwitch(action);
        }

        void FocusToGame(object obj = null)
        {
            // if(MaxPayManager.isPay) 
            // {
            //     MaxPayManager.isPay = false;
            //     UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.PayloadingUI_Close);
            //     MaxPayManager.Instance.PaySuccess();
            // }

            var last_time = PlayerPrefs.GetInt("Dayreftimes", 0);
            DateTime newDate = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            newDate = newDate.AddSeconds(GameHelper.GetNowTime(true));
            // Debug.Log($"barry newdate==== {newDate}");
            var newDays = newDate.Day;
            if (last_time != newDays)
            {
                var configs = GameHelper.GetCommonModel();
                // Debug.Log($"barry  hall-----{configs != null}");
                if (configs != null)
                {
                    WebviewManager.Instance.RefreshUrl();
                }
            }
        }

        private void InitSystem()
        {
            //添加游戏数据系统模块
            _bingoDataSys = new BingoDataSystem();
            // 添加新的一天模块
            _tomorrowSys = new TomorrowSystem();
            // 添加奖励系统模块
            _usaSys = new UsaSystem();
            // 添加消费系统模块
            _tuSys = new TuSystem();
            // 添加弹窗系统模块
            _dialogSys = new DialogSystem();
        }

        /// <summary>
        /// 游戏开始
        /// </summary>
        /// <param name="obj"></param>
        private void OnGameStart(object obj)
        {
            GameDispatcher.Instance.Dispatch(BingoInfo.EnterMain);
            isGameStart = true;

            GameHelper.PlayGameTimeEvent(1);
        }

        private void EnterHall(object obj = null)
        {
            // if (!GameHelper.IsGiftSwitch())
            // {
            UICtrlDispatcher.Instance.Dispatch(SkinInfo.PlayBgUI_Close);
            UICtrlDispatcher.Instance.Dispatch(SkinInfo.BingoStartUI_Close);
            UICtrlDispatcher.Instance.Dispatch(SkinInfo.BottomUI_Open);
            UICtrlDispatcher.Instance.Dispatch(SkinInfo.BingoHallUI_Open);
            UICtrlDispatcher.Instance.Dispatch(SkinInfo.BingoHalldUI_Open);
            // }
            // else
            // {
            //     UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.SheepPlayUI_Open);

            // }

            GameHelper.PostFunnelLogin("enterHall");
            Audio.Instance.StopBGM();

            Audio.Instance.PlayBGM(DoConst.BGMMainScene);

            if (GameHelper.IsGiftSwitch())
            {
                UICtrlDispatcher.Instance.Dispatch(SkinInfo.BroadcastUI_Open);
                if (PlayerPrefs.GetInt("first_uid", 0) != GameHelper.GetLoginModel().uid)
                {
                    //  UICtrlDispatcher.Instance.Dispatch(SkinInfo.FirstRewardUI_Open);

                    GameHelper.AddGoldNumber(50);
                    PlayerPrefs.SetInt("first_uid", (int)GameHelper.GetLoginModel().uid);
                    PreferencesMgr.Instance.Currency102 = PreferencesMgr.Instance.Currency102 + ConfigSystem.GetConfig<CommonModel>().InitialNum;
                }
            }

        }

        /// <summary>
        /// 进入玩法，隐藏主界面
        /// </summary>
        /// <param name="obj"></param>
        private void EnterGame(object obj)
        {

            UICtrlDispatcher.Instance.Dispatch(SkinInfo.PlayBgUI_Open);
            UICtrlDispatcher.Instance.Dispatch(SkinInfo.BingoHallUI_Close);
            UICtrlDispatcher.Instance.Dispatch(SkinInfo.BingoHalldUI_Close);
            Audio.Instance.StopBGM();
        }

        public void Update()
        {
            //if (!isGameStart) return;

            UpdateEvent?.Invoke();

            _secondTime += Time.deltaTime;
            if (_secondTime >= 1)
            {
                _secondTime = 0;
                UpdateSecondEvent?.Invoke();
            }
        }

        void OnApplicationQuit()
        {
            PreferencesMgr.Instance.ImmediateSendSave();
        }

        public void SetGaming(bool isGaming)
        {
            this.isGaming = isGaming;
            if (!isGaming)
            {
                Audio.Instance.PlayBGM(DoConst.BGMMainScene);
            }
        }

        public bool IsGaming()
        {
            return isGaming;
        }
    }
}