using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Diagnostics;
using System.Management;
using System.Runtime.InteropServices;

namespace Admin.Areas.Identity.Pages.Server
{
    public class InfoModel : PageModel
    {
        private readonly IHostEnvironment _env;

        // 앱, 호스트(Host)
        public required string EnvironmentName { get; set; }
        public required string ContentRootPath { get; set; }
        public required string ApplicationName { get; set; }

        // OS, Framework
        public required string OSDescription { get; set; }
        public required string OSArchitecture { get; set; }
        public required string FrameworkDescription { get; set; }
        public required string ProcessArchitecture { get; set; }

        public long WorkingSet { get; set; }
        public TimeSpan TotalCpuTime { get; set; }

        // 시스템
        public required string MachineName { get; set; }
        public required string CurrentDirectory { get; set; }
        public required string SystemDirectory { get; set; }
        public bool Is64BitOperatingSystem { get; set; }
        public bool Is64BitProcess { get; set; }
        public int ProcessorCount { get; set; }

        public required string TickCount { get; set; }

        public ManagementObjectCollection? CPU { get; private set; }
        public ManagementObjectCollection? Memory { get; private set; }
        public ManagementObjectCollection? Disk { get; private set; }

        public InfoModel(IHostEnvironment env)
        {
            _env = env;
        }

        public async Task<IActionResult> OnGetAsync()
        {
            var process = Process.GetCurrentProcess();

            // 앱, 호스트(Host) 관련
            EnvironmentName = _env.EnvironmentName;
            ContentRootPath = _env.ContentRootPath;
            ApplicationName = _env.ApplicationName;

            // OS, Framework 관련
            OSDescription = RuntimeInformation.OSDescription;
            OSArchitecture = RuntimeInformation.OSArchitecture.ToString();
            FrameworkDescription = RuntimeInformation.FrameworkDescription;
            ProcessArchitecture = RuntimeInformation.ProcessArchitecture.ToString();

            WorkingSet = process.WorkingSet64 / 1048576; // 물리 메모리 사용량(바이트)
            TotalCpuTime = process.TotalProcessorTime; // 프로세스 실행 시간

            // 시스템(환경변수) 관련
            MachineName = Environment.MachineName;
            CurrentDirectory = Environment.CurrentDirectory;
            SystemDirectory = Environment.SystemDirectory;
            Is64BitOperatingSystem = Environment.Is64BitOperatingSystem;
            Is64BitProcess = Environment.Is64BitProcess;
            ProcessorCount = Environment.ProcessorCount;

            // 시작 시각 등
            TickCount = TimeSpan.FromMicroseconds(Environment.TickCount).ToString(); // 시스템 시작 후 경과 시간(밀리초)

            // 플랫폼 조건부 코드로 Windows에서만 실행되도록 수정
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                // CPU 정보
                CPU = new ManagementObjectSearcher("select * from Win32_Processor").Get();

                // 메모리 정보
                Memory = new ManagementObjectSearcher("select * from Win32_MemoryDevice").Get();

                // 디스크 정보
                Disk = new ManagementObjectSearcher("select * from Win32_DiskDrive").Get();
            }

            return Page();
        }
    }
}