using BingoBrain.Core;
using BingoBrain.HotFix;

namespace BingoBrain
{
    public class SmailUICtrl : BaseUICtrl
    {
        private SmailUI ui;
        private SmailModel model;

        private uint openUIMsg = SkinInfo.SmailUI_Open;
        private uint closeUIMsg = SkinInfo.SmailUI_Close;

        #region 生命周期

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

        protected override void OnDispose()
        {
        }

        public override void OpenUI(object args = null)
        {
            if (ui == null)
            {
                ui = new SmailUI(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(BingoInfo.Update101, OnUpdate101);
            GameDispatcher.Instance.AddListener(BingoInfo.Update101Completed, OnUpdate101Completed);
            GameDispatcher.Instance.AddListener(BingoInfo.Update102, OnUpdate102);
            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.Update101Completed, OnUpdate101Completed);
            GameDispatcher.Instance.AddListener(BingoInfo.Update102, OnUpdate102);
            GameDispatcher.Instance.AddListener(BingoInfo.Update102Completed, OnUpdate102Completed);
        }

        #endregion

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

        private void OnUpdate101Completed(object obj = null)
        {
            if (!PreferencesMgr.Instance.IsShowRewardFly101)
            {
                var value = PreferencesMgr.Instance.Currency101;
                model.show101 = value;
                ui?.Set101();
            }
        }
        
        private void OnUpdate102(object args)
        {
            ui?.OnUpdate102(args);
        }
        
        private void OnUpdate102Completed(object obj = null)
        {
            if (!PreferencesMgr.Instance.IsShowRewardFly102)
            {
                var value = PreferencesMgr.Instance.Currency102;
                model.show102 = value;
                ui?.Set102();
            }
        }
    }
}