"""Builder for switch and combo settings section."""

from __future__ import annotations

from PyQt6.QtWidgets import QComboBox, QFormLayout, QGroupBox, QHBoxLayout, QLineEdit, QVBoxLayout


def build_switch_group(owner) -> QGroupBox:
    """Create input/output switch controls and run combos settings."""
    group = QGroupBox("Switches")
    layout = QVBoxLayout(group)
    input_defaults = owner._defaults_config.input_switch
    output_defaults = owner._defaults_config.output_switch

    global_form = QFormLayout()
    global_form.setFieldGrowthPolicy(QFormLayout.FieldGrowthPolicy.AllNonFixedFieldsGrow)
    owner._settling_ms = QLineEdit(str(owner._defaults_config.runtime.settling_ms))
    owner._combos_text = QLineEdit("")
    owner._combos_text.setPlaceholderText("input:output,input:output or empty for full")
    global_form.addRow("Settling ms", owner._settling_ms)
    global_form.addRow("Run combos", owner._combos_text)
    layout.addLayout(global_form)

    switch_columns = QHBoxLayout()

    input_group = QGroupBox("Input Switch (Radar Port 2)")
    input_form = QFormLayout(input_group)
    input_form.setFieldGrowthPolicy(QFormLayout.FieldGrowthPolicy.AllNonFixedFieldsGrow)

    owner._input_mode = QComboBox()
    owner._input_mode.addItems(["mock", "native"])
    owner._set_combo_current_text(owner._input_mode, input_defaults.driver_mode)
    owner._input_driver = QComboBox()
    owner._input_driver.addItems(["hmc349a", "h7992"])
    owner._set_combo_current_text(owner._input_driver, input_defaults.driver)
    owner._input_positions = QLineEdit(str(input_defaults.positions))
    owner._input_gpio_chip = QLineEdit(input_defaults.gpio_chip)
    owner._input_pin_a = QLineEdit(str(input_defaults.pin_a))
    owner._input_pin_b = QLineEdit(str(input_defaults.pin_b))
    owner._input_invert_logic = QComboBox()
    owner._input_invert_logic.addItems(["false", "true"])
    owner._set_combo_current_text(owner._input_invert_logic, "true" if input_defaults.invert_logic else "false")

    input_form.addRow("Mode", owner._input_mode)
    input_form.addRow("Driver", owner._input_driver)
    input_form.addRow("Positions", owner._input_positions)
    input_form.addRow("GPIO chip", owner._input_gpio_chip)
    input_form.addRow("Pin A", owner._input_pin_a)
    input_form.addRow("Pin B", owner._input_pin_b)
    input_form.addRow("Invert logic", owner._input_invert_logic)

    output_group = QGroupBox("Output Switch (Radar Port 1)")
    output_form = QFormLayout(output_group)
    output_form.setFieldGrowthPolicy(QFormLayout.FieldGrowthPolicy.AllNonFixedFieldsGrow)

    owner._output_mode = QComboBox()
    owner._output_mode.addItems(["mock", "native"])
    owner._set_combo_current_text(owner._output_mode, output_defaults.driver_mode)
    owner._output_driver = QComboBox()
    owner._output_driver.addItems(["h7992", "hmc349a"])
    owner._set_combo_current_text(owner._output_driver, output_defaults.driver)
    owner._output_positions = QLineEdit(str(output_defaults.positions))
    owner._output_gpio_chip = QLineEdit(output_defaults.gpio_chip)
    owner._output_pin_a = QLineEdit(str(output_defaults.pin_a))
    owner._output_pin_b = QLineEdit(str(output_defaults.pin_b))
    owner._output_invert_logic = QComboBox()
    owner._output_invert_logic.addItems(["false", "true"])
    owner._set_combo_current_text(owner._output_invert_logic, "true" if output_defaults.invert_logic else "false")

    output_form.addRow("Mode", owner._output_mode)
    output_form.addRow("Driver", owner._output_driver)
    output_form.addRow("Positions", owner._output_positions)
    output_form.addRow("GPIO chip", owner._output_gpio_chip)
    output_form.addRow("Pin A", owner._output_pin_a)
    output_form.addRow("Pin B", owner._output_pin_b)
    output_form.addRow("Invert logic", owner._output_invert_logic)

    switch_columns.addWidget(input_group)
    switch_columns.addWidget(output_group)
    layout.addLayout(switch_columns)

    return group
