using GraphicsManager.Enums;
using GraphicsManager.Objects;
using Luski.net.Structures.Public;
using OpenTK.Mathematics;
using SixLabors.ImageSharp.Processing;

namespace Luski.GUI.MainScreen.UI.PublicServers.ServerSettingPages;

public class RoleIcon : UserControl
{
    private ServerRoleInteraction SRI;
    private Role ThisRole;
    public RoleIcon(Role role, ServerRoleInteraction sri)
    :base(Globals.ms.TextureManager.GetTextureResource("RoundedRectangle.png"))
    {
        SRI = sri;
        ThisRole = role;
        base.BackgroundColor = new (0,0,0,0);
        TextureDisplay = TextureDisplay.HorizontalCenter;
        Shader = Rectangle.DefaultAlphaShader[Globals.ms.Context];
        base.Size = new(sri.RoleFlow.Size.X, 50.ScaleInt());
        Rectangle Color = new(Globals.ms.TextureManager.GetAlphaCircle())
        {
            Size = new(12.ScaleInt()),
            Location = new(4.ScaleInt(), 19.ScaleInt(), 0),
            Shader = Rectangle.DefaultAlphaShader[Globals.ms.Context],
            IgnoreHover = true,
            BackgroundColor = role.Color.ToColor4()
        };
        Label name = new(Globals.ServerRoleFont)
        {
            Text = role.DisplayName,
            IgnoreHover = true
        };
        name.Location = new(Color.Location.X + Color.Size.X + 4.ScaleInt(),
            (Color.Location.Y + ((Color.Size.Y - name.Size.Y) / 2)), 0);
        Controls.Add(name);
        Controls.Add(Color);
        Clicked += _ =>
        {
            if (sri.Selected != this)
            {
                sri.Selected.SetSelected(false);
                sri.Selected = this;
                SetSelected(true);
            }
            return Task.CompletedTask;
        };
        MouseEnter += _ =>
        {
            BackgroundColor = new(141, 151, 165, 30);
            return Task.CompletedTask;
        };
        MouseLeave += _ =>
        {
            BackgroundColor = sri.Selected == this ? new(141, 151, 165, 51) : new(0, 0, 0, 0);
            return Task.CompletedTask;
        };
    }

    public void SetSelected(bool value)
    {
        if (value)
        {
            BackgroundColor = new(141, 151, 165, 51);
            SRI.SRO.LoadRole(ThisRole);
        }
        else
        {
            BackgroundColor = new(0, 0, 0, 0);
        }
    }
}