namespace ScrewsMaster
{
    public class H5UICtrl : BaseUICtrl
    {
        private readonly uint closeUIMsg ;
        private H5Model model;

        private readonly uint openUIMsg ;
        private H5UI ui;

        private void OnUpdateDollar(object obj)
        {
            if (obj == null) return;
            var changeValue = (decimal)obj;
            if (model != null)
            {
                model.show102 += changeValue;
                ui?.SetCash(model.show102.ToString("N"));
            }
        }

        private void OnUpdateCompletedDollar(object obj = null)
        {
            if (PreferencesMgr.Instance.IsShowRewardFly101) return;
            var value = GameHelper.Get102();
            if (model != null) model.show102 = value;

            ui?.SetCash();
        }

        private void OnDollarChange(object data)
        {
        }

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

        private void OnMakeupSuccess(object obj)
        {
        }

        private void OnWebview_PageState(object args)
        {
        }

        #region 生命周期

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

        protected override void OnDispose()
        {
        }

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

        public override void CloseUI(object args = null)
        {
            if (ui is { isClose: false }) 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.AddListener(GameMsg.H5ViewClickBtn, OnH5ClickBtn);
            GameDispatcher.Instance.AddListener(GameMsg.Update101, OnUpdateDollar);
            GameDispatcher.Instance.AddListener(GameMsg.Update102Completed, OnUpdateCompletedDollar);
            gameDispatcher.AddListener(GameMsg.MakeupSuccess, OnMakeupSuccess);

            CtrlDispatcher.Instance.AddListener(CtrlMsg.Webview_PageState, OnWebview_PageState);
            CtrlDispatcher.Instance.AddListener(CtrlMsg.Webview_PageFinish, OnWebviewFinish);
        }

        protected override void RemoveListener()
        {
            uiCtrlDispatcher.RemoveListener(openUIMsg, OpenUI);
            uiCtrlDispatcher.RemoveListener(closeUIMsg, CloseUI);
            gameDispatcher.RemoveListener(GameMsg.H5ViewClickBtn, OnH5ClickBtn);
            GameDispatcher.Instance.RemoveListener(GameMsg.Update101, OnUpdateDollar);
            GameDispatcher.Instance.RemoveListener(GameMsg.Update102Completed, OnUpdateCompletedDollar);
            gameDispatcher.RemoveListener(GameMsg.MakeupSuccess, OnMakeupSuccess);

            CtrlDispatcher.Instance.RemoveListener(CtrlMsg.Webview_PageState, OnWebview_PageState);
            CtrlDispatcher.Instance.RemoveListener(CtrlMsg.Webview_PageFinish, OnWebviewFinish);
        }
        
        
        void OnH5ClickBtn(object arg)
        {
            ui?.ClickBtn((string)arg);
        }

        protected override void AddServerListener()
        {
        }

        protected override void RemoveServerListener()
        {
        }

        #endregion
    }
}