using VoiceCat.Interop;

namespace VoiceCat.App.Forms;

/// <summary>
/// Edit a user's server permissions.
/// </summary>
public sealed class PermissionsDialog : Form
{
    private readonly CheckBox _chkCreateTemp;
    private readonly CheckBox _chkKick;
    private readonly CheckBox _chkBan;
    private readonly CheckBox _chkMove;
    private readonly CheckBox _chkAdminAccounts;
    private readonly CheckBox _chkIsAdmin;

    public PermissionsInfo? Result { get; private set; }

    public PermissionsDialog(string nickname, PermissionsInfo current)
    {
        Text = $"Permissions — {nickname}";
        FormBorderStyle = FormBorderStyle.FixedDialog;
        MaximizeBox = false;
        MinimizeBox = false;
        StartPosition = FormStartPosition.CenterParent;
        AutoScaleMode = AutoScaleMode.Font;
        ClientSize = new Size(320, 260);

        var lbl = new Label
        {
            Text = $"Set permissions for {nickname}:",
            Location = new Point(12, 12),
            AutoSize = true,
        };

        _chkCreateTemp = new CheckBox
        {
            Text = "Can create &temporary channels",
            Location = new Point(12, 38),
            AutoSize = true,
            Checked = current.CanCreateTempChannel,
        };
        _chkKick = new CheckBox
        {
            Text = "Can &kick users",
            Location = new Point(12, 64),
            AutoSize = true,
            Checked = current.CanKick,
        };
        _chkBan = new CheckBox
        {
            Text = "Can &ban users",
            Location = new Point(12, 90),
            AutoSize = true,
            Checked = current.CanBan,
        };
        _chkMove = new CheckBox
        {
            Text = "Can &move users",
            Location = new Point(12, 116),
            AutoSize = true,
            Checked = current.CanMoveUsers,
        };
        _chkAdminAccounts = new CheckBox
        {
            Text = "Can manage &accounts",
            Location = new Point(12, 142),
            AutoSize = true,
            Checked = current.CanAdminAccounts,
        };
        _chkIsAdmin = new CheckBox
        {
            Text = "Is &admin",
            Location = new Point(12, 168),
            AutoSize = true,
            Checked = current.IsAdmin,
        };

        var btnOk = new Button
        {
            Text = "&OK",
            DialogResult = DialogResult.OK,
            Location = new Point(152, 210),
            Size = new Size(75, 27),
        };
        var btnCancel = new Button
        {
            Text = "&Cancel",
            DialogResult = DialogResult.Cancel,
            Location = new Point(233, 210),
            Size = new Size(75, 27),
        };

        AcceptButton = btnOk;
        CancelButton = btnCancel;
        Controls.AddRange([lbl, _chkCreateTemp, _chkKick, _chkBan, _chkMove,
                           _chkAdminAccounts, _chkIsAdmin, btnOk, btnCancel]);

        btnOk.Click += (_, _) =>
        {
            Result = new PermissionsInfo(
                CanCreateTempChannel: _chkCreateTemp.Checked,
                CanKick: _chkKick.Checked,
                CanBan: _chkBan.Checked,
                CanMoveUsers: _chkMove.Checked,
                CanAdminAccounts: _chkAdminAccounts.Checked,
                IsAdmin: _chkIsAdmin.Checked);
        };
    }
}
