using System.Net;
using System.Net.Http.Json;
using System.Text;
using System.Text.RegularExpressions;
using Markdig;
using Web.Models;

namespace Web.Services;

public class DocsService
{
    private static readonly MarkdownPipeline Pipeline = new MarkdownPipelineBuilder()
        .UseYamlFrontMatter()
        .UsePipeTables()
        .Build();

    private static readonly HashSet<string> ImageExtensions = new(StringComparer.OrdinalIgnoreCase)
    {
        ".png", ".jpg", ".jpeg", ".gif", ".svg", ".webp", ".bmp"
    };

    private readonly HttpClient _http;
    private NotesIndex? _index;
    private readonly Dictionary<string, string> _markdownCache = new();

    public DocsService(HttpClient http)
    {
        _http = http;
    }

    public async Task<NotesIndex> GetIndexAsync()
    {
        if (_index != null)
            return _index;

        _index = await _http.GetFromJsonAsync<NotesIndex>("docs/notes-index.json")
                 ?? new NotesIndex();

        return _index;
    }

    public async Task<NoteDocument?> GetNoteAsync(string slug)
    {
        var index = await GetIndexAsync();
        var noteInfo = index.Notes.FirstOrDefault(n =>
            string.Equals(n.Slug, slug, StringComparison.OrdinalIgnoreCase));
        if (noteInfo == null) return null;

        try
        {
            var markdown = await _http.GetStringAsync($"docs/notes/{slug}.md");
            return await ParseDocument(slug, noteInfo.Title, markdown);
        }
        catch
        {
            return null;
        }
    }

    private async Task<NoteDocument> ParseDocument(string slug, string title, string markdown, int depth = 0)
    {
        var doc = new NoteDocument
        {
            Slug = slug,
            Title = title
        };

        var frontmatterLines = new List<string>();
        var bodyLines = new List<string>();
        var inFrontmatter = false;
        var frontmatterDone = false;

        foreach (var line in markdown.Replace("\r\n", "\n").Split('\n'))
        {
            var trimmed = line.Trim();
            if (!frontmatterDone && trimmed == "---")
            {
                if (!inFrontmatter)
                {
                    inFrontmatter = true;
                    continue;
                }

                inFrontmatter = false;
                frontmatterDone = true;
                continue;
            }

            if (inFrontmatter)
                frontmatterLines.Add(line);
            else if (frontmatterDone || !string.IsNullOrWhiteSpace(line))
                bodyLines.Add(line);
        }

        if (frontmatterLines.Count > 0)
        {
            foreach (var line in frontmatterLines)
            {
                var colonIdx = line.IndexOf(':');
                if (colonIdx > 0)
                {
                    var key = line[..colonIdx].Trim();
                    if (string.Equals(key, "category", StringComparison.OrdinalIgnoreCase))
                        doc.Category = line[(colonIdx + 1)..].Trim().Trim('"');
                }
            }

            var fmHtml = new StringBuilder();
            fmHtml.Append("<table class=\"frontmatter\">");
            foreach (var line in frontmatterLines)
            {
                var colonIdx = line.IndexOf(':');
                if (colonIdx > 0)
                {
                    var key = line[..colonIdx].Trim();
                    var value = line[(colonIdx + 1)..].Trim().Trim('"');
                    fmHtml.Append("<tr><td class=\"fm-key\">");
                    fmHtml.Append(WebUtility.HtmlEncode(key));
                    fmHtml.Append("</td><td class=\"fm-value\">");
                    var encoded = WebUtility.HtmlEncode(value);
                    fmHtml.Append(ConvertWikiLinks(encoded));
                    fmHtml.Append("</td></tr>");
                }
                else
                {
                    fmHtml.Append("<tr><td colspan=\"2\">");
                    fmHtml.Append(ConvertWikiLinks(WebUtility.HtmlEncode(line.Trim())));
                    fmHtml.Append("</td></tr>");
                }
            }

            fmHtml.Append("</table>");
            doc.FrontmatterHtml = fmHtml.ToString();
        }

        var body = string.Join("\n", bodyLines);
        body = ConvertWikiLinks(body);
        var html = Markdown.ToHtml(body, Pipeline);
        html = await ResolveEmbedsInHtml(html, depth);
        doc.HtmlContent = html;

        return doc;
    }

    private async Task<string> ResolveEmbedsInHtml(string html, int depth)
    {
        if (depth > 10) return html;

        var regex = new Regex(@"(?:<p>)?!\[\[([^\]]+)\]\](?:</p>)?");
        var sb = new StringBuilder();
        var lastIndex = 0;

        foreach (Match match in regex.Matches(html))
        {
            sb.Append(html, lastIndex, match.Index - lastIndex);

            var filename = match.Groups[1].Value.Trim();
            var replacement = await ResolveEmbed(filename, depth);
            sb.Append(replacement);

            lastIndex = match.Index + match.Length;
        }

        sb.Append(html, lastIndex, html.Length - lastIndex);
        return sb.ToString();
    }

    private async Task<string> ResolveEmbed(string filename, int depth)
    {
        var ext = Path.GetExtension(filename);

        if (ImageExtensions.Contains(ext))
        {
            return $"<img src=\"/docs/images/{filename}\" alt=\"{WebUtility.HtmlEncode(filename)}\" />";
        }

        var slug = Slugify(filename);
        try
        {
            var markdown = await GetMarkdownAsync(slug);
            var embedded = await ParseDocument(slug, filename, markdown, depth + 1);
            return $"<div class=\"embed\">{embedded.HtmlContent}</div>";
        }
        catch
        {
            return $"<div class=\"embed-error\">[Embed not found: {WebUtility.HtmlEncode(filename)}]</div>";
        }
    }

    private async Task<string> GetMarkdownAsync(string slug)
    {
        if (_markdownCache.TryGetValue(slug, out var cached))
            return cached;

        var markdown = await _http.GetStringAsync($"docs/notes/{slug}.md");
        _markdownCache[slug] = markdown;
        return markdown;
    }

    private static string ConvertWikiLinks(string text)
    {
        return Regex.Replace(
            text,
            @"(?<!!)\[\[([^\]]+)\]\]",
            match =>
            {
                var content = match.Groups[1].Value;
                var parts = content.Split('|');
                var linkText = parts.Length > 1 ? parts[1].Trim() : parts[0].Trim();
                var target = parts[0].Trim();
                var slug = Slugify(target);
                return $"<a href=\"/docs/{slug}\">{WebUtility.HtmlEncode(linkText)}</a>";
            });
    }

    public static string Slugify(string title)
    {
        var slug = title.ToLowerInvariant()
            .Replace(' ', '-')
            .Replace("'", "")
            .Replace(".", "")
            .Replace("(", "")
            .Replace(")", "");
        slug = Regex.Replace(slug, @"[^a-z0-9\-]", "");
        slug = Regex.Replace(slug, @"-+", "-");
        return slug.Trim('-');
    }
}
