using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace AxCopilot.Services;

public static class GuideEncryptor
{
	private static readonly byte[] Key = new byte[32]
	{
		65, 88, 67, 111, 112, 105, 108, 111, 116, 71,
		117, 105, 100, 101, 75, 101, 121, 50, 48, 50,
		54, 83, 101, 99, 117, 114, 101, 70, 105, 120,
		101, 100
	};

	private static readonly byte[] Iv = new byte[16]
	{
		71, 117, 105, 100, 101, 73, 86, 70, 105, 120,
		101, 100, 50, 48, 50, 54
	};

	public static void EncryptFile(string inputHtmlPath, string outputEncPath)
	{
		byte[] array = File.ReadAllBytes(inputHtmlPath);
		using Aes aes = Aes.Create();
		aes.Key = Key;
		aes.IV = Iv;
		aes.Mode = CipherMode.CBC;
		aes.Padding = PaddingMode.PKCS7;
		using ICryptoTransform cryptoTransform = aes.CreateEncryptor();
		byte[] bytes = cryptoTransform.TransformFinalBlock(array, 0, array.Length);
		File.WriteAllBytes(outputEncPath, bytes);
	}

	public static string DecryptToString(string encFilePath)
	{
		byte[] array = File.ReadAllBytes(encFilePath);
		using Aes aes = Aes.Create();
		aes.Key = Key;
		aes.IV = Iv;
		aes.Mode = CipherMode.CBC;
		aes.Padding = PaddingMode.PKCS7;
		using ICryptoTransform cryptoTransform = aes.CreateDecryptor();
		byte[] bytes = cryptoTransform.TransformFinalBlock(array, 0, array.Length);
		return Encoding.UTF8.GetString(bytes);
	}

	public static string DecryptToString(byte[] encrypted)
	{
		using Aes aes = Aes.Create();
		aes.Key = Key;
		aes.IV = Iv;
		aes.Mode = CipherMode.CBC;
		aes.Padding = PaddingMode.PKCS7;
		using ICryptoTransform cryptoTransform = aes.CreateDecryptor();
		byte[] bytes = cryptoTransform.TransformFinalBlock(encrypted, 0, encrypted.Length);
		return Encoding.UTF8.GetString(bytes);
	}

	public static void EncryptGuides(string assetsFolder)
	{
		string text = Path.Combine(assetsFolder, "AX Copilot 사용가이드.htm");
		string text2 = Path.Combine(assetsFolder, "AX Copilot 개발자가이드.htm");
		if (File.Exists(text))
		{
			EncryptFile(text, Path.Combine(assetsFolder, "guide_user.enc"));
		}
		if (File.Exists(text2))
		{
			EncryptFile(text2, Path.Combine(assetsFolder, "guide_dev.enc"));
		}
	}
}
