using System;
using System.Collections.Generic;
using UnityEngine;
using BingoBrain.Core;

namespace BingoBrain
{
    public class BingoHallModel : BaseModel
    {
        public decimal show101;
        public decimal show102;

        public event Action RefreshUIEvent;
        public bool IsAni { get; set; }

        private void RefreshData(object obj)
        {
            if (!IsAni)
            {
                show101 = PreferencesMgr.Instance.Currency101;
                show102 = PreferencesMgr.Instance.Currency102;
                RefreshUI();
            }
        }

        public string NextCardBoardTimeStr
        {
            get
            {
                long sum = PreferencesMgr.Instance.NextRecoveryTime - GameHelper.GetNowTime();
                if (sum <= 0)
                {
                    return string.Empty;
                }

                return GameHelper.GetNoDecimalUnitStr((int)sum);
            }
        }

        public int CardBoardCountIndex
        {
            get { return PreferencesMgr.Instance.CardBoardIndex; }
            set { PreferencesMgr.Instance.CardBoardIndex = value; }
        }

        /// <summary>
        /// 最大卡牌数量
        /// </summary>
        public int MaxCardBoardSum { get; private set; }

        /// <summary>
        /// 恢复一张卡板的时间
        /// </summary>
        public int CardBoardRecoveryTime { get; private set; }

        #region 生命周期

        protected override void OnInit()
        {
        }

        protected override void OnDispose()
        {
        }

        protected override void OnReset()
        {
        }

        #endregion

        #region 读取数据

        protected override void OnReadData()
        {
            MaxCardBoardSum = GameHelper.GetCommonModel().MaxCardBoardSum[0];
            CardBoardRecoveryTime = GameHelper.GetCommonModel().CardBoardRecoveryTime;
            show101 = PreferencesMgr.Instance.Currency101;
            show102 = PreferencesMgr.Instance.Currency102;
        }

        #endregion


        #region 消息

        protected override void AddListener()
        {
            CtrlDispatcher.Instance.AddListener(CtrlMsg.Game_Start, CalculateOfflineTime);

            DataDispatcher.Instance.AddListener(PreferencesMsg.currency101, RefreshData);
            DataDispatcher.Instance.AddListener(PreferencesMsg.currency102, RefreshData);

            PreferencesDispatcher<int>.Instance.AddListener(PreferencesMsg.cardBoardSum, ChangeRecoveryTime);
            Hall.Instance.UpdateSecondEvent += CheckCarBoard;
        }

        protected override void RemoveListener()
        {
            DataDispatcher.Instance.RemoveListener(PreferencesMsg.currency101, RefreshData);
            DataDispatcher.Instance.RemoveListener(PreferencesMsg.currency102, RefreshData);

            CtrlDispatcher.Instance.RemoveListener(CtrlMsg.Game_Start, CalculateOfflineTime);
            PreferencesDispatcher<int>.Instance.RemoveListener(PreferencesMsg.cardBoardSum, ChangeRecoveryTime);
            Hall.Instance.UpdateSecondEvent -= CheckCarBoard;
        }

        #endregion

        /// <summary>
        /// 改变卡板恢复时间
        /// </summary>
        private void ChangeRecoveryTime(object o)
        {
            ExiteValue<int> exiteValue = o as ExiteValue<int>;
            if (exiteValue.oldValue >= MaxCardBoardSum)
            {
                if (exiteValue.newValue < MaxCardBoardSum)
                {
                    PreferencesMgr.Instance.NextRecoveryTime =
                        GameHelper.GetNowTime() + CardBoardRecoveryTime;
                }
            }
        }

        /// <summary>
        /// 在线恢复
        /// </summary>
        private void CheckCarBoard()
        {
            if (PreferencesMgr.Instance.CardBoardSum >= MaxCardBoardSum)
            {
                return;
            }

            if (GameHelper.GetNowTime() >= PreferencesMgr.Instance.NextRecoveryTime)
            {
                int val = Mathf.Clamp(PreferencesMgr.Instance.CardBoardSum + 1, 0, MaxCardBoardSum);
                PreferencesMgr.Instance.CardBoardSum = val;
                PreferencesMgr.Instance.NextRecoveryTime =
                    GameHelper.GetNowTime() + CardBoardRecoveryTime;
            }
        }

        /// <summary>
        /// 计算离线卡牌增加
        /// </summary>
        private void CalculateOfflineTime(object o)
        {
            if (PreferencesMgr.Instance.CardBoardSum >= MaxCardBoardSum)
            {
                return;
            }

            Debug.LogFormat("计算离线时间搓，当前时间搓{0}，生成下一张卡牌的时间搓{1}", DateTimeBoardk.Instance.GetServerCurrTimestamp(),
                PreferencesMgr.Instance.NextRecoveryTime);
            if (GameHelper.GetNowTime() >= PreferencesMgr.Instance.NextRecoveryTime)
            {
                int val =
                    (int)(GameHelper.GetNowTime() - PreferencesMgr.Instance.NextRecoveryTime);
                int sum1 = val / CardBoardRecoveryTime + 1;
                int sum2 = val % CardBoardRecoveryTime;
                int sum3 = Mathf.Clamp(sum1 + PreferencesMgr.Instance.CardBoardSum, 0, MaxCardBoardSum);
                if (sum3 >= MaxCardBoardSum)
                {
                    PreferencesMgr.Instance.NextRecoveryTime =
                        GameHelper.GetNowTime() + CardBoardRecoveryTime;
                }
                else
                {
                    PreferencesMgr.Instance.NextRecoveryTime =
                        GameHelper.GetNowTime() + CardBoardRecoveryTime - sum2;
                }

                PreferencesMgr.Instance.CardBoardSum = sum3;
            }
        }

        public void RefreshUI()
        {
            RefreshUIEvent?.Invoke();
        }
    }
}