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

namespace WebAPIBase.Utils
{
    public class Computer
    {
        /// <summary>
        /// cpu序列号
        /// </summary>
        public static string CpuID; //1.cpu序列号
        /// <summary>
        /// mac序列号
        /// </summary>
        public static string MacAddress; //2.mac序列号
        /// <summary>
        /// 硬盘id
        /// </summary>
        public static string DiskID; //3.硬盘id
        /// <summary>
        /// .ip地址
        /// </summary>
        public static string IpAddress; //4.ip地址
        /// <summary>
        /// 内存量 单位：M
        /// </summary>

        public static string TotalPhysicalMemory; //8.内存量 单位：M
        /// <summary>
        /// 主板
        /// </summary>

        public static string MotherboardID;

        static Computer()
        {
            CpuID = GetCpuID();
            MacAddress = GetMacAddress();
            DiskID = GetDiskID();
            IpAddress = GetIPAddress();
            MotherboardID = GetMotherboardID();
            TotalPhysicalMemory = GetTotalPhysicalMemory();

        }
        /// <summary>
        /// 获取CPU序列号代码 
        /// </summary>
        /// <returns></returns>

        static string GetCpuID()
        {
            try
            {
                string cpuInfo = "";//cpu序列号 
                ManagementClass mc = new ManagementClass("Win32_Processor");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                }
                moc = null;
                mc = null;
                return cpuInfo;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }

        }

        /// <summary>
        /// 获取网卡硬件地址
        /// </summary>
        /// <returns></returns>

        static string GetMacAddress()
        {
            try
            {
                string mac = "";
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    if ((bool)mo["IPEnabled"] == true)
                    {
                        mac = mo["MacAddress"].ToString();
                        break;
                    }
                }
                moc = null;
                mc = null;
                return mac;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }

        }

        /// <summary>
        /// 获取硬盘ID
        /// </summary>
        /// <returns></returns>
        static string GetDiskID()
        {
            try
            {
                String HDid = "";
                ManagementClass mc = new ManagementClass("Win32_DiskDrive");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    HDid = (string)mo.Properties["Model"].Value;
                }
                moc = null;
                mc = null;
                return HDid;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }

        }

        /// <summary>
        /// 获取主板序列号
        /// </summary>
        /// <returns></returns>
        private static string GetMotherboardID()
        {
            var myMb = new ManagementClass("Win32_BaseBoard").GetInstances();
            var serial = "";
            foreach (ManagementObject mb in myMb)
            {
                var val = mb.Properties["SerialNumber"].Value;
                serial += val == null ? "" : val.ToString();
            }
            return serial;
        }


        /// <summary>
        /// 获取IP地址 
        /// </summary>
        /// <returns></returns>

        static string GetIPAddress()
        {
            try
            {
                string st = "";
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    if ((bool)mo["IPEnabled"] == true)
                    {
                        //st=mo["IpAddress"].ToString(); 
                        System.Array ar;
                        ar = (System.Array)(mo.Properties["IpAddress"].Value);
                        st = ar.GetValue(0).ToString();
                        break;
                    }
                }
                moc = null;
                mc = null;
                return st;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }

        }

        /// <summary>
        /// 操作系统的登录用户名 
        /// </summary>
        /// <returns></returns>
        static string GetUserName()
        {
            try
            {
                string un = "";

                un = Environment.UserName;
                return un;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }

        }



        /// <summary>
        /// 获取计算机名
        /// </summary>
        /// <returns></returns>
        static string GetComputerName()
        {
            try
            {
                return System.Environment.MachineName;

            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }
        }



        /// <summary>
        /// PC类型 
        /// </summary>
        /// <returns></returns>
        static string GetSystemType()
        {
            try
            {
                string st = "";
                ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {

                    st = mo["SystemType"].ToString();

                }
                moc = null;
                mc = null;
                return st;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }
        }



        /// <summary>
        /// 物理内存
        /// </summary>
        /// <returns></returns>
        static string GetTotalPhysicalMemory()
        {
            try
            {

                string st = "";
                ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {

                    st = mo["TotalPhysicalMemory"].ToString();

                }
                moc = null;
                mc = null;
                return st;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }

        }

        /// <summary>
        /// 根据cpu序列化，主板id及内存信息生成16为授权码（16位HASH代码）
        /// </summary>
        /// <returns></returns>
        public static string AuthorizationCode(string cpu, string motherBoard, string physicalMemory)
        {
            string sensorID = string.Empty;
            string basicID = cpu + motherBoard + physicalMemory;
            System.Security.Cryptography.MD5CryptoServiceProvider mD5CryptoServiceProvider = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] hashedBuff = mD5CryptoServiceProvider.ComputeHash(Encoding.UTF8.GetBytes(basicID));
            for (int i = 4; i < 12; i++)
            {
                sensorID += hashedBuff[i].ToString("X2");
            }
            return sensorID;
        }

    }

}
