#include "adddesignatordialog.h"
#include "../model/designatormappingmodel.h"
#include <QRegularExpression>

AddDesignatorDialog::AddDesignatorDialog(QWidget *parent)
    : QDialog(parent)
{
    setUp();
}

void AddDesignatorDialog::setUp()
{
    setWindowTitle(tr("Добавить дезигнатор"));
    setModal(true);
    setFixedSize(400, 200);
    
    setUpLayout();
    setUpConnections();
    validateInputs();
}

void AddDesignatorDialog::setUpLayout()
{
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    
    // Группа для ввода данных
    QGroupBox *inputGroup = new QGroupBox(tr("Информация о дезигнаторе"), this);
    QVBoxLayout *inputLayout = new QVBoxLayout(inputGroup);
    
    // Дезигнатор
    QHBoxLayout *designatorLayout = new QHBoxLayout;
    QLabel *designatorLabel = new QLabel(tr("Дезигнатор (1-2 буквы):"), this);
    m_designatorEdit = new QLineEdit(this);
    m_designatorEdit->setPlaceholderText(tr("Например: R, BA"));
    m_designatorEdit->setMaxLength(2);
    
    // Валидатор для дезигнатора (только латинские буквы)
    QRegularExpression regex("^[A-Z]{1,2}$");
    m_designatorValidator = new QRegularExpressionValidator(regex, this);
    m_designatorEdit->setValidator(m_designatorValidator);
    
    designatorLayout->addWidget(designatorLabel);
    designatorLayout->addWidget(m_designatorEdit);
    
    // Единственное число
    QHBoxLayout *singularLayout = new QHBoxLayout;
    QLabel *singularLabel = new QLabel(tr("Единственное число:"), this);
    m_singularNameEdit = new QLineEdit(this);
    m_singularNameEdit->setPlaceholderText(tr("Например: Резистор"));
    
    singularLayout->addWidget(singularLabel);
    singularLayout->addWidget(m_singularNameEdit);
    
    // Множественное число
    QHBoxLayout *pluralLayout = new QHBoxLayout;
    QLabel *pluralLabel = new QLabel(tr("Множественное число:"), this);
    m_pluralNameEdit = new QLineEdit(this);
    m_pluralNameEdit->setPlaceholderText(tr("Например: Резисторы"));
    
    pluralLayout->addWidget(pluralLabel);
    pluralLayout->addWidget(m_pluralNameEdit);
    
    // Добавляем все в layout группы
    inputLayout->addLayout(designatorLayout);
    inputLayout->addLayout(singularLayout);
    inputLayout->addLayout(pluralLayout);
    
    // Кнопки
    QHBoxLayout *buttonsLayout = new QHBoxLayout;
    m_okButton = new QPushButton(tr("OK"), this);
    m_cancelButton = new QPushButton(tr("Отмена"), this);
    
    buttonsLayout->addStretch();
    buttonsLayout->addWidget(m_okButton);
    buttonsLayout->addWidget(m_cancelButton);
    
    // Добавляем все в основной layout
    mainLayout->addWidget(inputGroup);
    mainLayout->addLayout(buttonsLayout);
    
    setLayout(mainLayout);
}

void AddDesignatorDialog::setUpConnections()
{
    connect(m_designatorEdit, &QLineEdit::textChanged,
            this, &AddDesignatorDialog::onDesignatorChanged);
    connect(m_singularNameEdit, &QLineEdit::textChanged,
            this, &AddDesignatorDialog::validateInputs);
    connect(m_pluralNameEdit, &QLineEdit::textChanged,
            this, &AddDesignatorDialog::validateInputs);
    
    connect(m_okButton, &QPushButton::clicked,
            this, &QDialog::accept);
    connect(m_cancelButton, &QPushButton::clicked,
            this, &QDialog::reject);
}

void AddDesignatorDialog::onDesignatorChanged()
{
    QString text = m_designatorEdit->text().toUpper();
    if (text != m_designatorEdit->text()) {
        m_designatorEdit->setText(text);
    }
    validateInputs();
}

void AddDesignatorDialog::validateInputs()
{
    bool isValid = !m_designatorEdit->text().isEmpty() &&
                   !m_singularNameEdit->text().isEmpty() &&
                   !m_pluralNameEdit->text().isEmpty();
    
    m_okButton->setEnabled(isValid);
}

QString AddDesignatorDialog::getDesignator() const
{
    return m_designatorEdit->text().toUpper();
}

QString AddDesignatorDialog::getSingularName() const
{
    return m_singularNameEdit->text();
}

QString AddDesignatorDialog::getPluralName() const
{
    return m_pluralNameEdit->text();
}

DesignatorMappingItem AddDesignatorDialog::getMapping() const
{
    return DesignatorMappingItem(getDesignator(), getSingularName(), getPluralName(), true);
} 