#ifndef DEBUGTERMINALDIALOG_H
#define DEBUGTERMINALDIALOG_H

#include <QDialog>
#include <QModbusClient>
#include <QAbstractButton>
#include <QCheckBox>
#include <m3kte.h>

// Forward declarations вместо include
class M3KTE;
class AdcGraphDialog;

// Дефайны для адресов коилов
#define COIL_DEBUG_MODE         555

#define COIL_CONTINUOUS_CALL    571
#define COIL_CALIBRATE_CALL     572
#define COIL_POLL_TE_CALL       573
#define COIL_RESET_KEYS_CALL    574
#define COIL_RESET_DEFAULT_CALL 576
#define COIL_HARDFAULT_CALL     586

#define COIL_LED_TEST_ENABLE    587

// Тест дискретных сигналов
#define COIL_DISC_WORK_TEST     588
#define COIL_DISC_WARN_TEST     589
#define COIL_DISC_ERR_TEST      590

// Тест светодиодов
#define COIL_LED_CONNECT_TEST   591
#define COIL_LED_WORK_TEST      592
#define COIL_LED_WARN_TEST      593
#define COIL_LED_ERR_TEST       594
#define COIL_LED_VH1_TEST       595
#define COIL_LED_VH2_TEST       596
#define COIL_LED_VH3_TEST       597

// Адреса для чтения состояний ошибок питания
#define COIL_READ_ERR_24V       600  // Чтение ошибки 24В
#define COIL_READ_ERR_5V        601  // Чтение ошибки 5В
#define COIL_READ_ERR_5VSCI     602  // Чтение ошибки 5Vsci
#define COIL_READ_ERR_5VA       603  // Чтение ошибки 5VA

#define REGISTER_TE_NUMB        564



namespace Ui {
class DebugTerminalDialog;
}

class DebugTerminalDialog : public QDialog
{
    Q_OBJECT

public:
    explicit DebugTerminalDialog(QWidget *parent = nullptr);
    ~DebugTerminalDialog();
    AdcGraphDialog *m_adcGraphDialog;
    void setDebugTerminalCoil(int enable);
    void updateConnectionStatus(int boardId, bool connected);
    void setBoardActive(int boardId, bool active);
    void updateBoardStates(bool activeBoards[4]);
    void setModbusDevice(QModbusClient *device);
    void setGraphUpdateInterval(int milliseconds);
    void writeTENumber(int boardId, int teNumber);
    void openAdc(int boardID, int teNumber);
    void setMainTerm(M3KTE* term);

public slots:
    void boardDebugReading(int boardID);
    void setScanBoardActive(bool flag, int boardID);
    void offAllBoard();

protected:
    void showEvent(QShowEvent *event) override;
    void closeEvent(QCloseEvent *event) override;

private slots:


    void on_buttonBox_clicked(QAbstractButton *button);

    // Плата 1
    void on_continiusCallChkBox_1_stateChanged(int state);
    void on_calibrateCallChkBox_1_stateChanged(int state);
    void on_pollTECallChkBox_1_stateChanged(int state);
    void on_resetKeyCallChkBox_1_stateChanged(int state);
    void on_resetDefaultCallChkBox_1_stateChanged(int state);
    void on_getHardfaultCallChkBox_1_stateChanged(int state);

    void on_enableLedTestChkBox_1_stateChanged(int state);

    void on_discWorkTestChkBox_1_stateChanged(int state);
    void on_discWarnTestChkBox_1_stateChanged(int state);
    void on_discErrTestChkBox_1_stateChanged(int state);
    void on_discErr24TestChkBox_1_stateChanged(int state);
    void on_discErr5TestChkBox_1_stateChanged(int state);
    void on_discErr5VsciTestChkBox_1_stateChanged(int state);
    void on_discErr5VATestChkBox_1_stateChanged(int state);

    void on_ledWorkTestChkBox_1_stateChanged(int state);
    void on_ledWarnTestChkBox_1_stateChanged(int state);
    void on_ledErrTestChkBox_1_stateChanged(int state);
    void on_ledConnectTestChkBox_1_stateChanged(int state);
    void on_ledVH1TestChkBox_1_stateChanged(int state);
    void on_ledVH2TestChkBox_1_stateChanged(int state);
    void on_ledVH3TestChkBox_1_stateChanged(int state);

    // Плата 2
    void on_continiusCallChkBox_2_stateChanged(int state);
    void on_calibrateCallChkBox_2_stateChanged(int state);
    void on_pollTECallChkBox_2_stateChanged(int state);
    void on_resetKeyCallChkBox_2_stateChanged(int state);
    void on_resetDefaultCallChkBox_2_stateChanged(int state);
    void on_getHardfaultCallChkBox_2_stateChanged(int state);

    void on_enableLedTestChkBox_2_stateChanged(int state);

    void on_discWorkTestChkBox_2_stateChanged(int state);
    void on_discWarnTestChkBox_2_stateChanged(int state);
    void on_discErrTestChkBox_2_stateChanged(int state);
    void on_discErr24TestChkBox_2_stateChanged(int state);
    void on_discErr5TestChkBox_2_stateChanged(int state);
    void on_discErr5VsciTestChkBox_2_stateChanged(int state);
    void on_discErr5VATestChkBox_2_stateChanged(int state);

    void on_ledWorkTestChkBox_2_stateChanged(int state);
    void on_ledWarnTestChkBox_2_stateChanged(int state);
    void on_ledErrTestChkBox_2_stateChanged(int state);
    void on_ledConnectTestChkBox_2_stateChanged(int state);
    void on_ledVH1TestChkBox_2_stateChanged(int state);
    void on_ledVH2TestChkBox_2_stateChanged(int state);
    void on_ledVH3TestChkBox_2_stateChanged(int state);


    // Плата 3
    void on_continiusCallChkBox_3_stateChanged(int state);
    void on_calibrateCallChkBox_3_stateChanged(int state);
    void on_pollTECallChkBox_3_stateChanged(int state);
    void on_resetKeyCallChkBox_3_stateChanged(int state);
    void on_resetDefaultCallChkBox_3_stateChanged(int state);
    void on_getHardfaultCallChkBox_3_stateChanged(int state);

    void on_enableLedTestChkBox_3_stateChanged(int state);

    void on_discWorkTestChkBox_3_stateChanged(int state);
    void on_discWarnTestChkBox_3_stateChanged(int state);
    void on_discErrTestChkBox_3_stateChanged(int state);
    void on_discErr24TestChkBox_3_stateChanged(int state);
    void on_discErr5TestChkBox_3_stateChanged(int state);
    void on_discErr5VsciTestChkBox_3_stateChanged(int state);
    void on_discErr5VATestChkBox_3_stateChanged(int state);

    void on_ledWorkTestChkBox_3_stateChanged(int state);
    void on_ledWarnTestChkBox_3_stateChanged(int state);
    void on_ledErrTestChkBox_3_stateChanged(int state);
    void on_ledConnectTestChkBox_3_stateChanged(int state);
    void on_ledVH1TestChkBox_3_stateChanged(int state);
    void on_ledVH2TestChkBox_3_stateChanged(int state);
    void on_ledVH3TestChkBox_3_stateChanged(int state);

    // Плата 4
    void on_continiusCallChkBox_4_stateChanged(int state);
    void on_calibrateCallChkBox_4_stateChanged(int state);
    void on_pollTECallChkBox_4_stateChanged(int state);
    void on_resetKeyCallChkBox_4_stateChanged(int state);
    void on_resetDefaultCallChkBox_4_stateChanged(int state);
    void on_getHardfaultCallChkBox_4_stateChanged(int state);

    void on_enableLedTestChkBox_4_stateChanged(int state);

    void on_discWorkTestChkBox_4_stateChanged(int state);
    void on_discWarnTestChkBox_4_stateChanged(int state);
    void on_discErrTestChkBox_4_stateChanged(int state);
    void on_discErr24TestChkBox_4_stateChanged(int state);
    void on_discErr5TestChkBox_4_stateChanged(int state);
    void on_discErr5VsciTestChkBox_4_stateChanged(int state);
    void on_discErr5VATestChkBox_4_stateChanged(int state);

    void on_ledWorkTestChkBox_4_stateChanged(int state);
    void on_ledWarnTestChkBox_4_stateChanged(int state);
    void on_ledErrTestChkBox_4_stateChanged(int state);
    void on_ledConnectTestChkBox_4_stateChanged(int state);
    void on_ledVH1TestChkBox_4_stateChanged(int state);
    void on_ledVH2TestChkBox_4_stateChanged(int state);
    void on_ledVH3TestChkBox_4_stateChanged(int state);

signals:
    void coilValueChanged(int boardID, int coil, int value);
    void writeRegister(int boardID, int reg, int value);
    void readCoil(int boardID, int coil, QModbusReply *reply);

private:
    Ui::DebugTerminalDialog *ui;
    QModbusClient *m_modbusDevice;  // Храним указатель здесь
    M3KTE* mainTerm = nullptr;

    // Карты для хранения состояний
    QMap<int, bool> m_functionCalls; // boardId -> coil -> state
    QMap<int, bool> m_discreteTests; // boardId -> coil -> state
    QMap<int, bool> m_ledTests;      // boardId -> coil -> state

    // Номера ТЭ для каждой платы
    int m_teNumbers[4] = {0, 0, 0, 0};

    struct boardErrorLinks{
        bool isActive = false;
        QCheckBox* error24V = nullptr;
        QCheckBox* error5V = nullptr;
        QCheckBox* error5VSCI = nullptr;
        QCheckBox* error5VA = nullptr;
    };
    boardErrorLinks boards[4];

    void initializeConnections();

    void writeCoil(int boardID, int coil, int value);
    //void readCoil(int coil);
    void resetAll();
};

#endif // DEBUGTERMINALDIALOG_H
