"""Builder for radar settings section."""

from __future__ import annotations

from PyQt6.QtWidgets import QComboBox, QFormLayout, QGroupBox, QLabel, QLineEdit


def build_radar_group(owner) -> QGroupBox:
    """Create radar settings controls and labels."""
    group = QGroupBox("Radar")
    form = QFormLayout(group)
    form.setFieldGrowthPolicy(QFormLayout.FieldGrowthPolicy.AllNonFixedFieldsGrow)
    defaults = owner._defaults_config.radar

    owner._serial_input = QLineEdit(defaults.serial)
    owner._serial_input.setPlaceholderText("Optional: empty = auto-detect first LibreVNA")

    owner._radar_mode = QComboBox()
    owner._radar_mode.addItems(["mock", "native"])
    owner._radar_mode.setToolTip("mock: synthetic signal, native: real LibreVNA hardware")
    owner._set_combo_current_text(owner._radar_mode, defaults.driver_mode)

    owner._start_hz_input = QLineEdit(f"{defaults.sweep.start_hz:g}")
    owner._stop_hz_input = QLineEdit(f"{defaults.sweep.stop_hz:g}")
    owner._points_input = QLineEdit(str(defaults.sweep.points))
    owner._ifbw_input = QLineEdit(f"{defaults.sweep.if_bandwidth_hz:g}")
    owner._power_input = QLineEdit(f"{defaults.sweep.power_dbm:g}")
    owner._power_input.setToolTip("Device power limits are available only in native mode.")
    owner._serial_input.editingFinished.connect(owner._on_radar_identity_changed)
    owner._radar_mode.currentTextChanged.connect(owner._on_radar_identity_changed)
    owner._start_hz_input.editingFinished.connect(owner._on_radar_sweep_limits_changed)
    owner._stop_hz_input.editingFinished.connect(owner._on_radar_sweep_limits_changed)

    owner._radar_start_label = QLabel("Start Hz")
    owner._radar_stop_label = QLabel("Stop Hz")
    owner._radar_points_label = QLabel("Points")
    owner._radar_ifbw_label = QLabel("IF BW Hz")
    owner._radar_power_label = QLabel("Stimulus Power dBm")

    owner._radar_limits_hint = QLabel("Mock mode: device limits are not applied.")
    owner._radar_limits_hint.setObjectName("hintLabel")

    form.addRow("Serial", owner._serial_input)
    form.addRow("Mode", owner._radar_mode)
    form.addRow(owner._radar_start_label, owner._start_hz_input)
    form.addRow(owner._radar_stop_label, owner._stop_hz_input)
    form.addRow(owner._radar_points_label, owner._points_input)
    form.addRow(owner._radar_ifbw_label, owner._ifbw_input)
    form.addRow(owner._radar_power_label, owner._power_input)
    form.addRow(owner._radar_limits_hint)
    return group
