using System;
using System.Collections.Generic;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using AxCopilot.SDK;

namespace AxCopilot.Handlers;

public class EncodeHandler : IActionHandler
{
	public string? Prefix => "encode ";

	public PluginMetadata Metadata => new PluginMetadata("Encoder", "인코딩/해싱 — encode base64/url/hex/md5/sha256 텍스트", "1.0", "AX");

	public Task<IEnumerable<LauncherItem>> GetItemsAsync(string query, CancellationToken ct)
	{
		string text = query.Trim();
		if (string.IsNullOrWhiteSpace(text))
		{
			return Task.FromResult(HelpItems());
		}
		string text2;
		if (text.StartsWith("decode ", StringComparison.OrdinalIgnoreCase))
		{
			text2 = text;
			string rest = text2.Substring(7, text2.Length - 7).Trim();
			return Task.FromResult(HandleDecode(rest));
		}
		int num = text.IndexOf(' ');
		if (num < 0)
		{
			return Task.FromResult(HelpItems());
		}
		string type = text.Substring(0, num).Trim().ToLowerInvariant();
		text2 = text;
		int num2 = num + 1;
		string input = text2.Substring(num2, text2.Length - num2);
		return Task.FromResult(HandleEncode(type, input));
	}

	private static IEnumerable<LauncherItem> HandleEncode(string type, string input)
	{
		try
		{
			if (1 == 0)
			{
			}
			IEnumerable<LauncherItem> result;
			switch (type)
			{
			case "base64":
			case "b64":
				result = SingleResult("Base64 인코딩", Convert.ToBase64String(Encoding.UTF8.GetBytes(input)));
				break;
			case "url":
				result = SingleResult("URL 인코딩", Uri.EscapeDataString(input));
				break;
			case "hex":
				result = SingleResult("HEX 인코딩", Convert.ToHexString(Encoding.UTF8.GetBytes(input)).ToLowerInvariant());
				break;
			case "md5":
				result = SingleResult("MD5 해시", MD5Hash(input));
				break;
			case "sha1":
				result = SingleResult("SHA-1 해시", SHA1Hash(input));
				break;
			case "sha256":
				result = SingleResult("SHA-256 해시", SHA256Hash(input));
				break;
			case "sha512":
				result = SingleResult("SHA-512 해시", SHA512Hash(input));
				break;
			case "html":
				result = SingleResult("HTML 엔티티 인코딩", WebUtility.HtmlEncode(input));
				break;
			default:
				result = new _003C_003Ez__ReadOnlySingleElementList<LauncherItem>(new LauncherItem("알 수 없는 타입: " + type, "base64, url, hex, md5, sha1, sha256, sha512, html 중 선택", null, null, null, "\ue7ba"));
				break;
			}
			if (1 == 0)
			{
			}
			return result;
		}
		catch (Exception ex)
		{
			return new _003C_003Ez__ReadOnlySingleElementList<LauncherItem>(new LauncherItem("변환 오류", ex.Message, null, null, null, "\uea39"));
		}
	}

	private static IEnumerable<LauncherItem> HandleDecode(string rest)
	{
		int num = rest.IndexOf(' ');
		if (num < 0)
		{
			return new _003C_003Ez__ReadOnlySingleElementList<LauncherItem>(new LauncherItem("사용법: encode decode <타입> <값>", "타입: base64, url, hex, html", null, null, null, "\ue946"));
		}
		string text = rest.Substring(0, num).Trim().ToLowerInvariant();
		int num2 = num + 1;
		string text2 = rest.Substring(num2, rest.Length - num2);
		try
		{
			if (1 == 0)
			{
			}
			IEnumerable<LauncherItem> result;
			switch (text)
			{
			case "base64":
			case "b64":
				result = SingleResult("Base64 디코딩", Encoding.UTF8.GetString(Convert.FromBase64String(text2)));
				break;
			case "url":
				result = SingleResult("URL 디코딩", Uri.UnescapeDataString(text2));
				break;
			case "hex":
				result = SingleResult("HEX 디코딩", Encoding.UTF8.GetString(Convert.FromHexString(text2)));
				break;
			case "html":
				result = SingleResult("HTML 엔티티 디코딩", WebUtility.HtmlDecode(text2));
				break;
			default:
				result = new _003C_003Ez__ReadOnlySingleElementList<LauncherItem>(new LauncherItem("디코딩 불가 타입: " + text, "디코딩 지원: base64, url, hex, html", null, null, null, "\ue7ba"));
				break;
			}
			if (1 == 0)
			{
			}
			return result;
		}
		catch (Exception ex)
		{
			return new _003C_003Ez__ReadOnlySingleElementList<LauncherItem>(new LauncherItem("디코딩 오류", ex.Message, null, null, null, "\uea39"));
		}
	}

	private static IEnumerable<LauncherItem> SingleResult(string title, string result)
	{
		string text = ((result.Length > 80) ? (result.Substring(0, 77) + "…") : result);
		return new _003C_003Ez__ReadOnlySingleElementList<LauncherItem>(new LauncherItem(title, text + "  ·  Enter로 복사", null, result, null, "\ue8cb"));
	}

	private static IEnumerable<LauncherItem> HelpItems()
	{
		return new _003C_003Ez__ReadOnlyArray<LauncherItem>(new LauncherItem[7]
		{
			new LauncherItem("encode base64 <텍스트>", "Base64 인코딩/디코딩", null, null, null, "\ue8cb"),
			new LauncherItem("encode url <텍스트>", "URL 퍼센트 인코딩/디코딩", null, null, null, "\ue8cb"),
			new LauncherItem("encode hex <텍스트>", "16진수 인코딩/디코딩", null, null, null, "\ue8cb"),
			new LauncherItem("encode md5 <텍스트>", "MD5 해시 (단방향)", null, null, null, "\ue8cb"),
			new LauncherItem("encode sha256 <텍스트>", "SHA-256 해시 (단방향)", null, null, null, "\ue8cb"),
			new LauncherItem("encode html <텍스트>", "HTML 엔티티 인코딩/디코딩", null, null, null, "\ue8cb"),
			new LauncherItem("encode decode base64 <값>", "Base64 디코딩 예시", null, null, null, "\ue8cb")
		});
	}

	private static string MD5Hash(string input)
	{
		byte[] inArray = MD5.HashData(Encoding.UTF8.GetBytes(input));
		return Convert.ToHexString(inArray).ToLowerInvariant();
	}

	private static string SHA1Hash(string input)
	{
		byte[] inArray = SHA1.HashData(Encoding.UTF8.GetBytes(input));
		return Convert.ToHexString(inArray).ToLowerInvariant();
	}

	private static string SHA256Hash(string input)
	{
		byte[] inArray = SHA256.HashData(Encoding.UTF8.GetBytes(input));
		return Convert.ToHexString(inArray).ToLowerInvariant();
	}

	private static string SHA512Hash(string input)
	{
		byte[] inArray = SHA512.HashData(Encoding.UTF8.GetBytes(input));
		return Convert.ToHexString(inArray).ToLowerInvariant();
	}

	public Task ExecuteAsync(LauncherItem item, CancellationToken ct)
	{
		if (item.Data is string text)
		{
			try
			{
				Clipboard.SetText(text);
			}
			catch
			{
			}
		}
		return Task.CompletedTask;
	}
}
