"""Builder for processing mode and live-parameter section."""

from __future__ import annotations

from PyQt6.QtWidgets import (
    QCheckBox,
    QComboBox,
    QDoubleSpinBox,
    QFormLayout,
    QGroupBox,
    QLineEdit,
    QPlainTextEdit,
    QSizePolicy,
    QSpinBox,
    QStackedWidget,
    QVBoxLayout,
    QWidget,
)

from python_app.gui.controllers.sections.layout_helpers import FormRow, build_two_column_form_widget


def _format_geometry_row(position: int, x_m: float, y_m: float, z_m: float) -> str:
    """Render one geometry row, trimming trailing zero y/z so 1D layouts stay compact."""
    if z_m != 0.0:
        return f"{position} {x_m:g} {y_m:g} {z_m:g}"
    if y_m != 0.0:
        return f"{position} {x_m:g} {y_m:g}"
    return f"{position} {x_m:g}"


def _format_tx_geometry(owner) -> str:
    """Render Tx geometry defaults into editable line-based text."""
    return "\n".join(
        _format_geometry_row(int(entry.output_pos), float(entry.x_m), float(entry.y_m), float(entry.z_m))
        for entry in owner._defaults_config.gpr.tx_geometry
    )


def _format_rx_geometry(owner) -> str:
    """Render Rx geometry defaults into editable line-based text."""
    return "\n".join(
        _format_geometry_row(int(entry.input_pos), float(entry.x_m), float(entry.y_m), float(entry.z_m))
        for entry in owner._defaults_config.gpr.rx_geometry
    )


def _build_processing_mode_page(parent: QStackedWidget, rows: list[FormRow], *, split_index: int | None = None) -> QWidget:
    """Create one processing-mode page with a two-column form body."""
    page = QWidget(parent)
    page.setSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.Fixed)
    page_layout = QVBoxLayout(page)
    page_layout.setContentsMargins(0, 0, 0, 0)
    page_layout.setSpacing(0)
    page_layout.addWidget(build_two_column_form_widget(page, rows, split_index=split_index))
    return page


def build_processing_group(owner) -> QGroupBox:
    """Create processing mode section with pass-through, B-scan, and GPR pages."""
    group = QGroupBox("Processing")
    form = QFormLayout(group)
    form.setFieldGrowthPolicy(QFormLayout.FieldGrowthPolicy.AllNonFixedFieldsGrow)
    processing_defaults = owner._gui_defaults.processing
    pass_defaults = processing_defaults.pass_through
    bscan_defaults = processing_defaults.bscan
    gpr_live_defaults = processing_defaults.gpr
    legacy_gpr_defaults = processing_defaults.legacy_gpr

    owner._processing_mode = QComboBox()
    owner._processing_mode.addItems(["pass_through", "bscan", "gpr", "legacy_gpr"])
    owner._set_combo_current_text(owner._processing_mode, processing_defaults.selected_mode)

    owner._processing_mode_pages = QStackedWidget(group)
    owner._processing_mode_pages.setSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.Fixed)

    owner._show_magnitude_checkbox = QCheckBox("Show magnitude")
    owner._show_magnitude_checkbox.setChecked(bool(pass_defaults.show_magnitude))

    owner._show_phase_checkbox = QCheckBox("Show phase")
    owner._show_phase_checkbox.setChecked(bool(pass_defaults.show_phase))

    owner._pass_through_combo_filter_input = QLineEdit(str(pass_defaults.combo_filter))
    owner._pass_through_combo_filter_input.setPlaceholderText("empty = all, e.g. 0:0,1:0")

    owner._pass_through_fixed_y_enabled = QCheckBox("Fix magnitude Y range")
    owner._pass_through_fixed_y_enabled.setChecked(bool(pass_defaults.fixed_y_enabled))

    owner._pass_through_y_min_db = QDoubleSpinBox()
    owner._pass_through_y_min_db.setDecimals(1)
    owner._pass_through_y_min_db.setRange(-240.0, 240.0)
    owner._pass_through_y_min_db.setSingleStep(1.0)
    owner._pass_through_y_min_db.setValue(float(pass_defaults.y_min_db))

    owner._pass_through_y_max_db = QDoubleSpinBox()
    owner._pass_through_y_max_db.setDecimals(1)
    owner._pass_through_y_max_db.setRange(-240.0, 240.0)
    owner._pass_through_y_max_db.setSingleStep(1.0)
    owner._pass_through_y_max_db.setValue(float(pass_defaults.y_max_db))

    owner._sync_pass_through_y_controls()

    pass_through_page = _build_processing_mode_page(
        owner._processing_mode_pages,
        [
            owner._show_magnitude_checkbox,
            owner._show_phase_checkbox,
            ("Switch combos", owner._pass_through_combo_filter_input),
            owner._pass_through_fixed_y_enabled,
            ("Y min dB", owner._pass_through_y_min_db),
            ("Y max dB", owner._pass_through_y_max_db),
        ],
        split_index=4,
    )
    owner._processing_mode_pages.addWidget(pass_through_page)

    owner._bscan_axis = QComboBox()
    owner._bscan_axis.addItems(["abs", "real", "phase"])
    owner._set_combo_current_text(owner._bscan_axis, bscan_defaults.axis)

    owner._bscan_cut_m = QDoubleSpinBox()
    owner._bscan_cut_m.setDecimals(3)
    owner._bscan_cut_m.setRange(0.0, 2.0)
    owner._bscan_cut_m.setSingleStep(0.001)
    owner._bscan_cut_m.setValue(float(bscan_defaults.cut_m))

    owner._bscan_max_depth_m = QDoubleSpinBox()
    owner._bscan_max_depth_m.setDecimals(1)
    owner._bscan_max_depth_m.setRange(0.1, 20.0)
    owner._bscan_max_depth_m.setSingleStep(0.1)
    owner._bscan_max_depth_m.setValue(float(bscan_defaults.max_depth_m))

    owner._bscan_gain = QDoubleSpinBox()
    owner._bscan_gain.setDecimals(1)
    owner._bscan_gain.setRange(0.0, 3.0)
    owner._bscan_gain.setSingleStep(0.1)
    owner._bscan_gain.setValue(float(bscan_defaults.gain))

    owner._bscan_start_freq_mhz = QDoubleSpinBox()
    owner._bscan_start_freq_mhz.setDecimals(1)
    owner._bscan_start_freq_mhz.setRange(100.0, 8800.0)
    owner._bscan_start_freq_mhz.setSingleStep(10.0)
    owner._bscan_start_freq_mhz.setValue(float(bscan_defaults.start_freq_mhz))

    owner._bscan_stop_freq_mhz = QDoubleSpinBox()
    owner._bscan_stop_freq_mhz.setDecimals(1)
    owner._bscan_stop_freq_mhz.setRange(100.0, 8800.0)
    owner._bscan_stop_freq_mhz.setSingleStep(10.0)
    owner._bscan_stop_freq_mhz.setValue(float(bscan_defaults.stop_freq_mhz))

    owner._bscan_subtract_mean_ascan = QCheckBox("Subtract mean A-scan")
    owner._bscan_subtract_mean_ascan.setChecked(bool(bscan_defaults.subtract_mean_ascan))

    bscan_page = _build_processing_mode_page(
        owner._processing_mode_pages,
        [
            ("Axis", owner._bscan_axis),
            ("Cut m", owner._bscan_cut_m),
            ("Max depth m", owner._bscan_max_depth_m),
            ("Gain", owner._bscan_gain),
            ("Start MHz", owner._bscan_start_freq_mhz),
            ("Stop MHz", owner._bscan_stop_freq_mhz),
            owner._bscan_subtract_mean_ascan,
        ],
        split_index=4,
    )
    owner._processing_mode_pages.addWidget(bscan_page)

    gpr_defaults = owner._defaults_config.gpr

    owner._gpr_relative_permittivity = QDoubleSpinBox()
    owner._gpr_relative_permittivity.setDecimals(4)
    owner._gpr_relative_permittivity.setRange(0.0001, 1000.0)
    owner._gpr_relative_permittivity.setSingleStep(0.05)
    owner._gpr_relative_permittivity.setValue(float(gpr_defaults.relative_permittivity))

    owner._gpr_tx_geometry_input = QPlainTextEdit(_format_tx_geometry(owner))
    owner._gpr_tx_geometry_input.setPlaceholderText("output_pos x_m [y_m] [z_m]")
    owner._gpr_tx_geometry_input.setFixedHeight(78)

    owner._gpr_rx_geometry_input = QPlainTextEdit(_format_rx_geometry(owner))
    owner._gpr_rx_geometry_input.setPlaceholderText("input_pos x_m [y_m] [z_m]")
    owner._gpr_rx_geometry_input.setFixedHeight(78)

    owner._gpr_common_page = _build_processing_mode_page(
        group,
        [
            ("Relative permittivity", owner._gpr_relative_permittivity),
            ("Tx geometry", owner._gpr_tx_geometry_input),
            ("Rx geometry", owner._gpr_rx_geometry_input),
        ],
        split_index=1,
    )

    owner._gpr_input_positions_input = QLineEdit(str(gpr_live_defaults.input_positions))
    owner._gpr_input_positions_input.setPlaceholderText("0,1,2")

    owner._gpr_output_positions_input = QLineEdit(str(gpr_live_defaults.output_positions))
    owner._gpr_output_positions_input.setPlaceholderText("0,1")

    owner._gpr_min_depth_m = QDoubleSpinBox()
    owner._gpr_min_depth_m.setDecimals(2)
    owner._gpr_min_depth_m.setRange(0.0, 50.0)
    owner._gpr_min_depth_m.setSingleStep(0.1)
    owner._gpr_min_depth_m.setValue(float(gpr_live_defaults.min_depth_m))

    owner._gpr_max_depth_m = QDoubleSpinBox()
    owner._gpr_max_depth_m.setDecimals(2)
    owner._gpr_max_depth_m.setRange(0.1, 50.0)
    owner._gpr_max_depth_m.setSingleStep(0.1)
    owner._gpr_max_depth_m.setValue(float(gpr_live_defaults.max_depth_m))

    owner._gpr_range_comp_power = QDoubleSpinBox()
    owner._gpr_range_comp_power.setDecimals(3)
    owner._gpr_range_comp_power.setRange(0.0, 5.0)
    owner._gpr_range_comp_power.setSingleStep(0.01)
    owner._gpr_range_comp_power.setValue(float(gpr_live_defaults.range_comp_power))

    owner._gpr_angle_comp_power = QDoubleSpinBox()
    owner._gpr_angle_comp_power.setDecimals(3)
    owner._gpr_angle_comp_power.setRange(0.0, 5.0)
    owner._gpr_angle_comp_power.setSingleStep(0.01)
    owner._gpr_angle_comp_power.setValue(float(gpr_live_defaults.angle_comp_power))

    owner._gpr_score_mode = QComboBox()
    owner._gpr_score_mode.addItems(["peak", "combined"])
    owner._set_combo_current_text(owner._gpr_score_mode, gpr_live_defaults.score_mode)

    owner._gpr_max_detected_objects_to_draw = QSpinBox()
    owner._gpr_max_detected_objects_to_draw.setRange(0, 10_000)
    owner._gpr_max_detected_objects_to_draw.setValue(int(gpr_live_defaults.max_detected_objects_to_draw))

    owner._gpr_draw_top_m_objects = QSpinBox()
    owner._gpr_draw_top_m_objects.setRange(0, 10_000)
    owner._gpr_draw_top_m_objects.setValue(int(gpr_live_defaults.draw_top_m_objects))

    owner._gpr_start_freq_mhz = QDoubleSpinBox()
    owner._gpr_start_freq_mhz.setDecimals(1)
    owner._gpr_start_freq_mhz.setRange(100.0, 8800.0)
    owner._gpr_start_freq_mhz.setSingleStep(10.0)
    owner._gpr_start_freq_mhz.setValue(float(gpr_live_defaults.start_freq_mhz))

    owner._gpr_stop_freq_mhz = QDoubleSpinBox()
    owner._gpr_stop_freq_mhz.setDecimals(1)
    owner._gpr_stop_freq_mhz.setRange(100.0, 8800.0)
    owner._gpr_stop_freq_mhz.setSingleStep(10.0)
    owner._gpr_stop_freq_mhz.setValue(float(gpr_live_defaults.stop_freq_mhz))

    owner._gpr_background_subtract_enabled = QCheckBox("Subtract mean of previous collections")
    owner._gpr_background_subtract_enabled.setChecked(bool(gpr_live_defaults.background_subtract_enabled))

    owner._gpr_background_mean_count = QSpinBox()
    owner._gpr_background_mean_count.setRange(0, 10_000)
    owner._gpr_background_mean_count.setValue(int(gpr_live_defaults.background_mean_count))

    owner._gpr_remove_sidelobe_objects_enabled = QCheckBox("Remove sidelobe objects")
    owner._gpr_remove_sidelobe_objects_enabled.setChecked(bool(gpr_live_defaults.remove_sidelobe_objects_enabled))

    owner._gpr_render_mode = QComboBox()
    owner._gpr_render_mode.addItems(["heatmap", "objects_only"])
    owner._set_combo_current_text(owner._gpr_render_mode, gpr_live_defaults.render_mode)

    owner._gpr_min_visible_score = QDoubleSpinBox()
    owner._gpr_min_visible_score.setDecimals(2)
    owner._gpr_min_visible_score.setRange(0.0, 1.0)
    owner._gpr_min_visible_score.setSingleStep(0.05)
    owner._gpr_min_visible_score.setValue(float(gpr_live_defaults.min_visible_score))

    owner._gpr_visible_x_min_m = QDoubleSpinBox()
    owner._gpr_visible_x_min_m.setDecimals(2)
    owner._gpr_visible_x_min_m.setRange(-100.0, 100.0)
    owner._gpr_visible_x_min_m.setSingleStep(0.1)
    owner._gpr_visible_x_min_m.setValue(float(gpr_live_defaults.visible_x_min_m))

    owner._gpr_visible_x_max_m = QDoubleSpinBox()
    owner._gpr_visible_x_max_m.setDecimals(2)
    owner._gpr_visible_x_max_m.setRange(-100.0, 100.0)
    owner._gpr_visible_x_max_m.setSingleStep(0.1)
    owner._gpr_visible_x_max_m.setValue(float(gpr_live_defaults.visible_x_max_m))

    owner._gpr_visible_z_min_m = QDoubleSpinBox()
    owner._gpr_visible_z_min_m.setDecimals(2)
    owner._gpr_visible_z_min_m.setRange(0.0, 100.0)
    owner._gpr_visible_z_min_m.setSingleStep(0.1)
    owner._gpr_visible_z_min_m.setValue(float(gpr_live_defaults.visible_z_min_m))

    owner._gpr_visible_z_max_m = QDoubleSpinBox()
    owner._gpr_visible_z_max_m.setDecimals(2)
    owner._gpr_visible_z_max_m.setRange(0.0, 100.0)
    owner._gpr_visible_z_max_m.setSingleStep(0.1)
    owner._gpr_visible_z_max_m.setValue(float(gpr_live_defaults.visible_z_max_m))

    owner._gpr_imaging_plane_y_m = QDoubleSpinBox()
    owner._gpr_imaging_plane_y_m.setDecimals(3)
    owner._gpr_imaging_plane_y_m.setRange(-50.0, 50.0)
    owner._gpr_imaging_plane_y_m.setSingleStep(0.05)
    owner._gpr_imaging_plane_y_m.setValue(float(gpr_live_defaults.imaging_plane_y_m))
    owner._gpr_imaging_plane_y_m.setToolTip(
        "Y coordinate of the BP imaging slice (m). Use 0 for legacy 1D antenna layouts."
    )

    gpr_page = _build_processing_mode_page(
        owner._processing_mode_pages,
        [
            ("Input positions", owner._gpr_input_positions_input),
            ("Output positions", owner._gpr_output_positions_input),
            ("Min depth m", owner._gpr_min_depth_m),
            ("Max depth m", owner._gpr_max_depth_m),
            ("Range comp power", owner._gpr_range_comp_power),
            ("Angle comp power", owner._gpr_angle_comp_power),
            ("Score mode", owner._gpr_score_mode),
            ("Render mode", owner._gpr_render_mode),
            ("Min visible score", owner._gpr_min_visible_score),
            ("Max detected objects", owner._gpr_max_detected_objects_to_draw),
            ("Draw top M objects", owner._gpr_draw_top_m_objects),
            ("Start MHz", owner._gpr_start_freq_mhz),
            ("Stop MHz", owner._gpr_stop_freq_mhz),
            ("Imaging plane Y m", owner._gpr_imaging_plane_y_m),
            ("Visible X min m", owner._gpr_visible_x_min_m),
            ("Visible X max m", owner._gpr_visible_x_max_m),
            ("Visible Z min m", owner._gpr_visible_z_min_m),
            ("Visible Z max m", owner._gpr_visible_z_max_m),
            owner._gpr_background_subtract_enabled,
            ("Mean count", owner._gpr_background_mean_count),
            owner._gpr_remove_sidelobe_objects_enabled,
        ],
        split_index=8,
    )
    owner._processing_mode_pages.addWidget(gpr_page)

    owner._legacy_gpr_config_mode = QComboBox()
    owner._legacy_gpr_config_mode.addItems(["point", "extended"])
    owner._set_combo_current_text(owner._legacy_gpr_config_mode, legacy_gpr_defaults.mode)

    owner._legacy_gpr_input_positions_input = QLineEdit(str(legacy_gpr_defaults.input_positions))
    owner._legacy_gpr_input_positions_input.setPlaceholderText("0,1,2")

    owner._legacy_gpr_output_positions_input = QLineEdit(str(legacy_gpr_defaults.output_positions))
    owner._legacy_gpr_output_positions_input.setPlaceholderText("0,1")

    owner._legacy_gpr_min_depth_m = QDoubleSpinBox()
    owner._legacy_gpr_min_depth_m.setDecimals(2)
    owner._legacy_gpr_min_depth_m.setRange(0.0, 50.0)
    owner._legacy_gpr_min_depth_m.setSingleStep(0.1)
    owner._legacy_gpr_min_depth_m.setValue(float(legacy_gpr_defaults.min_depth_m))

    owner._legacy_gpr_max_depth_m = QDoubleSpinBox()
    owner._legacy_gpr_max_depth_m.setDecimals(2)
    owner._legacy_gpr_max_depth_m.setRange(0.1, 50.0)
    owner._legacy_gpr_max_depth_m.setSingleStep(0.1)
    owner._legacy_gpr_max_depth_m.setValue(float(legacy_gpr_defaults.max_depth_m))

    owner._legacy_gpr_comp_power = QDoubleSpinBox()
    owner._legacy_gpr_comp_power.setDecimals(3)
    owner._legacy_gpr_comp_power.setRange(0.0, 5.0)
    owner._legacy_gpr_comp_power.setSingleStep(0.05)
    owner._legacy_gpr_comp_power.setValue(float(legacy_gpr_defaults.comp_power))

    owner._legacy_gpr_snr_thresh = QDoubleSpinBox()
    owner._legacy_gpr_snr_thresh.setDecimals(2)
    owner._legacy_gpr_snr_thresh.setRange(0.0, 1_000.0)
    owner._legacy_gpr_snr_thresh.setSingleStep(0.1)
    owner._legacy_gpr_snr_thresh.setValue(float(legacy_gpr_defaults.snr_thresh))

    owner._legacy_gpr_snr_comp_max = QDoubleSpinBox()
    owner._legacy_gpr_snr_comp_max.setDecimals(2)
    owner._legacy_gpr_snr_comp_max.setRange(0.0, 1_000.0)
    owner._legacy_gpr_snr_comp_max.setSingleStep(0.5)
    owner._legacy_gpr_snr_comp_max.setValue(float(legacy_gpr_defaults.snr_comp_max))

    owner._legacy_gpr_start_freq_mhz = QDoubleSpinBox()
    owner._legacy_gpr_start_freq_mhz.setDecimals(1)
    owner._legacy_gpr_start_freq_mhz.setRange(100.0, 8800.0)
    owner._legacy_gpr_start_freq_mhz.setSingleStep(10.0)
    owner._legacy_gpr_start_freq_mhz.setValue(float(legacy_gpr_defaults.start_freq_mhz))

    owner._legacy_gpr_stop_freq_mhz = QDoubleSpinBox()
    owner._legacy_gpr_stop_freq_mhz.setDecimals(1)
    owner._legacy_gpr_stop_freq_mhz.setRange(100.0, 8800.0)
    owner._legacy_gpr_stop_freq_mhz.setSingleStep(10.0)
    owner._legacy_gpr_stop_freq_mhz.setValue(float(legacy_gpr_defaults.stop_freq_mhz))

    owner._legacy_gpr_speed_m_s = QDoubleSpinBox()
    owner._legacy_gpr_speed_m_s.setDecimals(3)
    owner._legacy_gpr_speed_m_s.setRange(-100.0, 100.0)
    owner._legacy_gpr_speed_m_s.setSingleStep(0.01)
    owner._legacy_gpr_speed_m_s.setValue(float(legacy_gpr_defaults.speed_m_s))

    owner._legacy_gpr_ignore_socket_speed_enabled = QCheckBox("Ignore socket speed")
    owner._legacy_gpr_ignore_socket_speed_enabled.setChecked(
        bool(legacy_gpr_defaults.ignore_socket_speed_enabled)
    )

    owner._legacy_gpr_look_angle_deg = QDoubleSpinBox()
    owner._legacy_gpr_look_angle_deg.setDecimals(2)
    owner._legacy_gpr_look_angle_deg.setRange(-90.0, 90.0)
    owner._legacy_gpr_look_angle_deg.setSingleStep(0.1)
    owner._legacy_gpr_look_angle_deg.setValue(float(legacy_gpr_defaults.look_angle_deg))

    owner._legacy_gpr_background_subtract_enabled = QCheckBox("Subtract mean of previous collections")
    owner._legacy_gpr_background_subtract_enabled.setChecked(bool(legacy_gpr_defaults.background_subtract_enabled))

    owner._legacy_gpr_background_mean_count = QSpinBox()
    owner._legacy_gpr_background_mean_count.setRange(0, 10_000)
    owner._legacy_gpr_background_mean_count.setValue(int(legacy_gpr_defaults.background_mean_count))

    owner._legacy_gpr_render_mode = QComboBox()
    owner._legacy_gpr_render_mode.addItems(["heatmap", "objects_only"])
    owner._set_combo_current_text(owner._legacy_gpr_render_mode, legacy_gpr_defaults.render_mode)

    owner._legacy_gpr_min_visible_pair_count = QSpinBox()
    owner._legacy_gpr_min_visible_pair_count.setRange(1, 10_000)
    owner._legacy_gpr_min_visible_pair_count.setValue(int(legacy_gpr_defaults.min_visible_pair_count))

    owner._legacy_gpr_visible_x_min_m = QDoubleSpinBox()
    owner._legacy_gpr_visible_x_min_m.setDecimals(2)
    owner._legacy_gpr_visible_x_min_m.setRange(-100.0, 100.0)
    owner._legacy_gpr_visible_x_min_m.setSingleStep(0.1)
    owner._legacy_gpr_visible_x_min_m.setValue(float(legacy_gpr_defaults.visible_x_min_m))

    owner._legacy_gpr_visible_x_max_m = QDoubleSpinBox()
    owner._legacy_gpr_visible_x_max_m.setDecimals(2)
    owner._legacy_gpr_visible_x_max_m.setRange(-100.0, 100.0)
    owner._legacy_gpr_visible_x_max_m.setSingleStep(0.1)
    owner._legacy_gpr_visible_x_max_m.setValue(float(legacy_gpr_defaults.visible_x_max_m))

    owner._legacy_gpr_visible_z_min_m = QDoubleSpinBox()
    owner._legacy_gpr_visible_z_min_m.setDecimals(2)
    owner._legacy_gpr_visible_z_min_m.setRange(0.0, 100.0)
    owner._legacy_gpr_visible_z_min_m.setSingleStep(0.1)
    owner._legacy_gpr_visible_z_min_m.setValue(float(legacy_gpr_defaults.visible_z_min_m))

    owner._legacy_gpr_visible_z_max_m = QDoubleSpinBox()
    owner._legacy_gpr_visible_z_max_m.setDecimals(2)
    owner._legacy_gpr_visible_z_max_m.setRange(0.0, 100.0)
    owner._legacy_gpr_visible_z_max_m.setSingleStep(0.1)
    owner._legacy_gpr_visible_z_max_m.setValue(float(legacy_gpr_defaults.visible_z_max_m))

    legacy_gpr_page = _build_processing_mode_page(
        owner._processing_mode_pages,
        [
            ("Config mode", owner._legacy_gpr_config_mode),
            ("Input positions", owner._legacy_gpr_input_positions_input),
            ("Output positions", owner._legacy_gpr_output_positions_input),
            ("Min depth m", owner._legacy_gpr_min_depth_m),
            ("Max depth m", owner._legacy_gpr_max_depth_m),
            ("Comp power", owner._legacy_gpr_comp_power),
            ("SNR thresh", owner._legacy_gpr_snr_thresh),
            ("SNR comp max", owner._legacy_gpr_snr_comp_max),
            ("Speed m/s", owner._legacy_gpr_speed_m_s),
            owner._legacy_gpr_ignore_socket_speed_enabled,
            ("Render mode", owner._legacy_gpr_render_mode),
            ("Min visible pairs", owner._legacy_gpr_min_visible_pair_count),
            ("Start MHz", owner._legacy_gpr_start_freq_mhz),
            ("Stop MHz", owner._legacy_gpr_stop_freq_mhz),
            ("Look angle deg", owner._legacy_gpr_look_angle_deg),
            ("Visible X min m", owner._legacy_gpr_visible_x_min_m),
            ("Visible X max m", owner._legacy_gpr_visible_x_max_m),
            ("Visible Z min m", owner._legacy_gpr_visible_z_min_m),
            ("Visible Z max m", owner._legacy_gpr_visible_z_max_m),
            owner._legacy_gpr_background_subtract_enabled,
            ("Mean count", owner._legacy_gpr_background_mean_count),
        ],
        split_index=10,
    )
    owner._processing_mode_pages.addWidget(legacy_gpr_page)

    owner._processing_mode.currentTextChanged.connect(owner._on_processing_mode_changed)
    owner._show_magnitude_checkbox.toggled.connect(owner._on_trace_visibility_changed)
    owner._show_phase_checkbox.toggled.connect(owner._on_trace_visibility_changed)
    owner._pass_through_combo_filter_input.editingFinished.connect(owner._on_trace_visibility_changed)
    owner._pass_through_fixed_y_enabled.toggled.connect(owner._sync_pass_through_y_controls)
    owner._pass_through_fixed_y_enabled.toggled.connect(owner._on_processing_live_settings_changed)
    owner._pass_through_y_min_db.valueChanged.connect(owner._on_processing_live_settings_changed)
    owner._pass_through_y_max_db.valueChanged.connect(owner._on_processing_live_settings_changed)
    form.addRow("Mode", owner._processing_mode)
    form.addRow(owner._processing_mode_pages)
    form.addRow(owner._gpr_common_page)

    owner._bscan_axis.currentTextChanged.connect(owner._on_processing_live_settings_changed)
    owner._bscan_cut_m.valueChanged.connect(owner._on_processing_live_settings_changed)
    owner._bscan_max_depth_m.valueChanged.connect(owner._on_processing_live_settings_changed)
    owner._bscan_gain.valueChanged.connect(owner._on_processing_live_settings_changed)
    owner._bscan_start_freq_mhz.valueChanged.connect(owner._on_processing_live_settings_changed)
    owner._bscan_stop_freq_mhz.valueChanged.connect(owner._on_processing_live_settings_changed)
    owner._bscan_subtract_mean_ascan.toggled.connect(owner._on_processing_live_settings_changed)
    owner._gpr_input_positions_input.editingFinished.connect(owner._on_processing_live_settings_changed)
    owner._gpr_output_positions_input.editingFinished.connect(owner._on_processing_live_settings_changed)
    owner._gpr_min_depth_m.valueChanged.connect(owner._on_processing_live_settings_changed)
    owner._gpr_max_depth_m.valueChanged.connect(owner._on_processing_live_settings_changed)
    owner._gpr_range_comp_power.valueChanged.connect(owner._on_processing_live_settings_changed)
    owner._gpr_angle_comp_power.valueChanged.connect(owner._on_processing_live_settings_changed)
    owner._gpr_score_mode.currentTextChanged.connect(owner._on_processing_live_settings_changed)
    owner._gpr_start_freq_mhz.valueChanged.connect(owner._on_processing_live_settings_changed)
    owner._gpr_stop_freq_mhz.valueChanged.connect(owner._on_processing_live_settings_changed)
    owner._gpr_background_subtract_enabled.toggled.connect(owner._on_processing_live_settings_changed)
    owner._gpr_background_mean_count.valueChanged.connect(owner._on_processing_live_settings_changed)
    owner._gpr_remove_sidelobe_objects_enabled.toggled.connect(owner._on_processing_live_settings_changed)
    owner._gpr_imaging_plane_y_m.valueChanged.connect(owner._on_processing_live_settings_changed)
    owner._gpr_render_mode.currentTextChanged.connect(owner._on_gpr_visual_settings_changed)
    owner._gpr_min_visible_score.valueChanged.connect(owner._on_gpr_locator_threshold_changed)
    owner._gpr_max_detected_objects_to_draw.valueChanged.connect(owner._on_gpr_locator_threshold_changed)
    owner._gpr_draw_top_m_objects.valueChanged.connect(owner._on_gpr_locator_threshold_changed)
    owner._gpr_visible_x_min_m.valueChanged.connect(owner._on_gpr_locator_window_changed)
    owner._gpr_visible_x_max_m.valueChanged.connect(owner._on_gpr_locator_window_changed)
    owner._gpr_visible_z_min_m.valueChanged.connect(owner._on_gpr_locator_window_changed)
    owner._gpr_visible_z_max_m.valueChanged.connect(owner._on_gpr_locator_window_changed)
    owner._legacy_gpr_config_mode.currentTextChanged.connect(owner._on_processing_live_settings_changed)
    owner._legacy_gpr_input_positions_input.editingFinished.connect(owner._on_processing_live_settings_changed)
    owner._legacy_gpr_output_positions_input.editingFinished.connect(owner._on_processing_live_settings_changed)
    owner._legacy_gpr_min_depth_m.valueChanged.connect(owner._on_processing_live_settings_changed)
    owner._legacy_gpr_max_depth_m.valueChanged.connect(owner._on_processing_live_settings_changed)
    owner._legacy_gpr_comp_power.valueChanged.connect(owner._on_processing_live_settings_changed)
    owner._legacy_gpr_snr_thresh.valueChanged.connect(owner._on_processing_live_settings_changed)
    owner._legacy_gpr_snr_comp_max.valueChanged.connect(owner._on_processing_live_settings_changed)
    owner._legacy_gpr_start_freq_mhz.valueChanged.connect(owner._on_processing_live_settings_changed)
    owner._legacy_gpr_stop_freq_mhz.valueChanged.connect(owner._on_processing_live_settings_changed)
    owner._legacy_gpr_speed_m_s.valueChanged.connect(owner._on_processing_live_settings_changed)
    owner._legacy_gpr_look_angle_deg.valueChanged.connect(owner._on_processing_live_settings_changed)
    owner._legacy_gpr_background_subtract_enabled.toggled.connect(owner._on_processing_live_settings_changed)
    owner._legacy_gpr_background_mean_count.valueChanged.connect(owner._on_processing_live_settings_changed)
    owner._legacy_gpr_render_mode.currentTextChanged.connect(owner._on_gpr_visual_settings_changed)
    owner._legacy_gpr_min_visible_pair_count.valueChanged.connect(owner._on_gpr_locator_threshold_changed)
    owner._legacy_gpr_visible_x_min_m.valueChanged.connect(owner._on_gpr_locator_window_changed)
    owner._legacy_gpr_visible_x_max_m.valueChanged.connect(owner._on_gpr_locator_window_changed)
    owner._legacy_gpr_visible_z_min_m.valueChanged.connect(owner._on_gpr_locator_window_changed)
    owner._legacy_gpr_visible_z_max_m.valueChanged.connect(owner._on_gpr_locator_window_changed)

    owner._set_processing_mode_page(owner._processing_mode.currentText())
    return group
