using BingoBrain.Core;
using BingoBrain.HotFix;

namespace BingoBrain
{
    public class CallEndUICtrl : BaseUICtrl
    {
        private CallEndUI ui;
        private CallEndModel model;

        private uint openUIMsg = SkinInfo.CallEndUI_Open;
        private uint closeUIMsg = SkinInfo.CallEndUI_Close;

        #region 生命周期

        protected override void OnInit()
        {
        }

        protected override void OnDispose()
        {
        }

        public override void OpenUI(object args = null)
        {
            if (ui == null)
            {
                ui = new CallEndUI(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()
        {
            gameDispatcher.AddListener(BingoInfo.CallNumFinish, CheckEnd);
            uiCtrlDispatcher.AddListener(closeUIMsg, CloseUI);
            gameDispatcher.AddListener(BingoInfo.EndBingoGame, CloseUI);
        }

        private void CheckEnd(object obj)
        {
            if (BingoCell.AddCallSum >= BingoCell.MaxAddCallSum)
            {
                gameDispatcher.Dispatch(BingoInfo.ShowGameOver);
            }
            else
            {
                GameHelper.GetRewardExtra(111, 1, FGUI.JRewardPop.com_extraItem.Extra_extraball, isSuccess =>
                {
                    if (!isSuccess)
                    {
                        gameDispatcher.Dispatch(BingoInfo.ShowGameOver);
                    }
                });
            }
        }

        protected override void RemoveListener()
        {
            gameDispatcher.RemoveListener(BingoInfo.CallNumFinish, CheckEnd);
            uiCtrlDispatcher.RemoveListener(closeUIMsg, CloseUI);
            gameDispatcher.RemoveListener(BingoInfo.EndBingoGame, CloseUI);
        }

        #endregion
    }
}