using ImGuiNET;

namespace Shoko;

[MediaType("text/gemini")]
class GeminiMediaHandler : MediaHandler
{
    List<string> lines;
    List<string> queries;
    public GeminiMediaHandler(ProtoHandler content)
    {
        Content = content;
        lines = new List<string>();
        queries = new List<string>();
    }

    public override async Task Load()
    {
        Title = Content.URL.AbsolutePath;
        var reader = new StreamReader(Content.Content);
        string line;
        while((line = await reader.ReadLineAsync()) is not null)
        {
            lines.Add(line);
        }
        OnLoaded();
    }

    public override void Render()
    {
        var formatting = true;
        var querynum = 0;
        foreach(var line in lines)
        {
            if(line.StartsWith("```"))
            {
                formatting = !formatting;
                if(formatting) ImGui.PopFont();
                else ImGui.PushFont(MainUI.MonospaceFont);
            }
            else if(formatting)
            {
                if(line.StartsWith("=>"))
                {
                    var parts = line[2..].Trim().Split(new char[]{' ', '\t'}, 2, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
                    var title = parts[parts.Length > 1 ? 1 : 0];
                    var linkurl = new Uri(Content.URL, parts[0]).ToString();
                    Gui.Link(title.Trim(), linkurl, ()=>
                        Content.CurrentTab.Load(linkurl));
                }
                else if(line.StartsWith("#"))
                {
                    var heading = 1;
                    if(line.StartsWith("##")) heading = 2;
                    if(line.StartsWith("###")) heading = 3;

                    Gui.Font(MainUI.HeadingFonts[heading-1], ()=>
                        ImGui.TextUnformatted(line[heading..].Trim()));
                }
                else if(line.StartsWith("* "))
                {
                    ImGui.BulletText(line[2..].Trim());
                }
                else if(line.StartsWith(">"))
                {
                    ImGui.TextDisabled("    "+line.Trim());
                }
                else if(line.StartsWith("---"))
                { // non standard extension from mozz
                    if(line.Trim(new char[]{' ','-'}).Length > 0)
                        ImGui.SeparatorText(line.Trim(new char[]{' ','-'}));
                    else
                        ImGui.Separator();
                }
                else if(line.StartsWith("=:"))
                { // non standard extension from spartan
                    var parts = line[2..].Trim().Split(new char[]{' ', '\t'}, 2, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
                    var title = parts[parts.Length > 1 ? 1 : 0];
                    var linkurl = new Uri(Content.URL, parts[0]);

                    if(queries.Count <= querynum) queries.Add("");
                    var str = queries[querynum];
                    ImGui.InputText(title.Trim()+"##query"+querynum, ref str, 1024);
                    queries[querynum] = str;

                    Gui.Button("Submit", ()=>{
                        var uri = new UriBuilder(linkurl)
                        {
                            Query = queries[querynum]
                        };
                        Content.CurrentTab.Load(uri.Uri.ToString());
                    });
                    querynum++;
                }
                else
                    ImGui.TextWrapped(line.Trim());
            }
            else
                ImGui.TextUnformatted(line);
        }
        if(!formatting) ImGui.PopFont();
    }
}