using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using AxCopilot.Models;

namespace AxCopilot.Services;

public static class PresetService
{
	private static readonly List<TopicPreset> _presets = new List<TopicPreset>();

	private static bool _loaded;

	public static IReadOnlyList<TopicPreset> Presets
	{
		get
		{
			if (!_loaded)
			{
				Load();
			}
			return _presets;
		}
	}

	public static TopicPreset? GetByCategory(string category)
	{
		return Presets.FirstOrDefault((TopicPreset p) => p.Category == category);
	}

	public static IReadOnlyList<TopicPreset> GetByTab(string tab)
	{
		return Presets.Where((TopicPreset p) => p.Tab.Equals(tab, StringComparison.OrdinalIgnoreCase)).ToList();
	}

	public static IReadOnlyList<TopicPreset> GetByTabWithCustom(string tab, IEnumerable<CustomPresetEntry> customPresets)
	{
		List<TopicPreset> list = GetByTab(tab).ToList();
		foreach (CustomPresetEntry item in customPresets.Where((CustomPresetEntry c) => c.Tab.Equals(tab, StringComparison.OrdinalIgnoreCase)))
		{
			list.Add(new TopicPreset
			{
				Category = "custom_" + item.Id,
				Label = item.Label,
				Symbol = item.Symbol,
				Color = item.Color,
				Description = item.Description,
				SystemPrompt = item.SystemPrompt,
				Placeholder = "",
				Tab = item.Tab,
				IsCustom = true,
				CustomId = item.Id
			});
		}
		return list;
	}

	private static void Load()
	{
		_loaded = true;
		Assembly executingAssembly = Assembly.GetExecutingAssembly();
		string value = "AxCopilot.Assets.Presets.";
		string[] manifestResourceNames = executingAssembly.GetManifestResourceNames();
		foreach (string text in manifestResourceNames)
		{
			if (!text.StartsWith(value) || !text.EndsWith(".json"))
			{
				continue;
			}
			try
			{
				using Stream stream = executingAssembly.GetManifestResourceStream(text);
				if (stream == null)
				{
					continue;
				}
				using StreamReader streamReader = new StreamReader(stream);
				string json = streamReader.ReadToEnd();
				TopicPreset topicPreset = JsonSerializer.Deserialize<TopicPreset>(json, new JsonSerializerOptions
				{
					PropertyNameCaseInsensitive = true
				});
				if (topicPreset != null)
				{
					_presets.Add(topicPreset);
				}
			}
			catch (Exception ex)
			{
				LogService.Warn("프리셋 로드 실패 (" + text + "): " + ex.Message);
			}
		}
		Dictionary<string, int> catOrder = ChatCategory.All.Select(((string Key, string Label, string Symbol, string Color) c, int item) => (Key: c.Key, i: item)).ToDictionary(((string Key, int i) x) => x.Key, ((string Key, int i) x) => x.i);
		_presets.Sort(delegate(TopicPreset a, TopicPreset b)
		{
			int order = a.Order;
			int order2 = b.Order;
			if (order != order2)
			{
				return order.CompareTo(order2);
			}
			int valueOrDefault = catOrder.GetValueOrDefault(a.Category, 99);
			int valueOrDefault2 = catOrder.GetValueOrDefault(b.Category, 99);
			return valueOrDefault.CompareTo(valueOrDefault2);
		});
		LogService.Info($"대화 주제 프리셋 {_presets.Count}개 로드 완료");
	}
}
