using UnityEngine;

namespace ChillConnect

{
    public class RecordViewUICtrl : BaseUICtrl
    {
        private RecordViewUI ui;
        public RecordViewModel model;

        private uint openUIMsg = UICtrlMsg.RecordViewUI_Open;
        private uint closeUIMsg = UICtrlMsg.RecordViewUI_Close;

        #region 生命周期
        protected override void OnInit()
        {
            model = moduleManager.GetModel(ModelConst.RecordViewModel) as RecordViewModel;
        }

        protected override void OnDispose()
        {
        }

        public override void OpenUI(object args = null)
        {
            if (ui == null)
            {
                ui = new RecordViewUI(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(GameMsg.updateRecordList, RefreshList);

        }
        protected override void RemoveListener()
        {
            uiCtrlDispatcher.RemoveListener(openUIMsg, OpenUI);
            uiCtrlDispatcher.RemoveListener(closeUIMsg, CloseUI);
            GameDispatcher.Instance.RemoveListener(GameMsg.updateRecordList, RefreshList);
        }

        protected override void AddServerListener()
        {

        }
        protected override void RemoveServerListener()
        {

        }

        void RefreshList(object args = null) 
        {
            if (args == null || model.recordList == null) return;

            Debug.Log($" barry refresh args ==   {args}");
            Debug.Log($" barry refresh list ==   {model.recordList.Count}");
            if (model.recordList.Count >= 9)
            {
                model.recordList.RemoveAt(0);
                
            }
            model.recordList.Add(args.ToString());

            ui?.UpdateRecord(); 

        }
        #endregion
    }
}