using System.Linq;
using BingoBrain.Core;

namespace BingoBrain
{
    public class JTodModel : BaseModel
    {
        /// <summary>
        /// 签到持续时间
        /// </summary>
        public int DailyDonusContinueTime;

        public int GetDailyDonusDay()
        {
            var val = GameHelper.GetNowTime() - PreferencesMgr.Instance.OpenDailyBonusStampTime;
            if (val <= 0)
            {
                val = 1;
            }

            var daySum = val / GameHelper.sunSecond;
            if (val % GameHelper.sunSecond > 0)
            {
                daySum++;
            }

            if (daySum > PreferencesMgr.Instance.DailyBonusItemLst.Count)
            {
                SetDailyBonusItem();
                return GetDailyDonusDay();
            }

            return (int)daySum;
        }

        public string GetDailyDonusDayTime()
        {
            return GameHelper.GetInterval(GameHelper.GetTomorrowCountTime());
        }

        #region 生命周期

        protected override void OnInit()
        {
        }

        protected override void OnDispose()
        {
        }

        protected override void OnReset()
        {
        }

        #endregion

        #region 读取数据

        protected override void OnReadData()
        {
            var signDailyRewardModel = GameHelper.GetConfig<SignDailyModel>();
            DailyDonusContinueTime = GameHelper.sunSecond * signDailyRewardModel.GetCount();
        }

        #endregion


        #region 消息

        protected override void AddListener()
        {
            ctrlDispatcher.AddListener(CtrlMsg.Game_StartLater, InitData);
        }

        private void InitData(object o)
        {
            GetDailyDonusDay();
            if (PreferencesMgr.Instance.DailyBonusItemLst.Count == 0)
            {
                SetDailyBonusItem();
            }

            Hall.Instance.UpdateSecondEvent += CheckData;
        }

        private void CheckData()
        {
            PreferencesMgr.Instance.DailyBonusItemLst.Clear();
            var val = GameHelper.GetNowTime() - PreferencesMgr.Instance.OpenDailyBonusStampTime;
            // 刷新签到
            if (PreferencesMgr.Instance.DailyBonusItemLst.Count == 0 || val >= DailyDonusContinueTime)
            {
                SetDailyBonusItem();
            }
        }

        private void SetDailyBonusItem()
        {
            PreferencesMgr.Instance.DailyBonusItemLst.Clear();
            var signDailyRewardModel = GameHelper.GetConfig<SignDailyModel>();
            if (signDailyRewardModel != null)
            {
                foreach (var bonusItem in signDailyRewardModel.GetDataList().Select(item => new DailyBonusItem
                         {
                             id = item.item1,
                             quantity = item.quantity[0],
                             isDouble = item.isMulti,
                             cont_index = item.cont_index,
                             IsCollect = false
                         }))
                {
                    PreferencesMgr.Instance.DailyBonusItemLst.Add(bonusItem);
                }

                PreferencesMgr.Instance.OpenDailyBonusStampTime = GameHelper.GetNowTime();
            }
        }

        protected override void RemoveListener()
        {
            ctrlDispatcher.RemoveListener(CtrlMsg.Game_Start, InitData);
        }

        #endregion
    }
}

public class DailyBonusItem
{
    /// <summary>
    /// 奖励id
    /// </summary>
    public int id;

    /// <summary>
    /// 奖励数量
    /// </summary>
    public float quantity;

    /// <summary>
    /// 是否翻倍
    /// </summary>
    public bool isDouble;

    /// <summary>
    /// 控制器索引
    /// </summary>
    public int cont_index;

    /// <summary>
    /// 是否领取过奖励
    /// </summary>
    public bool IsCollect;
}