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

using static SKIT.FlurlHttpClient.Wechat.TenpayV3.Models.CreateApplyForSubMerchantApplymentRequest.Types.Business.Types.SaleScene.Types;

namespace CloudGaming.Code.Extend;

/// <summary>
/// 
/// </summary>
public static class UseAppDefaultRequest
{
    /// <summary>
    /// 添加默认请求
    /// </summary>
    /// <param name="app"></param>
    public static void UseAppRequest(this WebApplication app, string name = "")
    {
        app.MapGet("/", () => $"请求成功{name}==>{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}").WithName("默认请求");

        var startDateTime = DateTime.Now;
        var InformationalVersion = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
        Console.WriteLine($"version:{InformationalVersion}");
        app.MapGet("/system", () =>
        {

            using Process currentProcess = Process.GetCurrentProcess();
            // CPU使用率 (一般是一个0-100之间的值，但实际是时间占比，需要转换)
            double cpuUsage = currentProcess.TotalProcessorTime.TotalMilliseconds / Environment.TickCount * 100;
            // 已用内存 (字节)
            long memoryUsage = currentProcess.WorkingSet64;
            return new
            {
                msg = $"系统信息：启动时间:{startDateTime.ToString("yyyy-MM-dd HH:mm:ss")},已安全运行时间:{DateTime.Now.Subtract(startDateTime).TotalMinutes.ToString("0.##")}分钟",

                startDateTime,
                MemoryUsage = $"{memoryUsage / (1024.0 * 1024.0):F2}MB",
                CPUUsage = $"{cpuUsage:F2}%"

            };
        }).WithName("获取系统数据");
    }
}
