#include "devicesettingsdialog.h"
#include "ui_devicesettingsdialog.h"
#include <QDebug>
#include <QProcess>
#include <QCoreApplication>

DeviceSettingsDialog::DeviceSettingsDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DeviceSettingsDialog)
{
    ui->setupUi(this);
    on_buttonApplyChangeTimer_clicked();
    {
        _m_timer[0] = ui->spinTimerBoard_1;
        _m_timer[1] = ui->spinTimerBoard_2;
        _m_timer[2] = ui->spinTimerBoard_3;
        _m_timer[3] = ui->spinTimerBoard_4;
    }
    _currentSpeed = ui->speedBox->currentText().toUInt();
    _currentParity = ui->parityBox->currentIndex();
    for(int i = 0; i < 4; i++) {
        _currentAdrs[i] = i+1;
    }
}

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

void DeviceSettingsDialog::on_buttonApplyChangeTimer_clicked()
{
    _currentBoardTimers[0] = ui->spinTimerBoard_1->value();
    _currentBoardTimers[1] = ui->spinTimerBoard_2->value();
    _currentBoardTimers[2] = ui->spinTimerBoard_3->value();
    _currentBoardTimers[3] = ui->spinTimerBoard_4->value();
}

void DeviceSettingsDialog::on_buttonApplyChangeSpeed_clicked()
{

    _currentSpeed = ui->speedBox->currentIndex();
    emit speedChanged();
}

void DeviceSettingsDialog::on_buttonApplyChangeParity_clicked()
{
    _currentParity = ui->parityBox->currentIndex();
    emit parityChanged();
}

void DeviceSettingsDialog::on_buttonApplyChangeAdr_clicked()
{
    BoardIdHasBeenChanged* _boardIdHasBeenChanged = new BoardIdHasBeenChanged(ui->idComboBox->currentText().toUInt(), ui->adrSpinBox->value());
    QCoreApplication::postEvent(parent(), _boardIdHasBeenChanged);
    close();
}

unsigned DeviceSettingsDialog::currentBoardTimer(unsigned short _ID)
{
    return _currentBoardTimers[_ID];
}

unsigned DeviceSettingsDialog::currentSpeed()
{
    return _currentSpeed;
}

unsigned short DeviceSettingsDialog::currentParity()
{
    return _currentParity;
}

void DeviceSettingsDialog::updateSettingsAfterConnection(unsigned tmp_speed, unsigned tmp_parity, unsigned *tmp_adr, bool *ActiveDevices)
{
    ui->speedBox->setCurrentText(QString::number(_currentSpeed=tmp_speed, 10));
    if(tmp_parity>0)
        tmp_parity--;
    ui->parityBox->setCurrentIndex(_currentParity = tmp_parity);
    for(int i = 0; i < 4; i++) {
        if(ActiveDevices[i]) {
            _m_timer[i]->setEnabled(true);
            ui->idComboBox->addItem(QString::number(i));
            _currentAdrs[i] = tmp_adr[i];
        } else {
            _m_timer[i]->setEnabled(false);
        }
    }
    on_idComboBox_currentIndexChanged(ui->idComboBox->currentIndex());
}

void DeviceSettingsDialog::on_idComboBox_currentIndexChanged(int index)
{
    ui->adrSpinBox->setValue(_currentAdrs[index]);
}

void DeviceSettingsDialog::on_buttonBox_clicked(QAbstractButton *button)
{
    switch (ui->buttonBox->buttonRole(button)) {
    case QDialogButtonBox::ResetRole:
        ui->spinTimerBoard_1->setValue(1000);
        ui->spinTimerBoard_2->setValue(1000);
        ui->spinTimerBoard_3->setValue(1000);
        ui->spinTimerBoard_4->setValue(1000);
        on_buttonApplyChangeTimer_clicked();
        ui->speedBox->setCurrentText("31250");
        _currentSpeed = ui->speedBox->currentText().toUInt();
        ui->parityBox->setCurrentIndex(0);
        _currentParity = ui->parityBox->currentIndex();
        for(int i = 0; i < 4; i++) {
            _currentAdrs[i] = i+1;
        }
        ui->adrSpinBox->setValue(_currentAdrs[ui->idComboBox->currentIndex()]);
        break;
    case QDialogButtonBox::AcceptRole:
        close();
        break;
    default:
        break;
    }
}

void DeviceSettingsDialog::initPollForBoard(unsigned boardID, unsigned boardAdr)
{
    ui->idPollComboBox->addItem(QString("Плата №%1 (ID %2)").arg(boardID+1).arg(boardAdr), QVariant(boardID));
}

void DeviceSettingsDialog::updatePollStatus(unsigned boardID, bool status)
{
    _currentPollStatus[boardID] = status;
}

void DeviceSettingsDialog::on_buttonApplyChangePoll_clicked()
{
    sendPollCommand(ui->idPollComboBox->currentData().toUInt(), (bool)ui->pollStatusBox->currentIndex());
}

void DeviceSettingsDialog::sendPollCommand(unsigned boardID, bool status)
{
    updatePollStatus(boardID, status);
    pollStatusChange* _pollStatusChanged = new pollStatusChange(boardID, status);
    QCoreApplication::postEvent(parent(), _pollStatusChanged);
}

void DeviceSettingsDialog::on_idPollComboBox_currentIndexChanged(int index)
{
    Q_UNUSED(index);
    ui->pollStatusBox->setCurrentIndex(_currentPollStatus[ui->idPollComboBox->currentData().toUInt()]);
}

void DeviceSettingsDialog::onDisconnect()
{
    ui->idComboBox->clear();
    ui->idPollComboBox->clear();
}
