#ifndef I2C_KEYBOARD_DETECTION_H
#define I2C_KEYBOARD_DETECTION_H

#include <QMainWindow>

#include <qserialport.h>

#include <qserialportinfo.h>

#include <qprocess.h>
#include <qthread.h>

#include "qmk_kbd_list.h"
#include "qmk_parser.h"

namespace Ui {
class i2c_keyboard_detection;
}

class i2c_keyboard_detection : public QMainWindow
{
    Q_OBJECT

public:
    explicit i2c_keyboard_detection(QWidget *parent = 0);
    ~i2c_keyboard_detection();

private slots:
    void comDataReceived();
    void displayMatrix();

    void qmkKbdSelected(int rev, int keymap);

    void processReadyRead();
    void processFinished(int result, QProcess::ExitStatus exitStatus);
    void processError(QProcess::ProcessError error);

    void on_keyList_itemSelectionChanged();
    void keyPressEvent(QKeyEvent *event);

    virtual void resizeEvent(QResizeEvent *event) override;

    void on_comRefleshList_clicked();

    void on_comConnect_clicked();

    void on_enSendKeyToReport_clicked(bool checked);

    void on_saveKbdReport_clicked();

    void on_openConnectionsFile_clicked();

    void on_clearKbdReport_clicked();

    void on_swapColumnsAndRows_clicked();

    void on_mergeRows_clicked();

    void on_setFnKey_clicked();

    void on_openMatrixKbdFile_clicked();

    void on_generateFwBinFile_clicked();

    void on_saveMatrixFile_clicked();

    void on_openMatrixFile_clicked();

    void on_addNewKey_clicked();

    void on_openQmkKbd_clicked();

private:

private:
    Ui::i2c_keyboard_detection *ui;

    QVector<int> m_kbdCodes;

    QStringList m_keyComments;

    QSerialPort m_com;
    bool m_isFirstComMsg;

    QByteArray m_kbdDetectBytes;

    QVector<int64_t> m_kbdDetectData;

    QVector<QString> m_kbdDetectReport;

    QVector<QString> m_generatedColumns;
    QVector<QString> m_generatedRows;
    QVector<QVector<QString> > m_generatedMatrix;
    QVector<QVector<QString> > m_generatedFnMatrix;
    int m_generatedNotEmptyElements;
    int m_diodeDirection;   // 0 - no diodes, 1 - (+) COL and (-) ROW, 2 - (-) COL and (+) ROW

    const QString m_emptyMatrixValue = "(0)";

    QProcess* m_fpgaFwGenerator;
    QThread m_fpgaFwGeneratorTrd;

    qmk_kbd_list m_uiKbdList;

    QFileInfoList m_qmkRevisions;
    QFileInfoList m_qmkKeymaps;

    qmk_parser m_qmkParser;
};

#endif // I2C_KEYBOARD_DETECTION_H
