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

from __future__ import annotations

from PyQt6.QtWidgets import (
    QCheckBox,
    QComboBox,
    QDoubleSpinBox,
    QFormLayout,
    QGroupBox,
    QHBoxLayout,
    QPushButton,
    QSizePolicy,
    QStackedWidget,
    QWidget,
)


def build_processing_group(owner) -> QGroupBox:
    """Create processing mode section with pass-through and B-scan pages."""
    group = QGroupBox("Processing")
    form = QFormLayout(group)
    form.setFieldGrowthPolicy(QFormLayout.FieldGrowthPolicy.AllNonFixedFieldsGrow)

    owner._processing_mode = QComboBox()
    owner._processing_mode.addItems(["pass_through", "bscan"])

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

    pass_through_page = QWidget(owner._processing_mode_pages)
    pass_through_page.setSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.Fixed)
    pass_through_form = QFormLayout(pass_through_page)
    pass_through_form.setFieldGrowthPolicy(QFormLayout.FieldGrowthPolicy.AllNonFixedFieldsGrow)

    owner._processing_gain_db = QDoubleSpinBox()
    owner._processing_gain_db.setDecimals(2)
    owner._processing_gain_db.setRange(-40.0, 40.0)
    owner._processing_gain_db.setSingleStep(0.25)
    owner._processing_gain_db.setValue(0.0)

    owner._processing_phase_deg = QDoubleSpinBox()
    owner._processing_phase_deg.setDecimals(1)
    owner._processing_phase_deg.setRange(-180.0, 180.0)
    owner._processing_phase_deg.setSingleStep(1.0)
    owner._processing_phase_deg.setValue(0.0)

    owner._show_magnitude_checkbox = QCheckBox("Show magnitude")
    owner._show_magnitude_checkbox.setChecked(True)

    owner._show_phase_checkbox = QCheckBox("Show phase")
    owner._show_phase_checkbox.setChecked(True)

    pass_through_form.addRow("Gain dB (live)", owner._processing_gain_db)
    pass_through_form.addRow("Phase deg (live)", owner._processing_phase_deg)
    pass_through_form.addRow(owner._show_magnitude_checkbox)
    pass_through_form.addRow(owner._show_phase_checkbox)
    owner._processing_mode_pages.addWidget(pass_through_page)

    bscan_page = QWidget(owner._processing_mode_pages)
    bscan_page.setSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.Fixed)
    bscan_form = QFormLayout(bscan_page)
    bscan_form.setFieldGrowthPolicy(QFormLayout.FieldGrowthPolicy.AllNonFixedFieldsGrow)

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

    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(0.824)

    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(1.0)

    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(1.0)

    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(100.0)

    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(8800.0)

    owner._bscan_clear_history_button = QPushButton("Clear B-scan History")
    owner._bscan_clear_history_button.clicked.connect(owner._on_bscan_clear_history_clicked)
    owner._bscan_remove_last_button = QPushButton("Remove Last Sweep")
    owner._bscan_remove_last_button.clicked.connect(owner._on_bscan_remove_last_sweep_clicked)
    bscan_actions = QWidget(owner._processing_mode_pages)
    bscan_actions_layout = QHBoxLayout(bscan_actions)
    bscan_actions_layout.setContentsMargins(0, 0, 0, 0)
    bscan_actions_layout.setSpacing(8)
    bscan_actions_layout.addWidget(owner._bscan_remove_last_button)
    bscan_actions_layout.addWidget(owner._bscan_clear_history_button)

    bscan_form.addRow("Axis", owner._bscan_axis)
    bscan_form.addRow("Cut m", owner._bscan_cut_m)
    bscan_form.addRow("Max depth m", owner._bscan_max_depth_m)
    bscan_form.addRow("Gain", owner._bscan_gain)
    bscan_form.addRow("Start MHz", owner._bscan_start_freq_mhz)
    bscan_form.addRow("Stop MHz", owner._bscan_stop_freq_mhz)
    bscan_form.addRow(bscan_actions)
    owner._processing_mode_pages.addWidget(bscan_page)

    owner._processing_mode.currentTextChanged.connect(owner._on_processing_mode_changed)
    owner._processing_gain_db.valueChanged.connect(owner._on_processing_live_settings_changed)
    owner._processing_phase_deg.valueChanged.connect(owner._on_processing_live_settings_changed)
    owner._show_magnitude_checkbox.toggled.connect(owner._on_trace_visibility_changed)
    owner._show_phase_checkbox.toggled.connect(owner._on_trace_visibility_changed)
    form.addRow("Mode", owner._processing_mode)
    form.addRow(owner._processing_mode_pages)

    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._on_processing_mode_changed(owner._processing_mode.currentText())
    return group
