using System;
using System.Collections.Generic;

namespace ScrewsMaster
{
    public class PreferencesDataReadyCtrl : BaseCtrl
    {
        LoginModel userModel;
        private List<uint> msgList = new List<uint>();

        #region 生命周期

        protected override void OnInit()
        {
            userModel = ModuleManager.Instance.GetModel(ModelConst.LoginModel) as LoginModel;
        }

        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


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


            PreferencesMgr.Instance.GameStartCount++;
        }


        public void InspectionNewDay()
        {
            long login_time = userModel.login_time;
            DateTime data = DateTimeManager.Instance.GetDateTime(login_time);
            string dateStr = DateTimeManager.Instance.DateTimeToYYYYMMDD(data);
            if (!PreferencesMgr.Instance.Date.Equals(dateStr))
            {
                PreferencesMgr.Instance.Date = dateStr;
                msgList.Add(CtrlMsg.NewDays);
                PreferencesMgr.Instance.LoginGameTodayTimes = 1;
            }
            else
            {
                PreferencesMgr.Instance.LoginGameTodayTimes++;
            }
        }


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

            msgList.Clear();
        }
    }
}