using System.Collections.Generic;
using ChillConnect;
using SGModule.NetKit;

namespace Assets.Scripts {
    public class PreferencesDataReadyCtrl : BaseCtrl {
        private readonly List<uint> msgList = new();


        public void PreferenceDataReady(object objs = null) {
            InspectionNewDay();


            DataMgr.GameStartCount.Value++;
        }


        public void InspectionNewDay() {
            var login_time = LoginKit.Instance.LoginModel.LoginTime;
            var data = DateTimeManager.Instance.GetDateTime(login_time);
            var dateStr = DateTimeManager.Instance.DateTimeToYYYYMMDD(data);
            if (!DataMgr.Date.Value.Equals(dateStr)) {
                DataMgr.Date.Value = dateStr;
                msgList.Add(CtrlMsg.NewDays);
                DataMgr.LoginGameTodayTimes.Value = 1;
            }
            else {
                DataMgr.LoginGameTodayTimes.Value++;
            }
        }


        public void SendCtrlMsg(object args = null) {
            for (var i = 0; i < msgList.Count; i++) {
                ctrlDispatcher.Dispatch(msgList[i]);
            }

            msgList.Clear();
        }

        #region 生命周期

        protected override void OnInit() {
        }

        protected override void OnDispose() {
        }

        #endregion

        #region 消息

        protected override void AddListener() {
            ctrlDispatcher.AddListener(CtrlMsg.Preferences_InitComplete, PreferenceDataReady);
            ctrlDispatcher.AddListener(CtrlMsg.Game_StartBefore, SendCtrlMsg);
        }

        protected override void RemoveListener() {
            ctrlDispatcher.RemoveListener(CtrlMsg.Preferences_InitComplete, PreferenceDataReady);
            ctrlDispatcher.RemoveListener(CtrlMsg.Game_StartBefore, SendCtrlMsg);
        }

        #endregion
    }
}
