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

from __future__ import annotations

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


def _default_gpr_input_positions(owner) -> str:
    """Build default live input-position selection from stable GPR config."""
    geometry_values = {int(entry.input_pos) for entry in owner._defaults_config.gpr.rx_geometry}
    combo_values = {int(combo.input) for combo in owner._defaults_config.combos}
    values = sorted(geometry_values & combo_values) or sorted(geometry_values)
    return ",".join(str(value) for value in values)


def _default_gpr_output_positions(owner) -> str:
    """Build default live output-position selection from stable GPR config."""
    geometry_values = {int(entry.output_pos) for entry in owner._defaults_config.gpr.tx_geometry}
    combo_values = {int(combo.output) for combo in owner._defaults_config.combos}
    values = sorted(geometry_values & combo_values) or sorted(geometry_values)
    return ",".join(str(value) for value in values)


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)

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

    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)

    owner._pass_through_fixed_y_enabled = QCheckBox("Fix magnitude Y range")
    owner._pass_through_fixed_y_enabled.setChecked(False)

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

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

    def sync_pass_through_y_controls() -> None:
        enabled = owner._pass_through_fixed_y_enabled.isChecked()
        owner._pass_through_y_min_db.setEnabled(enabled)
        owner._pass_through_y_max_db.setEnabled(enabled)

    sync_pass_through_y_controls()

    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)
    pass_through_form.addRow(owner._pass_through_fixed_y_enabled)
    pass_through_form.addRow("Y min dB", owner._pass_through_y_min_db)
    pass_through_form.addRow("Y max dB", owner._pass_through_y_max_db)
    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)

    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)
    owner._processing_mode_pages.addWidget(bscan_page)

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

    owner._gpr_input_positions_input = QLineEdit(_default_gpr_input_positions(owner))
    owner._gpr_input_positions_input.setPlaceholderText("0,1,2")

    owner._gpr_output_positions_input = QLineEdit(_default_gpr_output_positions(owner))
    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(2.0)

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

    owner._gpr_comp_power = QDoubleSpinBox()
    owner._gpr_comp_power.setDecimals(3)
    owner._gpr_comp_power.setRange(0.0, 5.0)
    owner._gpr_comp_power.setSingleStep(0.05)
    owner._gpr_comp_power.setValue(0.2)

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

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

    owner._gpr_background_subtract_enabled = QCheckBox("Subtract mean of previous collections")
    owner._gpr_background_subtract_enabled.setChecked(True)

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

    gpr_form.addRow("Input positions", owner._gpr_input_positions_input)
    gpr_form.addRow("Output positions", owner._gpr_output_positions_input)
    gpr_form.addRow("Min depth m", owner._gpr_min_depth_m)
    gpr_form.addRow("Max depth m", owner._gpr_max_depth_m)
    gpr_form.addRow("Comp power", owner._gpr_comp_power)
    gpr_form.addRow("Start MHz", owner._gpr_start_freq_mhz)
    gpr_form.addRow("Stop MHz", owner._gpr_stop_freq_mhz)
    gpr_form.addRow(owner._gpr_background_subtract_enabled)
    gpr_form.addRow("Mean count", owner._gpr_background_mean_count)
    owner._processing_mode_pages.addWidget(gpr_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)
    owner._pass_through_fixed_y_enabled.toggled.connect(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)

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