using SGModule.NetKit;
using UnityEngine;

namespace RedHotRoast
{
    public class HallUICtrl : BaseUICtrl
    {
        private HallUI ui;
        private HallModel model;

        private uint openUIMsg = UICtrlMsg.MainUI_Open;
        private uint closeUIMsg = UICtrlMsg.MainUI_Close;
        private const float _countTime = 5*60f;
        #region 生命周期

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

        protected override void OnDispose()
        {
        }

        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);
            GameDispatcher.Instance.AddListener(GameMsg.RefreshConfig, UpdateRefresh);
            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);
            GameDispatcher.Instance.RemoveListener(GameMsg.RefreshConfig, UpdateRefresh);

            HallManager.Instance.UpdateSecondEvent -= RefreshConfig;

        }

        protected override void AddServerListener()
        {

        }

        protected override void RemoveServerListener()
        {

        }

        #endregion

        private void OnUpdate101(object obj)
        {
            if (obj != null)
            {
                ui?.OnUpdate101(obj);
            }
        }

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

        private void OnUpdate102(object obj)
        {
            if (obj != null)
            {
                ui?.OnUpdate102(obj);
            }
        }

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

                if (DataMgr.MakeupTaskHistory.Value.Count == 1)
                {
                }
            }
        }
        private int _lastUpdateTime;


        public void UpdateRefresh(object obj = null)
        {

        }
        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);
            });
        }
    }
}