using GraphicsManager;
using GraphicsManager.Interfaces;
using GraphicsManager.Objects;
using OpenTK.Mathematics;
using OpenTK.Windowing.Desktop;
using OpenTK.Windowing.Common;

namespace Luski.GUI;

public class UpdateWindow : Window
{
    private RoundedButton? yes, no;
    private static readonly NativeWindowSettings Settings = new()
    {
        Title = "Update Available",
        WindowBorder = WindowBorder.Fixed,
        APIVersion = new Version(3, 2),
        StartFocused = true,
        Size = new Vector2i(481, 838),
        Icon = Globals.Icon,
        SharedContext = null
    };
    
    public enum DialogueResult
    {
        Yes,
        No,
        Closed
    }

    private DialogueResult Result = DialogueResult.Closed;
    
    public UpdateWindow() : base(Settings)
    {
        Label t;
        Controls.Add(t = new Label(Globals.DefaultFont) { Scale = 1.2f, Location = new(17.5.ScaleInt()), Text = "Luski has detected that your\nclient is on an older version\nfor your branch."});
        if (!Globals.Empty(Globals.UpdaterSettings.Updater))
        {
            t.Text += "\n\nWould you like to update?";
            Controls.Add(yes = new(Globals.LuskiTexture, Globals.DefaultFont)
            {
                Text = "Yes",
                Location = new(t.Location.X, t.Location.Y + t.Size.Y + (int)(t.Scale * t.Font.PixelHeight), 0),
                Size = new(t.Size.X, 33.5.ScaleInt())
            });
            Controls.Add(no = new(Globals.LuskiTexture, Globals.DefaultFont)
            {
                Text = "No", Location = new(t.Location.X, yes.Location.Y + yes.Size.Y + 20, 0),
                Size = new(yes.Size.X, 35.5.ScaleInt())
            });
            Size = new(t.Location.X + t.Location.X + t.Size.X, no.Location.Y + no.Size.Y + t.Location.X);
            yes.Clicked += YesOnClicked;
            no.Clicked += NoOnClicked;
        }
        else
        {
            t.Text += "\n\nNo updater path was set\nSet a path for auto updates";
            Controls.Add(no = new(Globals.LuskiTexture, Globals.DefaultFont)
            {
                Text = "Ok",
                Location = new(t.Location.X, t.Location.Y + t.Size.Y + (int)(t.Scale * t.Font.PixelHeight), 0),
                Size = new(t.Size.X, 35.5.ScaleInt()),
            });
            Size = new(t.Location.X + t.Location.X + t.Size.X, no.Location.Y + no.Size.Y + t.Location.X);
            no.Clicked += NoOnClicked;
        }

        try
        {
            CenterWindow(Globals.Settings.Display);
        }
        catch (Exception e)
        {
            CenterWindow();
        }
    }

    private Task NoOnClicked(IRenderObject arg)
    {
        Result = DialogueResult.No;
        base.Close();
        return Task.CompletedTask;
    }

    private Task YesOnClicked(IRenderObject arg)
    {
        Result = DialogueResult.Yes;
        base.Close();
        return Task.CompletedTask;
    }

    public DialogueResult ShowDialogue(Window? Parent = null)
    {
        try
        {
            if (Parent is not null) Parent.DrawFrame();
            StartRender();
            return Result;
        }
        finally
        {
            IsVisible = false;
            //Dispose();
        }
    }
}