using System.Reflection;
using System.Text;
using System.Web;
using ImGuiNET;
using Raylib_cs;

namespace Shoko;

[Protocol("about")]
[Protocol("shoko")]
class AboutProtoHandler : ProtoHandler
{
    string txtDebug = "";

    public AboutProtoHandler(Uri url)
    {
        URL = url;
    }

    public override Task Load()
    {
        var path = new UriBuilder(URL).Path;
        Content = new MemoryStream(new byte[]{});

        switch(path)
        {
            case "blank":
            case "":
            case "about":
            case "shoko":
            case "version":
            case "demo":
                break;
            case "debugtext":
                txtDebug = HttpUtility.UrlDecode(URL.Query);
                if(txtDebug.Length > 0) txtDebug = txtDebug[1..];
                break;
            default:
                throw new NotImplementedException();
        }

        MediaType = "text/plain";
        Status = "OK";
        OnLoaded();
        return Task.CompletedTask;
    }

    public override void Render()
    {
        var path = new UriBuilder(URL).Path;

        switch(path)
        {
            case "":
            case "about":
            case "shoko":
            case "version":
                var info = Assembly.GetExecutingAssembly().GetName();
                ImGui.Text("硝子 (shōko) v"+info.Version.ToString());
                ImGui.Text("an experimental browser");
                ImGui.Separator();
                ImGui.Text("(c) 2023 a39 studios");
                ImGui.Text("licensed under LiLiQ-P");
                ImGui.Separator();
                ImGui.Text(".NET v" + Environment.Version.ToString());
                ImGui.Text("Dear ImGui v" + ImGui.GetVersion());
                ImGui.Text("Raylib v" + Raylib.RAYLIB_VERSION);
                ImGui.Separator();
                Gui.TreeNode("Supported protos", ()=>{
                    foreach(var proto in SupportedProtos)
                        ImGui.BulletText(proto);
                });
                Gui.TreeNode("Supported media", ()=>{
                    foreach(var media in MediaHandler.SupportedMedia)
                        ImGui.BulletText(media);
                });
                break;
            case "demo":
                ImGui.ShowDemoWindow();
                break;
            case "debugtext":
                ImGui.InputText("Text", ref txtDebug, 1024);
                ImGui.SameLine();
                Gui.Button("Debug", ()=>{
                    var tourl = new UriBuilder(URL)
                    {
                        Query = txtDebug
                    };
                    CurrentTab.Load(tourl.Uri.ToString());
                });
                ImGui.DebugTextEncoding(HttpUtility.UrlDecode(URL.Query));
                break;
            default:
                break;
        }
    }
}