using System.Numerics;
using FluentFTP.Helpers;
using ImGuiNET;

namespace Shoko;

class Tab
{
    ProtoHandler Handler;
    public bool IsOpen = true;
    Exception Error = null;
    Stack<Uri> History;

    string txtURL = "";

    public Tab(string url)
    {
        History = new Stack<Uri>();
        Load(url);
        txtURL = url;
    }

    public async Task Load(string url)
    {
        Error = null;
        ImGui.SetScrollX(0);
        ImGui.SetScrollY(0);
        try
        {
            Handler = ProtoHandler.GetHandler(url);
            Handler.CurrentTab = this;
            txtURL = Handler.URL.ToString();
            History.Push(Handler.URL);
            await Handler.Load();
        }
        catch(Exception ex)
        {
            Error = ex;
        }
    }

    public void Previous()
    {
        if(History.Count > 1)
        {
            History.Pop();
            Load(History.Pop().ToString());
        }
    }

    public void Render()
    {
        var title = txtURL;
        if(Handler.Media is not null)
        {
            title = Handler.Media.Title;
        }
        Gui.Window(title+"###"+GetHashCode().ToString(), ref IsOpen, ImGuiWindowFlags.MenuBar | ImGuiWindowFlags.HorizontalScrollbar, ()=>
        {
            Gui.MenuBar(()=>{
                Gui.Menu("File", ()=>{
                    Gui.MenuItem("Close", null, ()=> IsOpen = false);
                });

                if(Error is null)
                {
                    if(Handler.IsLoaded)
                    {
                        Handler.MenuBar();
                        if(Handler.Media.IsLoaded)
                            Handler.Media.MenuBar();
                    }
                }

                Gui.Button("<<", ()=>{
                    Previous();
                });

                ImGui.InputText("##url", ref txtURL, 1024);

                Gui.Button("Go", ()=>{
                    Load(txtURL);
                });
                if(!Handler.IsLoaded)
                {
                    ImGui.Text("Loading...");
                    if(Handler.TotalBytes != 0 && Handler.TotalBytes != Handler.LoadedBytes)
                        ImGui.ProgressBar(Handler.LoadedBytes / Handler.TotalBytes, new Vector2(200, 20),
                            Handler.LoadedBytes.FileSizeToString() + "/" + Handler.TotalBytes.FileSizeToString());
                }
                else if(!Handler.Media.IsLoaded)
                {
                    ImGui.Text("Rendering...");
                }
            });
            if(Error is not null)
            {
                ImGui.Text("error: can't load page");
                ImGui.Text(Error.Message);
                ImGui.Text(Error.StackTrace);
            }
            else
            {
                try
                {
                    if(Handler.IsLoaded)
                    {
                        if(Handler.Media.IsLoaded)
                            Handler.Media.Render();
                        Handler.Render();
                    }
                }
                catch(Exception ex)
                {
                    ImGui.Text("error: can't render page");
                    ImGui.Text(ex.Message);
                    ImGui.Text(ex.StackTrace);
                }
            }
        });
    }
}