using VoiceCat.App.Models;

namespace VoiceCat.App.Forms;

/// <summary>Add or edit a saved server entry. DialogResult.OK -> read Result.</summary>
public partial class AddServerDialog : Form
{
    private readonly SavedServer _editing;
    private bool _passwordChanged;

    public SavedServer? Result { get; private set; }

    public AddServerDialog(SavedServer? existing = null)
    {
        InitializeComponent();
        _editing = existing ?? new SavedServer();

        txtDisplayName.Text = _editing.DisplayName;
        txtHost.Text = _editing.Host;
        numPort.Value = _editing.Port == 0 ? 8384 : _editing.Port;
        txtNickname.Text = _editing.LastNickname;
        radioGuest.Checked = _editing.AuthMode == AuthMode.Guest;
        radioPassword.Checked = _editing.AuthMode == AuthMode.Password;
        txtUsername.Text = _editing.SavedUsername ?? "";
        chkRememberPassword.Checked = _editing.ProtectedPasswordBase64 is not null;
        if (_editing.ProtectedPasswordBase64 is not null)
            txtPassword.Text = "********"; // placeholder — never decrypt-and-show; re-typing replaces it

        UpdateAuthFieldsEnabled();
        radioGuest.CheckedChanged += (_, _) => UpdateAuthFieldsEnabled();
        radioPassword.CheckedChanged += (_, _) => UpdateAuthFieldsEnabled();
        txtPassword.TextChanged += (_, _) => _passwordChanged = true;

        btnOk.Click += BtnOk_Click;
    }

    private void UpdateAuthFieldsEnabled()
    {
        bool password = radioPassword.Checked;
        txtNickname.Enabled = !password;
        txtUsername.Enabled = password;
        txtPassword.Enabled = password;
        chkRememberPassword.Enabled = password;
    }

    private void BtnOk_Click(object? sender, EventArgs e)
    {
        if (string.IsNullOrWhiteSpace(txtHost.Text))
        {
            MessageBox.Show(this, "Host is required.", "VoiceCat", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            DialogResult = DialogResult.None;
            return;
        }

        _editing.DisplayName = txtDisplayName.Text.Trim();
        _editing.Host = txtHost.Text.Trim();
        _editing.Port = (ushort)numPort.Value;
        _editing.AuthMode = radioPassword.Checked ? AuthMode.Password : AuthMode.Guest;

        if (_editing.AuthMode == AuthMode.Guest)
        {
            _editing.LastNickname = txtNickname.Text.Trim();
            _editing.SavedUsername = null;
            _editing.ProtectedPasswordBase64 = null;
        }
        else
        {
            _editing.SavedUsername = txtUsername.Text.Trim();
            if (chkRememberPassword.Checked && _passwordChanged && txtPassword.Text.Length > 0)
                _editing.ProtectedPasswordBase64 = PasswordProtector.Protect(txtPassword.Text);
            else if (!chkRememberPassword.Checked)
                _editing.ProtectedPasswordBase64 = null;
            // else: remember-password still checked and password box untouched (still shows
            // the placeholder) — keep whatever was already protected/stored.
        }

        Result = _editing;
        DialogResult = DialogResult.OK;
    }
}
