using IgnoreOPS;
using SGModule.Net;
using SGModule.NetKit;
using UnityEngine;

namespace ChillConnect
{
    public class HallUICtrl : BaseUICtrl
    {
        private HallUI ui;
        private HallModel model;
#if GAME_RELEASE
            private readonly float _countTime = 5 * 60.0f;
#else
        private readonly float _countTime = 1 * 60.0f;
#endif
        private uint openUIMsg = UICtrlMsg.MainUI_Open;
        private uint closeUIMsg = UICtrlMsg.MainUI_Close;

        #region 生命周期

        protected override void OnInit()
        {
            model = moduleManager.GetModel(ModelConst.HallModel) as HallModel;
        }

        protected override void OnDispose()
        {
            HallManager.Instance.UpdateSecondEvent -= RefreshConfig;
        }

        public override void OpenUI(object args = null)
        {
            if (ui == null)
            {
                ui = new HallUI(this);
                ui.Open(args);
            }
        }

        public override void CloseUI(object args = null)
        {
            if (ui != null && !ui.isClose)
            {
                ui.Close();
            }

            ui = null;
        }

        #endregion

        #region 消息

        public override uint GetOpenUIMsg(string uiName)
        {
            return openUIMsg;
        }

        public override uint GetCloseUIMsg(string uiName)
        {
            return closeUIMsg;
        }

        protected override void AddListener()
        {
            uiCtrlDispatcher.AddListener(openUIMsg, OpenUI);
            uiCtrlDispatcher.AddListener(closeUIMsg, CloseUI);
            GameDispatcher.Instance.AddListener(GameMsg.Update101, OnUpdate101);
            GameDispatcher.Instance.AddListener(GameMsg.Update101Completed, OnUpdate101Completed);
            GameDispatcher.Instance.AddListener(GameMsg.Update102, OnUpdate102);
            GameDispatcher.Instance.AddListener(GameMsg.Update102Completed, OnUpdate102Completed);
            HallManager.Instance.UpdateSecondEvent += RefreshConfig;

        }

        protected override void RemoveListener()
        {
            uiCtrlDispatcher.RemoveListener(openUIMsg, OpenUI);
            uiCtrlDispatcher.RemoveListener(closeUIMsg, CloseUI);
            GameDispatcher.Instance.RemoveListener(GameMsg.Update101, OnUpdate101);
            GameDispatcher.Instance.RemoveListener(GameMsg.Update102, OnUpdate102);
            GameDispatcher.Instance.RemoveListener(GameMsg.Update101Completed, OnUpdate101Completed);
            GameDispatcher.Instance.RemoveListener(GameMsg.Update102Completed, OnUpdate102Completed);
            HallManager.Instance.UpdateSecondEvent -= RefreshConfig;



        }

        protected override void AddServerListener()
        {

        }

        protected override void RemoveServerListener()
        {

        }

        #endregion

        private void OnUpdate101(object obj)
        {
            ui?.OnUpdate101(obj);
        }

        private void OnUpdate101Completed(object obj = null)
        {
            // if (!DataMgr.IsShowRewardFly101)
            // {
            //     var value = DataMgr.Coin.Value;
            //     model.showCoin = value;
            //     ui?.Set101();

            //     // var cardRedeemNewVOModel = ConfigSystem.GetConfig<CardRedeemNewModel>();
            //     // if (DataMgr.CoinMakeupTaskHistory.Count == 1)
            //     // {
            //     //     var vo = cardRedeemNewVOModel.DataList.First();
            //     // }
            // }
        }

        private void OnUpdate102(object obj)
        {
            ui?.OnUpdate102(obj);
        }

        private void OnUpdate102Completed(object obj = null)
        {
            // if (!DataMgr.IsShowRewardFly102)
            // {
            //     var value = DataMgr.Ticket.Value;
            //     model.showDollar = value;

            //     if (DataMgr.MakeupTaskHistory.Value.Count == 1)
            //     {



            //     }
            // }
        }

        private int _lastUpdateTime;

        private void RefreshConfig()
        {
            _lastUpdateTime++;
            // Debug.Log($"barry RefreshConfig======= {_lastUpdateTime}");
            if (_lastUpdateTime <= _countTime) return;
            _lastUpdateTime = 0;
            NetKit.Instance.Post<LoginModel>("settingRefresh", null, response =>
            {
                var savedCfgName = PlayerPrefs.GetString("configFileName");
                if (!response.IsSuccess) return;

                if (savedCfgName.Equals(response.Data.Setting)) return;
                // Debug.Log($"barry===== setting==== {obj.setting}" );
                var loginData = GameHelper.GetLoginModel();
                loginData.Setting = response.Data.Setting;

                NetworkDispatcher.Instance.Dispatch(NetworkMsg.GetConfig, 2);
            });
        }
    }
}
