#include "multiplesettings.h"
#include "ui_multiplesettings.h"

MultipleSettings::MultipleSettings(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MultipleSettings)
{
    ui->setupUi(this);
    ui->buttonBox->button(QDialogButtonBox::Ok)->setText("Записать");
    ui->buttonBox->button(QDialogButtonBox::SaveAll)->setText("Записать и установить");
    selectedBoard = 0;
}

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

void MultipleSettings::on_buttonBox_clicked(QAbstractButton *button)
{
    if(button == ui->buttonBox->button(QDialogButtonBox::Ok)) {
        newValue = ui->regValueLine->text().toInt(nullptr, 16);
        typeReg = ui->regTypeBox->currentIndex();
        startAdr = ui->adrBox->value();
        countReg = ui->countBox->value();
        boardId = ui->boardBox->currentIndex();
        emit write();
    } else if (button == ui->buttonBox->button(QDialogButtonBox::SaveAll)) {
        newValue = ui->regValueLine->text().toInt(nullptr, 16);
        typeReg = ui->regTypeBox->currentIndex();
        startAdr = ui->adrBox->value();
        countReg = ui->countBox->value();
        boardId = ui->boardBox->currentIndex();
        emit writeAndSend();
    } else {}
}

void MultipleSettings::on_regTypeBox_currentIndexChanged(int index)
{
    short maxRange = 0;
    switch (ui->boardBox->currentIndex()) {
    case 3:
        maxRange = 64;
        break;
    default:
        maxRange = 84;
    }
    switch (index) {
    case 0:
    case 1:
        ui->adrBox->setRange(0, maxRange);
        ui->adrBox->setValue(0);
        break;
    case 2:
        ui->adrBox->setRange(85, 85+maxRange);
        ui->adrBox->setValue(85);
        break;
    }
}

void MultipleSettings::on_boardBox_currentIndexChanged(int index)
{
    selectedBoard = index;
    on_regTypeBox_currentIndexChanged(ui->regTypeBox->currentIndex());
}

void MultipleSettings::on_adrBox_valueChanged(int arg1)
{
    ui->countBox->setRange(1, ((85-(20*(ui->boardBox->currentIndex()/3)))*(1+(ui->regTypeBox->currentIndex()/2))-arg1));
}
