using BingoBrain.Core;
using BingoBrain.HotFix;

namespace BingoBrain
{
    public class JTipsUICtrl : BaseUICtrl
    {
        private JTipsUI ui;
        private JTipsModel model;

        private uint openUIMsg = SkinInfo.JTipsUI_Open;
        private uint closeUIMsg = SkinInfo.JTipsUI_Close;

        #region 生命周期

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

        protected override void OnDispose()
        {
        }

        public override void OpenUI(object args = null)
        {
            if (ui == null)
            {
                ui = new JTipsUI(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, AddTips);
            uiCtrlDispatcher.AddListener(closeUIMsg, CheckNext);
        }

        private void CheckNext(object obj)
        {
            var tipsData = obj as TipsData;
            tipsData.Rest();
            CloseUI();
            ShowTips();
        }

        private void ShowTips()
        {
            if (ui != null) return;
            var tipsData = model.GetTips();
            if (tipsData != default)
            {
                OpenUI(tipsData);
            }
        }

        private void AddTips(object obj)
        {
            if (obj is not TipsData tipsData) return;
            model.AddTips(tipsData);
            ShowTips();
        }

        protected override void RemoveListener()
        {
            uiCtrlDispatcher.RemoveListener(openUIMsg, AddTips);
            uiCtrlDispatcher.RemoveListener(closeUIMsg, CheckNext);
        }

        #endregion
    }
}