namespace ChillConnect
{
    public class LoadingUICtrl : BaseUICtrl
    {
        private LoadingUI ui;
        private LoadingModel model;

        private uint openUIMsg = 0;
        private uint closeUIMsg = 0;

        #region 生命周期

        protected override void OnInit()
        {
        }

        protected override void OnDispose()
        {
        }

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

        public override void CloseUI(object args = null)
        {
            if (ui is { isClose: false })
            {
                ui.Close();
            }

            ui = null;
        }

        #endregion

        public override uint GetOpenUIMsg(string uiName)
        {
            return openUIMsg;
        }

        public override uint GetCloseUIMsg(string uiName)
        {
            return closeUIMsg;
        }

        protected override void AddListener()
        {
            AppDispatcher.Instance.AddListener(AppMsg.UI_DisplayLoadingUI, OpenUI);
            AppDispatcher.Instance.AddListener(AppMsg.UI_HideLoadingUI, OnHideLoading);
        }

        protected override void RemoveListener()
        {
            AppDispatcher.Instance.RemoveListener(AppMsg.UI_DisplayLoadingUI, OpenUI);
            AppDispatcher.Instance.RemoveListener(AppMsg.UI_HideLoadingUI, OnHideLoading);
        }

        private void OnHideLoading(object obj)
        {
            if (ui == null) return;
            ui.CheckAgree();
        }
    }
}