namespace ClawdDotNet;

public sealed partial class frm_AddService : Form
{
    public string ServiceName => txtName.Text.Trim();
    public string ServiceType => cbType.SelectedItem?.ToString() ?? "Custom";
    public int ServicePort => int.TryParse(txtPort.Text, out var p) ? p : 0;
    public string ServiceDescription => txtDescription.Text.Trim();

    public frm_AddService()
    {
        InitializeComponent();

        cbType.SelectedIndex = 0;
        OnTypeChanged(this, EventArgs.Empty);
    }

    private void OnTypeChanged(object? sender, EventArgs e)
    {
        var type = cbType.SelectedItem?.ToString() ?? "";
        if (string.IsNullOrWhiteSpace(txtName.Text) || txtName.Text.StartsWith("Neuer "))
            txtName.Text = $"Neuer {type}";

        txtDescription.Text = type switch
        {
            "StaticFileServer" => "Statischer Datei-Server",
            "ReverseProxy" => "Reverse-Proxy zu einem externen Service",
            "Custom" => "Benutzerdefinierter Service",
            _ => ""
        };
    }

    private void OnOkClick(object? sender, EventArgs e)
    {
        if (string.IsNullOrWhiteSpace(txtName.Text))
        {
            MessageBox.Show("Bitte einen Namen eingeben.", "Validierung",
                MessageBoxButtons.OK, MessageBoxIcon.Warning);
            DialogResult = DialogResult.None;
            return;
        }

        if (!int.TryParse(txtPort.Text, out var port) || port < 1 || port > 65535)
        {
            MessageBox.Show("Bitte einen gültigen Port (1-65535) eingeben.", "Validierung",
                MessageBoxButtons.OK, MessageBoxIcon.Warning);
            DialogResult = DialogResult.None;
        }
    }
}
