using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using UnityEngine;
using System.Linq;
using System.Net.NetworkInformation;

namespace NeoriginPCSDK
{
    public static class NeoriginSDKLibrary
    {

        public static string EncryptCBC(string plainText, string key, string iv)
        {
            // 转换密钥和 IV
            byte[] keyBytes = Encoding.UTF8.GetBytes(key);
            keyBytes = SHA256.Create().ComputeHash(keyBytes); // 生成 256 位密钥
            keyBytes = keyBytes[..32];
            byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
            ivBytes = ivBytes[..16];

            byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);

            using (Aes aes = Aes.Create())
            {
                aes.Key = keyBytes;
                aes.IV = ivBytes;
                aes.Mode = CipherMode.CBC;
                aes.Padding = PaddingMode.PKCS7;

                using (ICryptoTransform encryptor = aes.CreateEncryptor())
                {
                    byte[] encryptedBytes = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length);
                    return Convert.ToBase64String(encryptedBytes);
                }
            }
        }

        public static string DecryptCBC(string encryptedBase64, string key, string iv)
        {
            // 转换密钥和 IV
            byte[] keyBytes = Encoding.UTF8.GetBytes(key);
            keyBytes = SHA256.Create().ComputeHash(keyBytes); // 生成 256 位密钥
            keyBytes = keyBytes[..32];
            byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
            ivBytes = ivBytes[..16];

            byte[] encryptedBytes = Convert.FromBase64String(encryptedBase64);

            using (Aes aes = Aes.Create())
            {
                aes.Key = keyBytes;
                aes.IV = ivBytes;
                aes.Mode = CipherMode.CBC;
                aes.Padding = PaddingMode.PKCS7;

                using (ICryptoTransform decryptor = aes.CreateDecryptor())
                {
                    byte[] decryptedBytes = decryptor.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);
                    return Encoding.UTF8.GetString(decryptedBytes);
                }
            }
        }

        public static string GetPCModel()
        {
            string model = "Unknown";
            /*
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_ComputerSystem");
            foreach (ManagementObject obj in searcher.Get())
            {
                model = obj["Manufacturer"] + " " + obj["Model"];
            }*/
            return model;
        }

        public static long GetTotalDiskSpace()
        {
            DriveInfo[] drives = DriveInfo.GetDrives();
            long totalSpace = 0;
            foreach (DriveInfo drive in drives)
            {
                if (drive.IsReady)
                    totalSpace += drive.TotalSize;
            }
            return totalSpace / (1024 * 1024 * 1024); // GB
        }

        public static long GetFreeDiskSpace()
        {
            DriveInfo drive = new DriveInfo(Path.GetPathRoot(Application.dataPath));
            return drive.AvailableFreeSpace / (1024 * 1024 * 1024); // GB
        }

        public static string GetUtcOffset()
        {
            TimeSpan offset = TimeZoneInfo.Local.GetUtcOffset(DateTime.Now);
            string sign = offset.Hours >= 0 ? "+" : "-";
            return $"UTC{sign}{Math.Abs(offset.Hours)}";
        }

        // 获取第一个活跃网卡的MAC地址

        public static string GetMacAddress()
        {
            // 获取第一个活跃网卡的MAC地址
            NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
            NetworkInterface activeInterface = interfaces.FirstOrDefault(
                ni => ni.OperationalStatus == OperationalStatus.Up &&
                    !ni.Description.Contains("Virtual") &&
                    !ni.Name.Contains("Virtual")
            );

            if (activeInterface != null)
            {
                return activeInterface.GetPhysicalAddress().ToString();
            }
            return "00-00-00-00-00-00"; // 默认值
        }

        public static string GenerateRandomString(int length)
        {
            if (length <= 0)
            {
                Debug.LogWarning("GenerateRandomString length must be greater than 0.");
                return string.Empty;
            }

            const string chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
            char[] result = new char[length];

            for (int i = 0; i < length; i++)
            {
                result[i] = chars[UnityEngine.Random.Range(0, chars.Length)];
            }

            return new string(result);
        }
        //获取分辨率
        public static string GetScreenResolution()
        {
            return $"{Screen.width}*{Screen.height}";
        }

        //获取网络类型
        public static string GetNetworkType()
        {
            string network = "WiFi";
            if (Application.internetReachability == NetworkReachability.NotReachable)
            {
                Debug.Log("GetNetworkType no network");
                network = "Unknown";
            }
            else if (Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork)
            {
                Debug.Log("GetNetworkType USB");
                network = "USB";
            }
            else
            {
                Debug.Log("GetNetworkType WiFi");
            }

            return network;
        }
    }
}