using ImGuiNET;

namespace Shoko;

[MediaType("text/gopher-menu")]
class GopherMediaHandler : MediaHandler
{
    List<string> lines;
    List<string> queries;
    public GopherMediaHandler(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()
    {
        Gui.Font(MainUI.MonospaceFont, ()=>
        {
            var querynum = 0;
            foreach(var line in lines)
            {
                if(line.Length > 0)
                {
                    var l = line.Split('\t');
                    var type = l[0][0];
                    var info = l[0].Length > 1 ? l[0][1..] : "";
                    switch(type)
                    {
                        case '.':
                            break;
                        case 'i':
                        case '3': {
                            Gui.Text(info);
                        } break;
                        case '2':
                        case '8': 
                        case 'T': {
                            var link = new UriBuilder(Content.URL){
                                Scheme = type == '2' ? "cso" : "telnet",
                                Host = l[2],
                                Port = int.Parse(l[3]),
                                Path = l[1],
                            }.ToString();
                            Gui.Link(info, link, ()=>
                                Content.CurrentTab.Load(link));
                        } break;
                        case '7': {
                            if(queries.Count <= querynum) queries.Add("");
                            var str = queries[querynum];
                            ImGui.InputText(info+"##query"+querynum, ref str, 1024);
                            queries[querynum] = str;

                            Gui.Button("Submit", ()=>{
                                var url = new UriBuilder(Content.URL){
                                    Host = l[2],
                                    Port = int.Parse(l[3]),
                                    Path = type.ToString()+l[1],
                                }.ToString();
                                Content.CurrentTab.Load(url+"%09"+queries[querynum]);
                            });
                            querynum++;
                        } break;
                        default: {
                            var link = "";
                            if(l[1].StartsWith("URL:"))
                                link = l[1][4..];
                            else
                                link = new UriBuilder(Content.URL){
                                    Host = l[2],
                                    Port = int.Parse(l[3]),
                                    Path = type.ToString()+l[1],
                                }.ToString();
                            Gui.Link(info, link, ()=>
                                Content.CurrentTab.Load(link));
                        } break;
                    }
                }
            }
        });
    }
}