using System;
using System.Security.Cryptography;
using System.Text;
using Microsoft.Win32;

namespace RevitEstimate
{
    public static class LicenseManager
    {
        private const string RegistryKeyPath = @"SOFTWARE\PoojaEnterprises\RevitEstimate";

        // 1. Computer ka Unique Machine ID nikalne ke liye (No External Dependency)
        public static string GetMachineId()
        {
            try
            {
                // Machine Name aur OS Details ko combine karke unique signature banana
                string uniqueString = Environment.MachineName + Environment.UserName + "PoojaEnterprises2026";
                return ComputeSha256Hash(uniqueString).Substring(0, 8).ToUpper();
            }
            catch
            {
                return "POOJA999"; // Fallback ID
            }
        }

        // 2. Machine ID ke liye valid Activation Key generate karne ka secret formula
        public static string GenerateKeyForMachine(string machineId)
        {
            string raw = machineId + "SecretSaltKey_RevitEstimate";
            string hash = ComputeSha256Hash(raw).Substring(0, 8).ToUpper();
            return $"POOJA-{machineId}-{hash}";
        }

        private static string ComputeSha256Hash(string rawData)
        {
            using (SHA256 sha256Hash = SHA256.Create())
            {
                byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(rawData));
                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < bytes.Length; i++)
                {
                    builder.Append(bytes[i].ToString("x2"));
                }
                return builder.ToString().ToUpper();
            }
        }

        // 3. Check karna ki license valid hai ya nahi
        public static bool IsLicensed()
        {
            try
            {
                using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RegistryKeyPath))
                {
                    if (key != null)
                    {
                        object savedKeyObj = key.GetValue("LicenseKey");
                        if (savedKeyObj != null)
                        {
                            string savedKey = savedKeyObj.ToString();
                            string currentMachineId = GetMachineId();

                            // Verify ki saved key isi computer ki bani hai ya nahi
                            string validKeyForThisPc = GenerateKeyForMachine(currentMachineId);
                            return savedKey == validKeyForThisPc;
                        }
                    }
                }
            }
            catch { }
            return false;
        }

        // 4. License Key validate karke save karna
        public static bool ValidateAndSaveLicense(string enteredKey)
        {
            string currentMachineId = GetMachineId();
            string expectedKey = GenerateKeyForMachine(currentMachineId);

            if (enteredKey.Trim().ToUpper() == expectedKey)
            {
                try
                {
                    using (RegistryKey key = Registry.CurrentUser.CreateSubKey(RegistryKeyPath))
                    {
                        if (key != null)
                        {
                            key.SetValue("IsLicensed", 1, RegistryValueKind.DWord);
                            key.SetValue("LicenseKey", expectedKey, RegistryValueKind.String);
                            return true;
                        }
                    }
                }
                catch { }
            }
            return false;
        }
    }
}