using System.Text;

namespace SHH.MjpegPlayer
{
    /// <summary>
    /// 按时间统计
    /// </summary>
    public class SumByTime
    {
        #region Defines

        /// <summary>最近刷新在哪一秒</summary>
        private int LastRefreshSecond = DateTime.Now.Second;

        /// <summary>最近刷新在哪一分钟</summary>
        private int LastRefreshMinute = DateTime.Now.Minute;

        /// <summary>最近刷新在哪一小时</summary>
        private int LastRefreshHour = DateTime.Now.Minute;

        /// <summary>秒统计</summary>
        private Dictionary<string, uint> _second
            = new Dictionary<string, uint>();

        /// <summary>分钟统计</summary>
        private Dictionary<string, uint> _minute
            = new Dictionary<string, uint>();

        /// <summary>小时统计</summary>
        private Dictionary<string, uint> _hour
            = new Dictionary<string, uint>();

        /// <summary>累计统计</summary>
        public Dictionary<string, ulong> All { get; init; }
            = new Dictionary<string, ulong>();

        #endregion

        #region TotalSecond

        /// <summary>秒统计</summary>
        public Dictionary<string, uint> TotalSecond { get; init; }
            = new Dictionary<string, uint>();

        #endregion

        #region TotalMinute

        /// <summary>分统计</summary>
        public Dictionary<string, uint> TotalMinute { get; init; }
            = new Dictionary<string, uint>();

        #endregion

        #region TotalHour

        /// <summary>小时统计</summary>
        public Dictionary<string, uint> TotalHour { get; init; }
            = new Dictionary<string, uint>();

        #endregion

        #region Refresh

        /// <summary>
        /// 刷新方法调用次数
        /// </summary>
        /// <param name="methodName"></param>
        /// <param name="count"></param>
        public void Refresh(string methodName, uint count = 1)
        {
            try
            {
                #region 秒统计同步

                lock (_second)
                {
                    // 确保键存在
                    if (!_second.ContainsKey(methodName))
                        _second.Add(methodName, 0);

                    if (!LastRefreshSecond.Equals(DateTime.Now.Second))
                    {
                        LastRefreshSecond = DateTime.Now.Second;
                        // 获取当前键的快照进行遍历，确保线程安全
                        var keys = _second.Keys.ToList();
                        foreach (var key in keys)
                        {
                            uint val = _second[key];
                            if (!TotalSecond.ContainsKey(key))
                                TotalSecond.Add(key, val);
                            else
                                TotalSecond[key] = val;

                            _second[key] = 0; // 重置当前秒计数值
                        }
                    }

                    _second[methodName] += count;
                }

                #endregion

                #region 分钟统计同步

                lock (_minute)
                {
                    if (!_minute.ContainsKey(methodName))
                        _minute.Add(methodName, 0);

                    if (!LastRefreshMinute.Equals(DateTime.Now.Minute))
                    {
                        LastRefreshMinute = DateTime.Now.Minute;
                        var keys = _minute.Keys.ToList();
                        foreach (var key in keys)
                        {
                            uint val = _minute[key];
                            if (!TotalMinute.ContainsKey(key))
                                TotalMinute.Add(key, val);
                            else
                                TotalMinute[key] = val;

                            _minute[key] = 0;
                        }
                    }
                    _minute[methodName] += count;
                }

                #endregion

                #region 小时统计同步

                lock (_hour)
                {
                    if (!_hour.ContainsKey(methodName))
                        _hour.Add(methodName, 0);

                    if (!LastRefreshHour.Equals(DateTime.Now.Hour))
                    {
                        LastRefreshHour = DateTime.Now.Hour;
                        var keys = _hour.Keys.ToList();
                        foreach (var key in keys)
                        {
                            uint val = _hour[key];
                            if (!TotalHour.ContainsKey(key))
                                TotalHour.Add(key, val);
                            else
                                TotalHour[key] = val;

                            _hour[key] = 0;
                        }
                    }
                    _hour[methodName] += count;
                }

                #endregion

                #region 全量累计同步

                lock (All)
                {
                    if (!All.ContainsKey(methodName))
                        All.Add(methodName, 0);

                    All[methodName] += (ulong)count;
                }

                #endregion
            }
            catch (Exception ex)
            {
                // 可选：利用 Ayay 项目规范记录日志
                // _sysLog.Warning("统计刷新异常: {Msg}", ex.Message);
            }
        }

        #endregion
    }
}