using CZGL.SystemInfo;
using System; 
using System.Collections.ObjectModel;
using System.Linq; 
using System.Threading; 
using 常用工具集.Base;

namespace 常用工具集.ViewModel._04破解及系统相关
{
    public class 服务器性能监控ViewModel : ViewModelBase
    {
        public string OSArchitecture { get; set; } = "";
        public string OSPlatformID { get; set; } = "";
        public string OSVersion { get; set; } = "";
        public string OSDescription { get; set; } = "";
        public string ProcessArchitecture { get; set; } = "";
        public string ProcessorCount { get; set; } = "";
        public string MachineName { get; set; } = "";
        public string FrameworkVersion { get; set; } = "";
        public string FrameworkDescription { get; set; } = "";

        public string CPURate { get; set; } = "";
        public string MemoryRate { get; set; } = "";
        public string UploadSpeed { get; set; } = "";
        public string DownloadSpeed { get; set; } = "";

        public ObservableCollection<MyDisk> DataList { get; set; }

        public 服务器性能监控ViewModel()
        {
            DataList = new ObservableCollection<MyDisk>();
            DiskInfo[] diskInfo = DiskInfo.GetDisks();
            foreach (DiskInfo info in diskInfo)
            {
                MyDisk myDisk = new MyDisk();
                myDisk.Name = info.Name;
                myDisk.SetValue(info.TotalSize, info.UsedSize, info.FreeSpace);
                DataList.Add(myDisk);
            }
            OSArchitecture = SystemPlatformInfo.OSArchitecture;
            OSPlatformID = SystemPlatformInfo.OSPlatformID;
            OSVersion = SystemPlatformInfo.OSVersion;
            OSDescription = SystemPlatformInfo.OSDescription;
            ProcessArchitecture = SystemPlatformInfo.ProcessArchitecture;
            ProcessorCount = SystemPlatformInfo.ProcessorCount.ToString();
            MachineName = SystemPlatformInfo.MachineName;
            FrameworkVersion = SystemPlatformInfo.FrameworkVersion;
            FrameworkDescription = SystemPlatformInfo.FrameworkDescription;
            new Thread(() =>
            {
                CPUTime v1 = CPUHelper.GetCPUTime();
                var network = NetworkInfo.TryGetRealNetworkInfo();
                var oldRate = network.GetIpv4Speed();
                while (true)
                {
                    try
                    {
                        Thread.Sleep(1000);
                        var v2 = CPUHelper.GetCPUTime();
                        var value = CPUHelper.CalculateCPULoad(v1, v2);
                        v1 = v2;
                        var memory = MemoryHelper.GetMemoryValue();
                        var newRate = network.GetIpv4Speed();
                        var speed = NetworkInfo.GetSpeed(oldRate, newRate);
                        oldRate = newRate;
                        CPURate = $"{(int)(value * 100)} %";
                        MemoryRate = $"{memory.UsedPercentage}%";
                        UploadSpeed = $"{speed.Sent.Size} {speed.Sent.SizeType}/S";
                        DownloadSpeed = $"{speed.Received.Size} {speed.Received.SizeType}/S";
                        DiskInfo[] diskInfos = DiskInfo.GetDisks();
                        foreach (DiskInfo info1 in diskInfos)
                        {
                            MyDisk disk = DataList.Where(it => it.Name == info1.Name).FirstOrDefault();
                            if (disk != null)
                            {
                                disk.SetValue(info1.TotalSize, info1.UsedSize, info1.FreeSpace);
                            }
                        }

                    }
                    catch
                    {
                        Thread.Sleep(10);
                    }
                }
            }).Start();
        }

    }

    public class MyDisk : ViewModelBase
    {
        public void SetValue(long total, long used, long free)
        {
            Total = GetLength(total);
            Used = GetLength(used);
            Free = GetLength(free);
        }
        public string Name { get; set; }
        public string Total { get; set; }
        public string Used { get; set; }
        public string Free { get; set; }


        private string GetLength(long bytes)
        {
            double tb = Math.Pow(1024.0, 4);
            if (bytes > tb)
            {
                return $"{((int)((bytes / tb) * 100)) / 100.0f}TB";
            }
            double gb = Math.Pow(1024.0, 3);
            if (bytes > gb)
            {
                return $"{((int)((bytes / gb) * 100)) / 100.0f}GB";
            }
            double mb = Math.Pow(1024.0, 2);
            if (bytes > mb)
            {
                return $"{((int)((bytes / mb) * 100)) / 100.0f}MB";
            }
            else if (bytes > 1024)
            {
                return $"{((int)((bytes / 1024.0f) * 100)) / 100.0f}KB";
            }
            else
            {
                return $"{bytes}Byte";
            }
        }
    }

}
