using AgileConfig.Client;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using CloudGaming.Code.DataAccess.MultiTenantUtil;
using CloudGaming.DtoModel.Other;

using Quartz;

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
using static SKIT.FlurlHttpClient.Wechat.TenpayV3.ExtendedSDK.Global.Models.QueryPartnerRefundsResponse.Types;
using HuanMeng.DotNetCore.QuartzExtend;

namespace CloudGaming.Code.Monitor;

/// <summary>
/// 监控扩展类
/// </summary>
public static class MonitorExtend
{
    /// <summary>
    /// 
    /// </summary>
    public static ConcurrentDictionary<string, AppMonitorInfo> AppMonitorConfigs { get; set; } = new ConcurrentDictionary<string, AppMonitorInfo>();


    /// <summary>
    /// 添加监控服务
    /// </summary>
    /// <param name="builder"></param>
    /// <returns></returns>
    public static WebApplicationBuilder AddMonitorConfig(this WebApplicationBuilder builder)
    {
        builder.Services.AddQuartzWithAttributes(typeof(MonitorExtend).Assembly);
        return builder;
    }

    /// <summary>
    /// 获取app监控数据
    /// </summary>
    /// <param name="appConfig"></param>
    /// <returns></returns>
    public static AppMonitorInfo GetAppMonitorInfo(this AppConfig appConfig)
    {
        AppMonitorInfo appMonitorInfo = MonitorExtend.AppMonitorConfigs.GetOrAdd(appConfig.Identifier, new AppMonitorInfo());
        return appMonitorInfo;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="appMonitorInfo"></param>
    /// <returns></returns>
    public static AppBasicStatistics ToAppBasicStatistics(this AppMonitorInfo appMonitorInfo)
    {
        return new AppBasicStatistics()
        {
            CurrentOnlineUsers = appMonitorInfo.CurrentOnlineUsers,
            CurrentPlayingUsers = appMonitorInfo.CurrentPlayingUsers,
            CurrentQueuedUsers = appMonitorInfo.CurrentQueuedUsers,
            TodayIntendedOrders = appMonitorInfo.TodayIntendedOrders,
            TodayLoggedInUsers = appMonitorInfo.TodayLoggedInUsers,
            TodayPaidOrders = appMonitorInfo.TodayPaidOrders,
            TodayRechargeAmount = appMonitorInfo.TodayRechargeAmount,
            TodayRegisteredUsers = appMonitorInfo.TodayRegisteredUsers
        };
    }



}
