using GraphicsManager.Enums;
using GraphicsManager.Interfaces;
using GraphicsManager.Objects;
using Luski.GUI.MainScreen.UI.PublicServers.ServerSettingPages;
using Luski.net;
using OpenTK.Mathematics;

namespace Luski.GUI.MainScreen.UI.PublicServers;

public class ServerSettings : UserControl
{
    private string BehindName;
    public ServerSettingPageTab? Selected;
    public FlowLayout fl;

    public ServerSettings(PublicServer ps)
    {
        base.BackgroundColor = new(34, 34, 34, 255);
        base.Size = Globals.ms.ClientSize;
        BehindName = Globals.ms.Title;
        Globals.ms.Title = $"Server Settings - Luski";
        fl = new()
        {
            BackgroundColor = new(20, 20, 20, 255),
            Size = new(307.ScaleInt(), base.Size.Y),
            Anchor = ObjectAnchor.Top | ObjectAnchor.Left | ObjectAnchor.Bottom
        };
        ServerSettingCategory ssc = new(ps.Name, this);
        ServerRolePage srp;
        ServerSettingPageTab cb = ssc.AddPage(srp = new(ps)
        {
            BackgroundColor = base.BackgroundColor
        });
        Controls.Add(srp);
        fl.Controls.Add(ssc);
        cb.ToggleSelected().Wait();
        Anchor = ObjectAnchor.All;
        Controls.Add(fl);
        Rectangle closebtn = new(Globals.ms.TextureManager.GetTextureResource("close.png"))
        {
            Location = new(Globals.ms.ClientSize.X - 40.ScaleInt(), 8.ScaleInt(),0),
            Size = new(32.ScaleInt()),
            Shader = Rectangle.DefaultAlphaShader[Globals.ms.Context],
            BackgroundColor = Color4.Gray,
            Anchor = ObjectAnchor.Top | ObjectAnchor.Right
        };
        closebtn.MouseEnter += _ =>
        {
            closebtn.BackgroundColor = Color4.White;
            return Task.CompletedTask;
        };
        closebtn.MouseLeave += _ =>
        {
            closebtn.BackgroundColor = Color4.Gray;
            return Task.CompletedTask;
        };
        closebtn.Clicked += ClosebtnOnClicked;
        closebtn.ForceDistanceUpdate(this);
        Controls.Add(closebtn);
    }
    
    private Task ClosebtnOnClicked(IRenderObject arg)
    {
        Globals.ms.Controls.Remove(this);
        Globals.ms.Title = BehindName;
        Globals.ms.DrawFrame();
        return Task.CompletedTask;
    }
}