using CloudGaming.DtoModel.Other;

using Microsoft.Win32;

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using static SKIT.FlurlHttpClient.Wechat.TenpayV3.Models.CreateNewTaxControlFapiaoApplicationRequest.Types.Fapiao.Types;

namespace CloudGaming.Code.Monitor;

/// <summary>
/// 监控类
/// </summary>
public class MonitorBLL : CloudGamingBase
{
    public MonitorBLL(IServiceProvider serviceProvider) : base(serviceProvider)
    {
    }

    /// <summary>
    /// 获取监控数据
    /// </summary>
    /// <returns></returns>
    public AppBasicStatistics GetAppMonitorInfo()
    {
        return AppConfig.GetAppMonitorInfo().ToAppBasicStatistics();
    }

    /// <summary>
    /// 获取近一月活跃人数统计
    /// </summary>
    /// <returns></returns>
    public async Task<dynamic> GetActiveUserCount(long startTimeStamp = 0, long endTimeStamp = 0)
    {
        if (endTimeStamp == 0)
        {
            endTimeStamp = DateTime.Now.ToUnixTimestamp();
        }
        if (startTimeStamp == 0)
        {
            startTimeStamp = DateTime.Now.AddMonths(-1).ToUnixTimestamp();
        }
        //开始时间
        DateOnly startDate = DateOnly.FromDateTime(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(startTimeStamp));
        //结束时间
        DateOnly endDate = DateOnly.FromDateTime(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(endTimeStamp));
        DateOnly curr = startDate;
        var userStatisticsList = await Dao.DaoExt.Context.T_Statistics_User.Where(it => it.LoginDate > startDate && it.LoginDate < endDate).ToListAsync();
        var channels = userStatisticsList.GroupBy(it => it.Channel).Select(it => it.Key).ToList();
        List<StatisticsDto> userLogin = new List<StatisticsDto>();
        List<StatisticsDto> userRegistr = new List<StatisticsDto>();
        List<StatisticsDto> userActive = new List<StatisticsDto>();
        var nowDay = int.Parse(DateTime.Now.ToString("yyyyMMdd"));
        while (curr <= endDate)
        {
            int day = int.Parse(curr.ToString("yyyyMMdd"));
            var _Statistics_Users = userStatisticsList.Where(it => it.LoginDate == curr).OrderBy(it => it.Channel).ToList();
            var statisticsDtoUserList = new List<StatisticsDto>();
            var statisticsDtoRegistrList = new List<StatisticsDto>();
            var statisticsDtoActiveList = new List<StatisticsDto>();

            var currDateStr = curr.ToString("MM月dd号");
            StatisticsDto _UserLogin = new StatisticsDto(currDateStr, 0, "全部");
            StatisticsDto _userRegistr = new StatisticsDto(currDateStr, 0, "全部");
            StatisticsDto _userActive = new StatisticsDto(currDateStr, 0, "全部");
            if (day == nowDay)
            {
                var m = AppConfig.GetAppMonitorInfo();
                _UserLogin.Value += m.TodayLoggedInUsers;
                _userRegistr.Value += m.TodayRegisteredUsers;
                _userActive.Value += 0;

            }
            foreach (var _channel in channels)
            {


                var item = _Statistics_Users.FirstOrDefault(it => it.Channel == _channel);

                if (item == null)
                {
                    item = new T_Statistics_User();
                }
                _UserLogin.Value += item.LoginCount;
                _userRegistr.Value += item.RegistrCount;
                _userActive.Value += item.ActiveCount;
                //item.ActiveCount
                statisticsDtoUserList.Add(new StatisticsDto(currDateStr, item.LoginCount, item.Channel));
                statisticsDtoRegistrList.Add(new StatisticsDto(currDateStr, item.RegistrCount, item.Channel));
                statisticsDtoActiveList.Add(new StatisticsDto(currDateStr, item.ActiveCount, item.Channel));

            }
            userLogin.Add(_UserLogin);
            userLogin.AddRange(statisticsDtoUserList);
            userRegistr.Add(_userRegistr);
            userRegistr.AddRange(statisticsDtoRegistrList);
            userActive.Add(_userActive);
            userActive.AddRange(statisticsDtoActiveList);

            curr = curr.AddDays(1);
        }
        return new { Login = userLogin, Registr = userRegistr, Active = userActive };
    }

    /// <summary>
    /// 获取近一月登录人数统计
    /// </summary>
    /// <returns></returns>
    public async Task GetLoginUserCount()
    {

    }
    /// <summary>
    /// 获取近一月注册人数统计
    /// </summary>
    /// <returns></returns>
    public async Task GetRegisteredUserCount()
    {
    }

}

