#include "debugterminaldialog.h"
#include "ui_debugTerminalDialog.h"
#include "adcgraphdialog.h"
#include <QDebug>
#include <QShowEvent>
#include <QCloseEvent>
#include <QAbstractButton>

DebugTerminalDialog::DebugTerminalDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DebugTerminalDialog),
    m_adcGraphDialog(nullptr),
    m_modbusDevice(nullptr)
{
    ui->setupUi(this);
    boards[0].error24V = ui->discErr24TestChkBox_1;
    boards[0].error5V = ui->discErr5TestChkBox_1;
    boards[0].error5VSCI = ui->discErr5VsciTestChkBox_1;
    boards[0].error5VA = ui->discErr5VATestChkBox_1;

    boards[1].error24V = ui->discErr24TestChkBox_2;
    boards[1].error5V = ui->discErr5TestChkBox_2;
    boards[1].error5VSCI = ui->discErr5VsciTestChkBox_2;
    boards[1].error5VA = ui->discErr5VATestChkBox_2;

    boards[2].error24V = ui->discErr24TestChkBox_3;
    boards[2].error5V = ui->discErr5TestChkBox_3;
    boards[2].error5VSCI = ui->discErr5VsciTestChkBox_3;
    boards[2].error5VA = ui->discErr5VATestChkBox_3;

    boards[3].error24V = ui->discErr24TestChkBox_4;
    boards[3].error5V = ui->discErr5TestChkBox_4;
    boards[3].error5VSCI = ui->discErr5VsciTestChkBox_4;
    boards[3].error5VA = ui->discErr5VATestChkBox_4;
    initializeConnections();

    // Создаем AdcGraphDialog с nullptr
    m_adcGraphDialog = new AdcGraphDialog(nullptr, this);
}

DebugTerminalDialog::~DebugTerminalDialog()
{
    delete ui;
}
void DebugTerminalDialog::setMainTerm(M3KTE* term)
{
    mainTerm = term;
}

void DebugTerminalDialog::setModbusDevice(QModbusClient *device)
{
    m_modbusDevice = device;
    if (m_adcGraphDialog && device) {
        m_adcGraphDialog->setModbusDevice(device);
    }
}

void DebugTerminalDialog::setDebugTerminalCoil(int enable)
{
    writeCoil(0, COIL_DEBUG_MODE, enable);
    writeCoil(1, COIL_DEBUG_MODE, enable);
    writeCoil(2, COIL_DEBUG_MODE, enable);
    writeCoil(3, COIL_DEBUG_MODE, enable);
}

void DebugTerminalDialog::showEvent(QShowEvent *event)
{
    QDialog::showEvent(event);
    // При открытии окна записываем в коил 555 значение "1"

    resetAll();

    setDebugTerminalCoil(1);
}

void DebugTerminalDialog::closeEvent(QCloseEvent *event)
{
    // При закрытии окна записываем в коил 555 значение "0"
    setDebugTerminalCoil(0);
    QDialog::closeEvent(event);
}

void DebugTerminalDialog::initializeConnections()
{
//    // Подключаем кнопки OK и RestoreDefaults
//    connect(ui->buttonBox, &QDialogButtonBox::clicked, this, &DebugTerminalDialog::on_buttonBox_clicked);

//    // Подключаем все чекбоксы для платы 1
//    connect(ui->continiusCallChkBox_1, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_continiusCallChkBox_1_stateChanged);
//    connect(ui->calibrateCallChkBox_1, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_calibrateCallChkBox_1_stateChanged);
//    connect(ui->pollTECallChkBox_1, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_pollTECallChkBox_1_stateChanged);
//    connect(ui->resetKeyCallChkBox_1, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_resetKeyCallChkBox_1_stateChanged);
//    connect(ui->resetDefaultCallChkBox_1, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_resetDefaultCallChkBox_1_stateChanged);
//    connect(ui->getHardfaultCallChkBox_1, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_getHardfaultCallChkBox_1_stateChanged);


//    connect(ui->enableLedTestChkBox_1, &QCheckBox::stateChanged, this, &DebugTerminalDialog::enableLedTestChkBox_1_stateChanged);

//    connect(ui->discWorkTestChkBox_1, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_discWorkTestChkBox_1_stateChanged);
//    connect(ui->discWarnTestChkBox_1, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_discWarnTestChkBox_1_stateChanged);
//    connect(ui->discErrTestChkBox_1, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_discErrTestChkBox_1_stateChanged);


//    connect(ui->discErr24TestChkBox_1, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_discErr24TestChkBox_1_stateChanged);
//    ui->discErr24TestChkBox_1->setAttribute(Qt::WA_TransparentForMouseEvents, true);
//    connect(ui->discErr5TestChkBox_1, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_discErr5TestChkBox_1_stateChanged);
//    ui->discErr5TestChkBox_1->setAttribute(Qt::WA_TransparentForMouseEvents, true);
//    connect(ui->discErr5VsciTestChkBox_1, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_discErr5VsciTestChkBox_1_stateChanged);
//    ui->discErr5VsciTestChkBox_1->setAttribute(Qt::WA_TransparentForMouseEvents, true);
//    connect(ui->discErr5VATestChkBox_1, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_discErr5VATestChkBox_1_stateChanged);
//    ui->discErr5VATestChkBox_1->setAttribute(Qt::WA_TransparentForMouseEvents, true);

//    connect(ui->ledWorkTestChkBox_1, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_ledWorkTestChkBox_1_stateChanged);
//    connect(ui->ledWarnTestChkBox_1, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_ledWarnTestChkBox_1_stateChanged);
//    connect(ui->ledErrTestChkBox_1, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_ledErrTestChkBox_1_stateChanged);
//    connect(ui->ledConnectTestChkBox_1, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_ledConnectTestChkBox_1_stateChanged);
//    connect(ui->ledVH1TestChkBox_1, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_ledVH1TestChkBox_1_stateChanged);
//    connect(ui->ledVH2TestChkBox_1, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_ledVH2TestChkBox_1_stateChanged);
//    connect(ui->ledVH3TestChkBox_1, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_ledVH3TestChkBox_1_stateChanged);

//    // Подключаем все чекбоксы для платы 2
//    connect(ui->continiusCallChkBox_2, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_continiusCallChkBox_2_stateChanged);
//    connect(ui->calibrateCallChkBox_2, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_calibrateCallChkBox_2_stateChanged);
//    connect(ui->pollTECallChkBox_2, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_pollTECallChkBox_2_stateChanged);
//    connect(ui->resetKeyCallChkBox_2, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_resetKeyCallChkBox_2_stateChanged);
//    connect(ui->resetDefaultCallChkBox_2, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_resetDefaultCallChkBox_2_stateChanged);
//    connect(ui->getHardfaultCallChkBox_2, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_getHardfaultCallChkBox_2_stateChanged);

//    connect(ui->enableLedTestChkBox_2, &QCheckBox::stateChanged, this, &DebugTerminalDialog::enableLedTestChkBox_2_stateChanged);

//    connect(ui->discWorkTestChkBox_2, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_discWorkTestChkBox_2_stateChanged);
//    connect(ui->discWarnTestChkBox_2, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_discWarnTestChkBox_2_stateChanged);
//    connect(ui->discErrTestChkBox_2, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_discErrTestChkBox_2_stateChanged);

//    connect(ui->discErr24TestChkBox_2, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_discErr24TestChkBox_2_stateChanged);
//    ui->discErr24TestChkBox_2->setAttribute(Qt::WA_TransparentForMouseEvents, true);
//    connect(ui->discErr5TestChkBox_2, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_discErr5TestChkBox_2_stateChanged);
//    ui->discErr5TestChkBox_2->setAttribute(Qt::WA_TransparentForMouseEvents, true);
//    connect(ui->discErr5VsciTestChkBox_2, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_discErr5VsciTestChkBox_2_stateChanged);
//    ui->discErr5VsciTestChkBox_2->setAttribute(Qt::WA_TransparentForMouseEvents, true);
//    connect(ui->discErr5VATestChkBox_2, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_discErr5VATestChkBox_2_stateChanged);
//    ui->discErr5VATestChkBox_2->setAttribute(Qt::WA_TransparentForMouseEvents, true);

//    connect(ui->ledWorkTestChkBox_2, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_ledWorkTestChkBox_2_stateChanged);
//    connect(ui->ledWarnTestChkBox_2, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_ledWarnTestChkBox_2_stateChanged);
//    connect(ui->ledErrTestChkBox_2, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_ledErrTestChkBox_2_stateChanged);
//    connect(ui->ledConnectTestChkBox_2, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_ledConnectTestChkBox_2_stateChanged);
//    connect(ui->ledVH1TestChkBox_2, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_ledVH1TestChkBox_2_stateChanged);
//    connect(ui->ledVH2TestChkBox_2, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_ledVH2TestChkBox_2_stateChanged);
//    connect(ui->ledVH3TestChkBox_2, &QCheckBox::stateChanged, this, &DebugTerminalDialog::on_ledVH3TestChkBox_2_stateChanged);

}

void DebugTerminalDialog::updateConnectionStatus(int boardID, bool connected)
{
    // Обновляем визуальное отображение статуса соединения
    // Можно изменить цвет рамки или добавить индикатор
    Q_UNUSED(boardID);
    Q_UNUSED(connected);
    // Реализация по необходимости
}

void DebugTerminalDialog::on_buttonBox_clicked(QAbstractButton *button)
{
    switch (ui->buttonBox->buttonRole(button)) {
    case QDialogButtonBox::ResetRole:
        resetAll();
        break;
    case QDialogButtonBox::AcceptRole:
        accept();
        break;
    default:
        break;
    }
}
// Реализация слотов для вызова функций платы 1
void DebugTerminalDialog::on_continiusCallChkBox_1_stateChanged(int state)
{
    writeCoil(0, COIL_CONTINUOUS_CALL, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_calibrateCallChkBox_1_stateChanged(int state)
{
    writeCoil(0, COIL_CALIBRATE_CALL, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_pollTECallChkBox_1_stateChanged(int state)
{
    writeCoil(0, COIL_POLL_TE_CALL, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_resetKeyCallChkBox_1_stateChanged(int state)
{
    writeCoil(0, COIL_RESET_KEYS_CALL, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_resetDefaultCallChkBox_1_stateChanged(int state)
{
    writeCoil(0, COIL_RESET_DEFAULT_CALL, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_getHardfaultCallChkBox_1_stateChanged(int state)
{
    writeCoil(0, COIL_HARDFAULT_CALL, state == Qt::Checked ? 1 : 0);
}

// Реализация слотов для теста дискретных сигналов платы 1
void DebugTerminalDialog::on_enableLedTestChkBox_1_stateChanged(int state)
{
    writeCoil(0, COIL_LED_TEST_ENABLE, state == Qt::Checked ? 1 : 0);
    ui->leds_1->setEnabled(state);
    ui->discs_1->setEnabled(state);
}

void DebugTerminalDialog::on_discWorkTestChkBox_1_stateChanged(int state)
{
    writeCoil(0, COIL_DISC_WORK_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_discWarnTestChkBox_1_stateChanged(int state)
{
    writeCoil(0, COIL_DISC_WARN_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_discErrTestChkBox_1_stateChanged(int state)
{
    writeCoil(0, COIL_DISC_ERR_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_discErr24TestChkBox_1_stateChanged(int state){Q_UNUSED(state)}

void DebugTerminalDialog::on_discErr5TestChkBox_1_stateChanged(int state){Q_UNUSED(state)}

void DebugTerminalDialog::on_discErr5VsciTestChkBox_1_stateChanged(int state){Q_UNUSED(state)}

void DebugTerminalDialog::on_discErr5VATestChkBox_1_stateChanged(int state){Q_UNUSED(state)}

// Реализация слотов для теста светодиодов платы 1
void DebugTerminalDialog::on_ledWorkTestChkBox_1_stateChanged(int state)
{
    writeCoil(0, COIL_LED_WORK_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_ledWarnTestChkBox_1_stateChanged(int state)
{
    writeCoil(0, COIL_LED_WARN_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_ledErrTestChkBox_1_stateChanged(int state)
{
    writeCoil(0, COIL_LED_ERR_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_ledConnectTestChkBox_1_stateChanged(int state)
{
    writeCoil(0, COIL_LED_CONNECT_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_ledVH1TestChkBox_1_stateChanged(int state)
{
    writeCoil(0, COIL_LED_VH1_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_ledVH2TestChkBox_1_stateChanged(int state)
{
    writeCoil(0, COIL_LED_VH2_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_ledVH3TestChkBox_1_stateChanged(int state)
{
    writeCoil(0, COIL_LED_VH3_TEST, state == Qt::Checked ? 1 : 0);
}

// Реализация слотов для вызова функций платы 2
void DebugTerminalDialog::on_continiusCallChkBox_2_stateChanged(int state)
{
    writeCoil(1, COIL_CONTINUOUS_CALL, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_calibrateCallChkBox_2_stateChanged(int state)
{
    writeCoil(1, COIL_CALIBRATE_CALL, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_pollTECallChkBox_2_stateChanged(int state)
{
    writeCoil(1, COIL_POLL_TE_CALL, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_resetKeyCallChkBox_2_stateChanged(int state)
{
    writeCoil(1, COIL_RESET_KEYS_CALL, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_resetDefaultCallChkBox_2_stateChanged(int state)
{
    writeCoil(1, COIL_RESET_DEFAULT_CALL, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_getHardfaultCallChkBox_2_stateChanged(int state)
{
    writeCoil(1, COIL_HARDFAULT_CALL, state == Qt::Checked ? 1 : 0);
}

// Реализация слотов для теста дискретных сигналов платы 2
void DebugTerminalDialog::on_enableLedTestChkBox_2_stateChanged(int state)
{
    writeCoil(1, COIL_LED_TEST_ENABLE, state == Qt::Checked ? 1 : 0);
    ui->leds_2->setEnabled(state);
    ui->discs_2->setEnabled(state);
}

void DebugTerminalDialog::on_discWorkTestChkBox_2_stateChanged(int state)
{
    writeCoil(1, COIL_DISC_WORK_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_discWarnTestChkBox_2_stateChanged(int state)
{
    writeCoil(1, COIL_DISC_WARN_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_discErrTestChkBox_2_stateChanged(int state)
{
    writeCoil(1, COIL_DISC_ERR_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_discErr24TestChkBox_2_stateChanged(int state){Q_UNUSED(state)}

void DebugTerminalDialog::on_discErr5TestChkBox_2_stateChanged(int state){Q_UNUSED(state)}

void DebugTerminalDialog::on_discErr5VsciTestChkBox_2_stateChanged(int state){Q_UNUSED(state)}

void DebugTerminalDialog::on_discErr5VATestChkBox_2_stateChanged(int state){Q_UNUSED(state)}

// Реализация слотов для теста светодиодов платы 2
void DebugTerminalDialog::on_ledWorkTestChkBox_2_stateChanged(int state)
{
    writeCoil(1, COIL_LED_WORK_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_ledWarnTestChkBox_2_stateChanged(int state)
{
    writeCoil(1, COIL_LED_WARN_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_ledErrTestChkBox_2_stateChanged(int state)
{
    writeCoil(1, COIL_LED_ERR_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_ledConnectTestChkBox_2_stateChanged(int state)
{
    writeCoil(1, COIL_LED_CONNECT_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_ledVH1TestChkBox_2_stateChanged(int state)
{
    writeCoil(1, COIL_LED_VH1_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_ledVH2TestChkBox_2_stateChanged(int state)
{
    writeCoil(1, COIL_LED_VH2_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_ledVH3TestChkBox_2_stateChanged(int state)
{
    writeCoil(1, COIL_LED_VH3_TEST, state == Qt::Checked ? 1 : 0);
}

// Реализация слотов для вызова функций платы 3
void DebugTerminalDialog::on_continiusCallChkBox_3_stateChanged(int state)
{
    writeCoil(2, COIL_CONTINUOUS_CALL, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_calibrateCallChkBox_3_stateChanged(int state)
{
    writeCoil(2, COIL_CALIBRATE_CALL, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_pollTECallChkBox_3_stateChanged(int state)
{
    writeCoil(2, COIL_POLL_TE_CALL, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_resetKeyCallChkBox_3_stateChanged(int state)
{
    writeCoil(2, COIL_RESET_KEYS_CALL, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_resetDefaultCallChkBox_3_stateChanged(int state)
{
    writeCoil(2, COIL_RESET_DEFAULT_CALL, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_getHardfaultCallChkBox_3_stateChanged(int state)
{
    writeCoil(2, COIL_HARDFAULT_CALL, state == Qt::Checked ? 1 : 0);
}

// Реализация слотов для теста дискретных сигналов платы 3
void DebugTerminalDialog::on_enableLedTestChkBox_3_stateChanged(int state)
{
    writeCoil(2, COIL_LED_TEST_ENABLE, state == Qt::Checked ? 1 : 0);
    ui->leds_3->setEnabled(state);
    ui->discs_3->setEnabled(state);
}

void DebugTerminalDialog::on_discWorkTestChkBox_3_stateChanged(int state)
{
    writeCoil(2, COIL_DISC_WORK_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_discWarnTestChkBox_3_stateChanged(int state)
{
    writeCoil(2, COIL_DISC_WARN_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_discErrTestChkBox_3_stateChanged(int state)
{
    writeCoil(2, COIL_DISC_ERR_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_discErr24TestChkBox_3_stateChanged(int state){Q_UNUSED(state)}

void DebugTerminalDialog::on_discErr5TestChkBox_3_stateChanged(int state){Q_UNUSED(state)}

void DebugTerminalDialog::on_discErr5VsciTestChkBox_3_stateChanged(int state){Q_UNUSED(state)}

void DebugTerminalDialog::on_discErr5VATestChkBox_3_stateChanged(int state){Q_UNUSED(state)}

// Реализация слотов для теста светодиодов платы 3
void DebugTerminalDialog::on_ledWorkTestChkBox_3_stateChanged(int state)
{
    writeCoil(2, COIL_LED_WORK_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_ledWarnTestChkBox_3_stateChanged(int state)
{
    writeCoil(2, COIL_LED_WARN_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_ledErrTestChkBox_3_stateChanged(int state)
{
    writeCoil(2, COIL_LED_ERR_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_ledConnectTestChkBox_3_stateChanged(int state)
{
    writeCoil(2, COIL_LED_CONNECT_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_ledVH1TestChkBox_3_stateChanged(int state)
{
    writeCoil(2, COIL_LED_VH1_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_ledVH2TestChkBox_3_stateChanged(int state)
{
    writeCoil(2, COIL_LED_VH2_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_ledVH3TestChkBox_3_stateChanged(int state)
{
    writeCoil(2, COIL_LED_VH3_TEST, state == Qt::Checked ? 1 : 0);
}

// Реализация слотов для вызова функций платы 4
void DebugTerminalDialog::on_continiusCallChkBox_4_stateChanged(int state)
{
    writeCoil(3, COIL_CONTINUOUS_CALL, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_calibrateCallChkBox_4_stateChanged(int state)
{
    writeCoil(3, COIL_CALIBRATE_CALL, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_pollTECallChkBox_4_stateChanged(int state)
{
    writeCoil(3, COIL_POLL_TE_CALL, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_resetKeyCallChkBox_4_stateChanged(int state)
{
    writeCoil(3, COIL_RESET_KEYS_CALL, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_resetDefaultCallChkBox_4_stateChanged(int state)
{
    writeCoil(3, COIL_RESET_DEFAULT_CALL, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_getHardfaultCallChkBox_4_stateChanged(int state)
{
    writeCoil(3, COIL_HARDFAULT_CALL, state == Qt::Checked ? 1 : 0);
}

// Реализация слотов для теста дискретных сигналов платы 4
void DebugTerminalDialog::on_enableLedTestChkBox_4_stateChanged(int state)
{
    writeCoil(3, COIL_LED_TEST_ENABLE, state == Qt::Checked ? 1 : 0);
    ui->leds_4->setEnabled(state);
    ui->discs_4->setEnabled(state);
}

void DebugTerminalDialog::on_discWorkTestChkBox_4_stateChanged(int state)
{
    writeCoil(3, COIL_DISC_WORK_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_discWarnTestChkBox_4_stateChanged(int state)
{
    writeCoil(3, COIL_DISC_WARN_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_discErrTestChkBox_4_stateChanged(int state)
{
    writeCoil(3, COIL_DISC_ERR_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_discErr24TestChkBox_4_stateChanged(int state){Q_UNUSED(state)}

void DebugTerminalDialog::on_discErr5TestChkBox_4_stateChanged(int state){Q_UNUSED(state)}

void DebugTerminalDialog::on_discErr5VsciTestChkBox_4_stateChanged(int state){Q_UNUSED(state)}

void DebugTerminalDialog::on_discErr5VATestChkBox_4_stateChanged(int state){Q_UNUSED(state)}

// Реализация слотов для теста светодиодов платы 4
void DebugTerminalDialog::on_ledWorkTestChkBox_4_stateChanged(int state)
{
    writeCoil(3, COIL_LED_WORK_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_ledWarnTestChkBox_4_stateChanged(int state)
{
    writeCoil(3, COIL_LED_WARN_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_ledErrTestChkBox_4_stateChanged(int state)
{
    writeCoil(3, COIL_LED_ERR_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_ledConnectTestChkBox_4_stateChanged(int state)
{
    writeCoil(3, COIL_LED_CONNECT_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_ledVH1TestChkBox_4_stateChanged(int state)
{
    writeCoil(3, COIL_LED_VH1_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_ledVH2TestChkBox_4_stateChanged(int state)
{
    writeCoil(3, COIL_LED_VH2_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::on_ledVH3TestChkBox_4_stateChanged(int state)
{
    writeCoil(3, COIL_LED_VH3_TEST, state == Qt::Checked ? 1 : 0);
}

void DebugTerminalDialog::openAdc(int boardID, int teNumber)
{
    // Удаляем старый диалог и создаем новый
    if (m_adcGraphDialog) {
        m_adcGraphDialog->deleteLater();
        m_adcGraphDialog = nullptr;
    }

    m_adcGraphDialog = new AdcGraphDialog(m_modbusDevice, this);
    connect(m_adcGraphDialog, &AdcGraphDialog::dialogClosed, this, [this]() {
        setDebugTerminalCoil(0);
    });

    setGraphUpdateInterval(1000);
    m_adcGraphDialog->startGraph(boardID, teNumber);
    m_adcGraphDialog->show();
    m_adcGraphDialog->raise();
    m_adcGraphDialog->activateWindow();
}

void DebugTerminalDialog::setGraphUpdateInterval(int milliseconds)
{
    if (m_adcGraphDialog) {
       m_adcGraphDialog->setTimeout(milliseconds);
    }
}

void DebugTerminalDialog::writeCoil(int boardID, int coil, int value)
{
    QGroupBox* boardGroup = nullptr;
    switch(boardID) {
    case 0: boardGroup = ui->DbgPlate_1; break;     // Плата 1
    case 1: boardGroup = ui->DbgPlate_2; break;    // Плата 2
    case 2: boardGroup = ui->DbgPlate_3; break;   // Плата 3
    case 3: boardGroup = ui->DbgPlate_4; break;  // Плата 4
    default: return;
    }
    if(!boardGroup->isEnabled())
        return;
    qDebug() << "Writing board" << boardID << "coil:" << coil << "value:" << value;
    emit coilValueChanged(boardID, coil, value);
}

void DebugTerminalDialog::writeTENumber(int boardId, int teNumber)
{
    m_adcGraphDialog->setTENumber(boardId, teNumber);
}

void DebugTerminalDialog::setBoardActive(int boardID, bool active)
{
    // Получаем групбокс для указанной платы
    QGroupBox* boardGroup = nullptr;
    switch(boardID) {
    case 0: boardGroup = ui->DbgPlate_1; break;     // Плата 1
    case 1: boardGroup = ui->DbgPlate_2; break;    // Плата 2
    case 2: boardGroup = ui->DbgPlate_3; break;   // Плата 3
    case 3: boardGroup = ui->DbgPlate_4; break;  // Плата 4
    default: return;
    }
    if (boardGroup) {
        boardGroup->setEnabled(active);
        // Можно добавить визуальное отличие неактивных плат
        if (!active) {
            boardGroup->setStyleSheet("QGroupBox { color: gray; }");
        } else {
            boardGroup->setStyleSheet(""); // Сброс стиля
        }
    }
}

void DebugTerminalDialog::updateBoardStates(bool activeBoards[4])
{
    for (int i = 0; i < 4; i++) {
        setBoardActive(i, activeBoards[i]);
    }
}

void DebugTerminalDialog::resetAll()
{
    // Сброс всех чекбоксов вызова функций
    ui->continiusCallChkBox_1->setChecked(false);
    ui->calibrateCallChkBox_1->setChecked(false);
    ui->pollTECallChkBox_1->setChecked(false);
    ui->resetKeyCallChkBox_1->setChecked(false);
    ui->resetDefaultCallChkBox_1->setChecked(false);
    ui->getHardfaultCallChkBox_1->setChecked(false);

    ui->enableLedTestChkBox_1->setChecked(false);
    ui->leds_1->setEnabled(false);
    ui->discs_1->setEnabled(false);

    ui->discWorkTestChkBox_1->setChecked(false);
    // Сброс всех чекбоксов теста дискретных сигналов
    ui->discWorkTestChkBox_1->setChecked(false);
    ui->discWarnTestChkBox_1->setChecked(false);
    ui->discErrTestChkBox_1->setChecked(false);

    // Сброс всех чекбоксов теста светодиодов
    ui->ledWorkTestChkBox_1->setChecked(false);
    ui->ledWarnTestChkBox_1->setChecked(false);
    ui->ledErrTestChkBox_1->setChecked(false);
    ui->ledConnectTestChkBox_1->setChecked(false);
    ui->ledVH1TestChkBox_1->setChecked(false);
    ui->ledVH2TestChkBox_1->setChecked(false);
    ui->ledVH3TestChkBox_1->setChecked(false);

    // Сброс всех чекбоксов вызова функций
    ui->continiusCallChkBox_2->setChecked(false);
    ui->calibrateCallChkBox_2->setChecked(false);
    ui->pollTECallChkBox_2->setChecked(false);
    ui->resetKeyCallChkBox_2->setChecked(false);
    ui->resetDefaultCallChkBox_2->setChecked(false);
    ui->getHardfaultCallChkBox_2->setChecked(false);

    ui->enableLedTestChkBox_2->setChecked(false);
    ui->leds_2->setEnabled(false);
    ui->discs_2->setEnabled(false);

    ui->discWorkTestChkBox_2->setChecked(false);
    // Сброс всех чекбоксов теста дискретных сигналов
    ui->discWorkTestChkBox_2->setChecked(false);
    ui->discWarnTestChkBox_2->setChecked(false);
    ui->discErrTestChkBox_2->setChecked(false);

    // Сброс всех чекбоксов теста светодиодов
    ui->ledWorkTestChkBox_2->setChecked(false);
    ui->ledWarnTestChkBox_2->setChecked(false);
    ui->ledErrTestChkBox_2->setChecked(false);
    ui->ledConnectTestChkBox_2->setChecked(false);
    ui->ledVH1TestChkBox_2->setChecked(false);
    ui->ledVH2TestChkBox_2->setChecked(false);
    ui->ledVH3TestChkBox_2->setChecked(false);

    // Сброс всех чекбоксов вызова функций
    ui->continiusCallChkBox_3->setChecked(false);
    ui->calibrateCallChkBox_3->setChecked(false);
    ui->pollTECallChkBox_3->setChecked(false);
    ui->resetKeyCallChkBox_3->setChecked(false);
    ui->resetDefaultCallChkBox_3->setChecked(false);
    ui->getHardfaultCallChkBox_3->setChecked(false);

    ui->enableLedTestChkBox_3->setChecked(false);
    ui->leds_3->setEnabled(false);
    ui->discs_3->setEnabled(false);

    ui->discWorkTestChkBox_3->setChecked(false);
    // Сброс всех чекбоксов теста дискретных сигналов
    ui->discWorkTestChkBox_3->setChecked(false);
    ui->discWarnTestChkBox_3->setChecked(false);
    ui->discErrTestChkBox_3->setChecked(false);

    // Сброс всех чекбоксов теста светодиодов
    ui->ledWorkTestChkBox_3->setChecked(false);
    ui->ledWarnTestChkBox_3->setChecked(false);
    ui->ledErrTestChkBox_3->setChecked(false);
    ui->ledConnectTestChkBox_3->setChecked(false);
    ui->ledVH1TestChkBox_3->setChecked(false);
    ui->ledVH2TestChkBox_3->setChecked(false);
    ui->ledVH3TestChkBox_3->setChecked(false);

    // Сброс всех чекбоксов вызова функций
    ui->continiusCallChkBox_4->setChecked(false);
    ui->calibrateCallChkBox_4->setChecked(false);
    ui->pollTECallChkBox_4->setChecked(false);
    ui->resetKeyCallChkBox_4->setChecked(false);
    ui->resetDefaultCallChkBox_4->setChecked(false);
    ui->getHardfaultCallChkBox_4->setChecked(false);

    ui->enableLedTestChkBox_4->setChecked(false);
    ui->leds_4->setEnabled(false);
    ui->discs_4->setEnabled(false);

    ui->discWorkTestChkBox_4->setChecked(false);
    // Сброс всех чекбоксов теста дискретных сигналов
    ui->discWorkTestChkBox_4->setChecked(false);
    ui->discWarnTestChkBox_4->setChecked(false);
    ui->discErrTestChkBox_4->setChecked(false);

    // Сброс всех чекбоксов теста светодиодов
    ui->ledWorkTestChkBox_4->setChecked(false);
    ui->ledWarnTestChkBox_4->setChecked(false);
    ui->ledErrTestChkBox_4->setChecked(false);
    ui->ledConnectTestChkBox_4->setChecked(false);
    ui->ledVH1TestChkBox_4->setChecked(false);
    ui->ledVH2TestChkBox_4->setChecked(false);
    ui->ledVH3TestChkBox_4->setChecked(false);
}

void DebugTerminalDialog::boardDebugReading(int boardID)
{
    if(mainTerm == nullptr)
        return;

    if(!boards[boardID].isActive)
        return;

    if(!this->isVisible())
        return;

    QModbusReply *_24V = mainTerm->readSingleCoil(boardID, 603);
    if(_24V != nullptr)
        connect(_24V, &QModbusReply::finished, this, [this, boardID, _24V]() {
            if(_24V->error() == QModbusDevice::NoError)
                boards[boardID].error24V->setChecked(_24V->result().value(0));
            _24V->deleteLater();
        });

    QModbusReply *_5V = mainTerm->readSingleCoil(boardID, 604);
    if(_5V != nullptr)
        connect(_5V, &QModbusReply::finished, this, [this, boardID, _5V]() {
            if(_5V->error() == QModbusDevice::NoError)
                boards[boardID].error5V->setChecked(_5V->result().value(0));
            _5V->deleteLater();
        });

    QModbusReply *_5VSCI = mainTerm->readSingleCoil(boardID, 605);
    if(_5VSCI != nullptr)
        connect(_5VSCI, &QModbusReply::finished, this, [this, boardID, _5VSCI]() {
            if(_5VSCI->error() == QModbusDevice::NoError)
                boards[boardID].error5VSCI->setChecked(_5VSCI->result().value(0));
            _5VSCI->deleteLater();
        });

    QModbusReply *_5VA = mainTerm->readSingleCoil(boardID, 606);
    if(_5VA != nullptr)
        connect(_5VA, &QModbusReply::finished, this, [this, boardID, _5VA]() {
            if(_5VA->error() == QModbusDevice::NoError)
                boards[boardID].error5VA->setChecked(_5VA->result().value(0));
            _5VA->deleteLater();
        });
}

void DebugTerminalDialog::setScanBoardActive(bool flag, int boardID)
{
    boards[boardID].isActive = flag;
}

void DebugTerminalDialog::offAllBoard()
{
    for(int i = 0; i < 4; i++)
    {
        boards[i].isActive = false;
    }
}
