#include "configuration.h"
#include "ui_configuration.h"

#include <QDebug>

Configuration::Configuration(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Configuration)
{
    ui->setupUi(this);

    port_ = new Port_Com;

    QObject::connect(this, SIGNAL(sig_baud_Changed(BaudRateType)), port_, SLOT(set_baudRate(BaudRateType)));
    QObject::connect(this, SIGNAL(sig_portName_Changed(QString)), port_, SLOT(set_portName(QString)));
}

Configuration::~Configuration()
{
    delete ui;
}

QString Configuration::set_partNumber()
{
    qDebug() << "Configuration::set_partNumber";

    return part_Number_;
}


QString Configuration::set_serialNumber()
{
    qDebug() << "Configuration::set_serialNumber";

    return serial_Number_;
}


/******************** SLOTS ********************/
/*!
  \brief Slot appelé lors du changement du nom du port série
*
*        Récupère le nom de l'index courant
*        Emet le signal sig_portName_Changed
  */
void Configuration::on_portName_combo_currentIndexChanged(int index)
{
    QString name_;

    name_ = ui->portName_combo->itemText(index);

    emit sig_portName_Changed(name_);
}

/*!
  \brief Slot appelé lors du changement de la vitesse du port série
*
*        Récupère la valeur du baudRate en fonction de l'index courant
*        Emet le signal sig_baud_Changed
  */
void Configuration::on_baud_combo_currentIndexChanged(int index)
{
    BaudRateType baudRate_;

    switch(index)
    {
    case 0:
        baudRate_ = BAUD9600;
        break;
    case 1:
        baudRate_ = BAUD19200;
        break;
    case 2:
        baudRate_ = BAUD38400;
        break;
    case 3:
        baudRate_ = BAUD57600;
        break;
    }

    emit sig_baud_Changed(baudRate_);
}

/*!
  \brief Slot appelé lors de la fermeture ou l'ouverture du port série
  */
void Configuration::on_openCom_toggled(bool)
{        
    int resul = RS232_NOT_USED;

    if(ui->openCom->isChecked())
    {
        resul = port_->open_Serial();

        if(resul == RS232_NOT_USED)
        {
            ui->closeCom->setChecked(true);
            QMessageBox::critical(this, "ERROR COMMUNICATION DATA LINK", "Port com not used");
        }
        else if(resul == RS232_NOT_OPENED)
        {
            ui->closeCom->setChecked(true);
            QMessageBox::critical(this, "ERROR COMMUNICATION DATA LINK", "Port com not opened");
        }
        else if(resul == RS232_OPENED)
        {
            ui->baud_combo->setEnabled(false);
            ui->portName_combo->setEnabled(false);
        }
    }
    else
    {
        resul = port_->close_Serial();

        if(resul == RS232_OPENED)
            QMessageBox::critical(this, "ERROR COMMUNICATION DATA LINK", "Port com already opened");
        else if(resul == RS232_NOT_OPENED)
        {
            ui->baud_combo->setEnabled(true);
            ui->portName_combo->setEnabled(true);
        }
    }

    emit sig_open_Serial(resul);
}

/*!
  \brief Slot appelé lors d'une saisie dans le champ part number
  *
  * Emet le signal sig_partNumber
  */
void Configuration::on_partNumber_textChanged(QString text)
{
    part_Number_ = text;

    qDebug() << "Configuration::on_partNumber_textChanged";
}

/*!
  \brief Slot appelé lors d'une saisie dans le champ serial number
  *
  * Emet le signal sig_serialNumber
  */
void Configuration::on_serialNumber_textChanged(QString text)
{
    serial_Number_ = text;

    qDebug() << "Configuration::on_serialNumber_textChanged";
}

/******************** END SLOTS ********************/
