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

namespace Luski.GUI.MainScreen.UI.PublicServers.ServerSettings.Pages.Server.Roles;

public class ServerRoleInteraction : UserControl
{
    public FlowLayout RoleFlow;
    public RoleIcon Selected;
    public ServerRoleOptions SRO;
    
    public ServerRoleInteraction(PublicServer server, Roles srp)
    {
        Task<Role[]> task = Task.Run(() => server.GetRoles());
        task.Wait();
        Role[] roles_pre = task.Result;
        var roles = roles_pre.OrderByDescending(s => s.Index);
        base.Size = new(srp.Size.X, srp.Size.Y - srp.TitleLable.Size.Y);

        RoleFlow = new()
        {
            Size = new(247.ScaleInt(), base.Size.Y)
        };
        Rectangle hline = new()
        {
            Location = new(RoleFlow.Size.X, 0),
            Size = new(1.ScaleInt(), base.Size.Y),
            Anchor = ObjectAnchor.Left | ObjectAnchor.Top | ObjectAnchor.Bottom,
            BackgroundColor = Color4.White
        };
        hline.ForceDistanceUpdate(this);
        Controls.Add(hline);
        SRO = new()
        {
            //BackgroundColor = base.BackgroundColor,
            Size = new(srp.Size.X - RoleFlow.Size.X, base.Size.Y),
            Location = new(RoleFlow.Size.X + hline.Size.X, 0),
            Anchor = ObjectAnchor.All
        };
        SRO.ForceDistanceUpdate(this);
        Controls.Add(SRO);
        
        foreach (var role in roles)
        {
            Selected = new RoleIcon(role, this);
            RoleFlow.Controls.Add(Selected);
            Selected.ForceDistanceUpdate(RoleFlow);
        }
        Selected!.SetSelected(true);
        var space = 10.ScaleInt();
        Controls.Add(RoleFlow);
        Warning = new()
        {
            Visible = false
        };
        Warning.Size = new(base.Size.X - space - space, Warning.Size.Y);
        Warning.Location = new(space, base.Size.Y - Warning.Size.Y - space);
        Warning.ForceDistanceUpdate(this);
        Controls.Add(Warning);
        SRO.Init(Warning);
        SRO.ForceDistanceUpdate(this);
    }
    
    public SaveWarning Warning; 

    public override Color4 BackgroundColor
    {
        get
        {
            return base.BackgroundColor;
        }
        set
        {
            base.BackgroundColor = value;
            RoleFlow.BackgroundColor = value;
            SRO.BackgroundColor = value;
            Selected.SetSelected(true);
        }
    }
}