using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace MeterVision.Util
{
    public class LicenseValidator
    {

        public static bool ValidateLicense(string licenseKey, string publicKey)
        {
            // 分割注册码为签名和许可证数据
            //string[] parts = licenseKey.Split("::");
            string[] parts = licenseKey.Split(new string[] { "::" }, StringSplitOptions.None);
            if (parts.Length != 2)
            {
                Console.WriteLine("无效的注册码格式");
                return false;
            }

            string signatureBase64 = parts[0];
            string jsonData = parts[1];

            // 解析许可证数据
            var licenseData = JsonConvert.DeserializeObject<dynamic>(jsonData);
            string machineCode = licenseData.MachineCode;
            string expirationDateStr = licenseData.ExpirationDate;

            // 检查硬件码是否匹配
            string currentMachineCode = RegistrationHelper.GenerateMachineCode();
                //HardwareInfo.GenerateMachineCode();
            if (machineCode != currentMachineCode)
            {
                Console.WriteLine("激活码不匹配");
                return false;
            }

            // 检查有效期
            DateTime expirationDate = DateTime.Parse(expirationDateStr);
            if (DateTime.Now > expirationDate)
            {
                Console.WriteLine("注册码已过期");
                return false;
            }

            // 验证签名
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
            {
                rsa.FromXmlString(publicKey);

                // 将签名从 Base64 字符串转换为字节数组
                byte[] signature = Convert.FromBase64String(signatureBase64);

                // 对许可证数据进行哈希
                byte[] dataToVerify = Encoding.UTF8.GetBytes(jsonData);

                // 验证签名
                bool isValid = rsa.VerifyData(dataToVerify, CryptoConfig.MapNameToOID("SHA256"), signature);
                if (!isValid)
                {
                    Console.WriteLine("无效的签名");
                    return false;
                }

                Console.WriteLine("注册码验证成功");
                return true;
            }
        }

        public static string GetPublicKeyFromResource()
        {
            // 获取当前程序集
            Assembly assembly = Assembly.GetExecutingAssembly();

            // 列出所有嵌入的资源名称
            foreach (string resourceName1 in assembly.GetManifestResourceNames())
            {
                Console.WriteLine(resourceName1);
            }

            // 获取资源名称
            string resourceName = "MeterVision.public_key.xml"; // 注意命名空间和文件名

            // 读取嵌入的资源文件
            using (Stream stream = assembly.GetManifestResourceStream(resourceName))
            {
                if (stream == null)
                {
                    return null;
                }
                    //throw new FileNotFoundException("无法找到私钥资源文件");

                using (StreamReader reader = new StreamReader(stream))
                {
                    return reader.ReadToEnd();
                }
            }
        }



        //--------------------------------------------------
    }
}
