using BingoBrain.Core;
using BingoBrain.HotFix;

namespace BingoBrain
{
    public class BingoStartUICtrl : BaseUICtrl
    {
        private BingoStartUI ui;
        private BingoStartModel model;

        private uint openUIMsg = SkinInfo.BingoStartUI_Open;
        private uint closeUIMsg = SkinInfo.BingoStartUI_Close;

        #region 生命周期

        protected override void OnInit()
        {
            model = ModuleBoardk.GetModel(ModelConst.BingoStartModel) as BingoStartModel;
        }

        protected override void OnDispose()
        {
        }

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

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

        #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(BingoInfo.Update101, OnUpdate101);
            GameDispatcher.Instance.AddListener(BingoInfo.Update102, OnUpdate102);
            GameDispatcher.Instance.AddListener(BingoInfo.Update101Completed, OnUpdate101Completed);
            GameDispatcher.Instance.AddListener(BingoInfo.Update102Completed, OnUpdate102Completed);
        }

        protected override void RemoveListener()
        {
            uiCtrlDispatcher.RemoveListener(openUIMsg, OpenUI);
            uiCtrlDispatcher.RemoveListener(closeUIMsg, CloseUI);
            GameDispatcher.Instance.RemoveListener(BingoInfo.Update101, OnUpdate101);
            GameDispatcher.Instance.RemoveListener(BingoInfo.Update102, OnUpdate102);
            GameDispatcher.Instance.RemoveListener(BingoInfo.Update101Completed, OnUpdate101Completed);
            GameDispatcher.Instance.RemoveListener(BingoInfo.Update102Completed, OnUpdate102Completed);
        }

        #endregion

        private void OnUpdate101(object obj)
        {
            if (obj == null) return;
            var changeValue = (decimal)obj;
            if (model != null)
            {
                model.show101 += changeValue;
                ui?.Set101(model.show101.ToString("N0"));
            }
        }

        private void OnUpdate101Completed(object obj)
        {
            if (PreferencesMgr.Instance.IsShowRewardFly101) return;
            var value = PreferencesMgr.Instance.Currency101;
            if (model != null)
            {
                model.show101 = value;
            }

            ui?.Set101();
        }

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

        private void OnUpdate102Completed(object obj)
        {
            if (PreferencesMgr.Instance.IsShowRewardFly102) return;
            var value = PreferencesMgr.Instance.Currency102;
            if (model != null)
            {
                model.show102 = value;
            }

            ui?.Set102();
        }
    }
}