// Copyright 2015 - 2026, GIBIS-UNIFESP and the wiRedPanda contributors
// SPDX-License-Identifier: GPL-3.0-or-later

#include "App/UI/ElementEditorUI.h"

#include <QtCore/QCoreApplication>
#include <QtCore/QMetaObject>
#include <QtGui/QIcon>

#include "App/UI/LabeledSlider.h"

void ElementEditorUi::setupUi(QWidget *ElementEditor)
{
    if (ElementEditor->objectName().isEmpty()) {
        ElementEditor->setObjectName("ElementEditor");
    }

    // Design size — actual size is controlled by the parent splitter.
    ElementEditor->resize(304, 395);

    // Zero margins/spacing so the group box fills the widget edge-to-edge,
    // keeping the panel compact inside the narrow left splitter pane.
    gridLayout = new QGridLayout(ElementEditor);
    gridLayout->setSpacing(0);
    gridLayout->setObjectName("gridLayout");
    gridLayout->setContentsMargins(0, 0, 0, 0);

    // The group box title shows the element type name (set dynamically by
    // ElementEditor::setCurrentElements).  Bold font makes it stand out.
    groupBox = new QGroupBox(ElementEditor);
    groupBox->setObjectName("groupBox");
    QFont font;
    font.setBold(true);
    groupBox->setFont(font);

    gridLayout_2 = new QGridLayout(groupBox);
    gridLayout_2->setObjectName("gridLayout_2");

    // Pre-populate with 8 empty items so retranslateUi() can fill them in
    // without needing to know the count at construction time.
    comboBoxAudio = new QComboBox(groupBox);

    for (int i = 0; i < 8; ++i) {
        comboBoxAudio->addItem(QString());
    }

    comboBoxAudio->setObjectName("comboBoxAudio");
    gridLayout_2->addWidget(comboBoxAudio, 8, 1, 1, 1);

    checkBoxLocked = new QCheckBox(groupBox);
    checkBoxLocked->setObjectName("checkBoxLocked");
    gridLayout_2->addWidget(checkBoxLocked, 4, 1, 1, 1);

    labelLabels = new QLabel(groupBox);
    labelLabels->setObjectName("labelLabels");
    gridLayout_2->addWidget(labelLabels, 0, 0, 1, 1);

    labelTrigger = new QLabel(groupBox);
    labelTrigger->setObjectName("labelTrigger");
    gridLayout_2->addWidget(labelTrigger, 10, 0, 1, 1);

    pushButtonTruthTable = new QPushButton(groupBox);
    pushButtonTruthTable->setObjectName("pushButtonTruthTable");
    pushButtonTruthTable->setEnabled(true);
    gridLayout_2->addWidget(pushButtonTruthTable, 11, 0, 1, 2);

    // Appearance state selector (for multi-state elements like LEDs)
    labelAppearanceState = new QLabel(groupBox);
    labelAppearanceState->setObjectName("labelAppearanceState");
    gridLayout_2->addWidget(labelAppearanceState, 12, 0, 1, 1);

    comboBoxAppearanceState = new QComboBox(groupBox);
    comboBoxAppearanceState->setObjectName("comboBoxAppearanceState");
    gridLayout_2->addWidget(comboBoxAppearanceState, 12, 1, 1, 1);

    horizontalLayout = new QHBoxLayout();
    horizontalLayout->setSpacing(6);
    horizontalLayout->setObjectName("horizontalLayout");
    horizontalLayout->setSizeConstraint(QLayout::SetNoConstraint);

    pushButtonChangeAppearance = new QPushButton(groupBox);
    pushButtonChangeAppearance->setObjectName("pushButtonChangeAppearance");
    horizontalLayout->addWidget(pushButtonChangeAppearance);

    pushButtonDefaultAppearance = new QPushButton(groupBox);
    pushButtonDefaultAppearance->setObjectName("pushButtonDefaultAppearance");
    QSizePolicy sizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
    sizePolicy.setHorizontalStretch(0);
    sizePolicy.setVerticalStretch(0);
    sizePolicy.setHeightForWidth(pushButtonDefaultAppearance->sizePolicy().hasHeightForWidth());
    pushButtonDefaultAppearance->setSizePolicy(sizePolicy);
    pushButtonDefaultAppearance->setIcon(QIcon(":/Interface/Locale/default.svg"));
    pushButtonDefaultAppearance->setIconSize(QSize(16, 16));
    horizontalLayout->addWidget(pushButtonDefaultAppearance);

    gridLayout_2->addLayout(horizontalLayout, 13, 0, 1, 2);

    lineEditTrigger = new QLineEdit(groupBox);
    lineEditTrigger->setObjectName("lineEditTrigger");
    gridLayout_2->addWidget(lineEditTrigger, 10, 1, 1, 1);

    labelValue = new QLabel(groupBox);
    labelValue->setObjectName("labelValue");
    gridLayout_2->addWidget(labelValue, 3, 0, 1, 1);

    doubleSpinBoxFrequency = new QDoubleSpinBox(groupBox);
    doubleSpinBoxFrequency->setObjectName("doubleSpinBoxFrequency");
    doubleSpinBoxFrequency->setDecimals(1);
    // Minimum 0.0 here; ElementEditor raises it to 0.1 when a real value is shown
    // (0.0 is reserved as the sentinel for "many frequencies").  50 Hz max matches
    // the simulation's 1 ms tick rate (anything faster would be indistinguishable).
    doubleSpinBoxFrequency->setMinimum(0.000000000000000);
    doubleSpinBoxFrequency->setMaximum(50.000000000000000);
    doubleSpinBoxFrequency->setSingleStep(0.100000000000000);
    gridLayout_2->addWidget(doubleSpinBoxFrequency, 5, 1, 1, 1);

    comboBoxInputSize = new QComboBox(groupBox);
    comboBoxInputSize->setObjectName("comboBoxInputSize");
    gridLayout_2->addWidget(comboBoxInputSize, 1, 1, 1, 1);

    labelFrequency = new QLabel(groupBox);
    labelFrequency->setObjectName("labelFrequency");
    gridLayout_2->addWidget(labelFrequency, 5, 0, 1, 1);

    labelColor = new QLabel(groupBox);
    labelColor->setObjectName("labelColor");
    gridLayout_2->addWidget(labelColor, 7, 0, 1, 1);

    horizontalLayout_2 = new QHBoxLayout();
    horizontalLayout_2->setObjectName("horizontalLayout_2");

    // Read-only line edit shows the currently selected audio file path.
    // Editing is done via the "..." button which opens a file dialog.
    lineCurrentAudioBox = new QLineEdit(groupBox);
    lineCurrentAudioBox->setObjectName("lineCurrentAudioBox");
    lineCurrentAudioBox->setReadOnly(true);
    horizontalLayout_2->addWidget(lineCurrentAudioBox);

    // Narrow "..." button beside the audio path — constrained to 28 px wide so
    // it doesn't steal space from the filename display.
    pushButtonAudioBox = new QPushButton(groupBox);
    pushButtonAudioBox->setObjectName("pushButtonAudioBox");
    QSizePolicy sizePolicy1(QSizePolicy::Fixed, QSizePolicy::Fixed);
    sizePolicy1.setHorizontalStretch(0);
    sizePolicy1.setVerticalStretch(0);
    sizePolicy1.setHeightForWidth(pushButtonAudioBox->sizePolicy().hasHeightForWidth());
    pushButtonAudioBox->setSizePolicy(sizePolicy1);
    pushButtonAudioBox->setMaximumSize(QSize(28, 16777215));
    horizontalLayout_2->addWidget(pushButtonAudioBox);
    gridLayout_2->addLayout(horizontalLayout_2, 9, 1, 1, 1);

    lineEditElementLabel = new QLineEdit(groupBox);
    lineEditElementLabel->setObjectName("lineEditElementLabel");
    gridLayout_2->addWidget(lineEditElementLabel, 0, 1, 1, 1);

    labelInputs = new QLabel(groupBox);
    labelInputs->setObjectName("labelInputs");
    gridLayout_2->addWidget(labelInputs, 1, 0, 1, 1);

    comboBoxColor = new QComboBox(groupBox);
    comboBoxColor->setObjectName("comboBoxColor");
    gridLayout_2->addWidget(comboBoxColor, 7, 1, 1, 1);

    labelAudioBox = new QLabel(groupBox);
    labelAudioBox->setObjectName("labelAudioBox");
    gridLayout_2->addWidget(labelAudioBox, 9, 0, 1, 1);

    comboBoxValue = new QComboBox(groupBox);
    comboBoxValue->setObjectName("comboBoxValue");
    gridLayout_2->addWidget(comboBoxValue, 3, 1, 1, 1);

    comboBoxOutputSize = new QComboBox(groupBox);
    comboBoxOutputSize->setObjectName("comboBoxOutputSize");
    gridLayout_2->addWidget(comboBoxOutputSize, 2, 1, 1, 1);

    labelOutputs = new QLabel(groupBox);
    labelOutputs->setObjectName("labelOutputs");
    gridLayout_2->addWidget(labelOutputs, 2, 0, 1, 1);

    labelLocked = new QLabel(groupBox);
    labelLocked->setObjectName("labelLocked");
    gridLayout_2->addWidget(labelLocked, 4, 0, 1, 1);

    labelAudio = new QLabel(groupBox);
    labelAudio->setObjectName("labelAudio");
    gridLayout_2->addWidget(labelAudio, 8, 0, 1, 1);

    labelDelay = new QLabel(groupBox);
    labelDelay->setObjectName("labelDelay");
    gridLayout_2->addWidget(labelDelay, 6, 0, 1, 1);

    // Delay slider range -4 to +4 in integer steps.  Each step represents 1/8 of
    // a clock period, giving a phase offset range of -0.5 to +0.5 periods.
    // ElementEditor converts between slider integer and float delay on read/write.
    sliderDelay = new LabeledSlider(groupBox);
    sliderDelay->setObjectName("sliderDelay");
    sliderDelay->setOrientation(Qt::Horizontal);
    sliderDelay->setMinimum(-4);
    sliderDelay->setMaximum(4);
    sliderDelay->setValue(0);
    sliderDelay->setSingleStep(1);
    sliderDelay->setPageStep(1);
    sliderDelay->setTickPosition(QSlider::TicksBelow);
    sliderDelay->setTickInterval(1);
    gridLayout_2->addWidget(sliderDelay, 6, 1, 1, 1);

    labelWirelessMode = new QLabel(groupBox);
    labelWirelessMode->setObjectName("labelWirelessMode");
    gridLayout_2->addWidget(labelWirelessMode, 11, 0, 1, 1);

    comboBoxWirelessMode = new QComboBox(groupBox);
    comboBoxWirelessMode->setObjectName("comboBoxWirelessMode");
    for (int i = 0; i < 3; ++i) {
        comboBoxWirelessMode->addItem(QString());
    }
    gridLayout_2->addWidget(comboBoxWirelessMode, 11, 1, 1, 1);

    labelBlobName = new QLabel(groupBox);
    labelBlobName->setObjectName("labelBlobName");
    gridLayout_2->addWidget(labelBlobName, 13, 0, 1, 1);

    lineEditBlobName = new QLineEdit(groupBox);
    lineEditBlobName->setObjectName("lineEditBlobName");
    gridLayout_2->addWidget(lineEditBlobName, 13, 1, 1, 1);

    labelVolume = new QLabel(groupBox);
    labelVolume->setObjectName("labelVolume");
    gridLayout_2->addWidget(labelVolume, 14, 0, 1, 1);

    sliderVolume = new QSlider(groupBox);
    sliderVolume->setObjectName("sliderVolume");
    sliderVolume->setOrientation(Qt::Horizontal);
    sliderVolume->setMinimum(0);
    sliderVolume->setMaximum(100);
    sliderVolume->setValue(50);
    sliderVolume->setSingleStep(5);
    sliderVolume->setPageStep(10);
    sliderVolume->setTickPosition(QSlider::TicksBelow);
    sliderVolume->setTickInterval(25);
    gridLayout_2->addWidget(sliderVolume, 14, 1, 1, 1);

    gridLayout->addWidget(groupBox, 0, 0, 1, 1);

    retranslateUi(ElementEditor);

    QMetaObject::connectSlotsByName(ElementEditor);
}

void ElementEditorUi::retranslateUi(QWidget *ElementEditor)
{
    ElementEditor->setWindowTitle(QCoreApplication::translate("ElementEditor", "Form"));
    groupBox->setTitle(QCoreApplication::translate("ElementEditor", "Title"));
    comboBoxAudio->setItemText(0, QCoreApplication::translate("ElementEditor", "C6"));
    comboBoxAudio->setItemText(1, QCoreApplication::translate("ElementEditor", "D6"));
    comboBoxAudio->setItemText(2, QCoreApplication::translate("ElementEditor", "E6"));
    comboBoxAudio->setItemText(3, QCoreApplication::translate("ElementEditor", "F6"));
    comboBoxAudio->setItemText(4, QCoreApplication::translate("ElementEditor", "G6"));
    comboBoxAudio->setItemText(5, QCoreApplication::translate("ElementEditor", "A7"));
    comboBoxAudio->setItemText(6, QCoreApplication::translate("ElementEditor", "B7"));
    comboBoxAudio->setItemText(7, QCoreApplication::translate("ElementEditor", "C7"));

    checkBoxLocked->setText(QString());
    labelLabels->setText(QCoreApplication::translate("ElementEditor", "Label:"));
    labelTrigger->setText(QCoreApplication::translate("ElementEditor", "Trigger:"));
    pushButtonTruthTable->setText(QCoreApplication::translate("ElementEditor", "Edit Truth Table"));
    labelAppearanceState->setText(QCoreApplication::translate("ElementEditor", "Appearance:"));
    pushButtonChangeAppearance->setText(QCoreApplication::translate("ElementEditor", "Change appearance to ..."));
    pushButtonDefaultAppearance->setToolTip(QCoreApplication::translate("ElementEditor", "Default"));
    pushButtonDefaultAppearance->setText(QString());
    labelValue->setText(QCoreApplication::translate("ElementEditor", "Value:"));
    doubleSpinBoxFrequency->setSpecialValueText(QString());
    doubleSpinBoxFrequency->setSuffix(QCoreApplication::translate("ElementEditor", " Hz"));
    labelFrequency->setText(QCoreApplication::translate("ElementEditor", "Frequency:"));
    labelColor->setText(QCoreApplication::translate("ElementEditor", "Color:"));
    pushButtonAudioBox->setText(QCoreApplication::translate("ElementEditor", "..."));
    labelInputs->setText(QCoreApplication::translate("ElementEditor", "Inputs:"));
    comboBoxColor->setCurrentText(QString());
    labelAudioBox->setText(QCoreApplication::translate("ElementEditor", "Audio:"));
    labelOutputs->setText(QCoreApplication::translate("ElementEditor", "Outputs:"));
    labelLocked->setText(QCoreApplication::translate("ElementEditor", "Locked:"));
    labelAudio->setText(QCoreApplication::translate("ElementEditor", "Audio:"));
    labelDelay->setText(QCoreApplication::translate("ElementEditor", "Delay:"));
    labelWirelessMode->setText(QCoreApplication::translate("ElementEditor", "Wireless:"));
    comboBoxWirelessMode->setItemText(0, QCoreApplication::translate("ElementEditor", "None"));
    comboBoxWirelessMode->setItemText(1, QCoreApplication::translate("ElementEditor", "Transmit (Tx)"));
    comboBoxWirelessMode->setItemText(2, QCoreApplication::translate("ElementEditor", "Receive (Rx)"));
    labelBlobName->setText(QCoreApplication::translate("ElementEditor", "IC Name:"));
    labelVolume->setText(QCoreApplication::translate("ElementEditor", "Volume:"));
}
