#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QImage>
#include <QFileDialog>
#include <QFile>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QProcess>
#include <QDir>
#include <QObject>
#include <QMessageBox>
#include <QProgressDialog>
#include <QTime>
#include <QTimer>
#include <QLocalSocket>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QSystemSemaphore>
#include <QThread>

#ifdef _WIN32
#include <qt_windows.h>
#endif

QT_BEGIN_NAMESPACE

void delay(int ms);

class MainWindow;

class SemaphoreWorkerThread : public QThread
{
protected:
    void run() override;
public:
    SemaphoreWorkerThread(QObject*, MainWindow*);
private:
    MainWindow* wnd;
};

class Counter : public QObject
{
    Q_OBJECT
private:
    int counts;
    int ncounts;
public:
    Counter(int counts);
    void reset();
    void rebind(int counts);
public slots:
    void count();
signals:
    void countsEnd();
};

namespace Ui {

class MainWindow;

}
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    int cancelProgram();
    HANDLE semget();

private slots:
    void sdrFDialog();
    void syncFDialog();
    void graFDialog();
    void gra2FDialog();
    void gra3FDialog();
    void outFDialog();
    int runButtonPress();
    int saveProfilePress();
    int loadProfilePress();

    void GRA1checkBoxEvent();
    void GRA2checkBoxEvent();
    void GRA3checkBoxEvent();
    void OUTcheckBoxEvent();

    int syncFinished();
    int graFinished();
    int startHackRF();
    int hackrfFinished();
    int picoFinished();
    int readOutput();
    int programFinished();

    int analizeButtonPress();
    void plotprocFinishedSlot();
    //void picoprocFinishedSlot();

signals:
    void readyForRF();

private:
    Ui::MainWindow *ui;
    QList<QSerialPortInfo> portInfos;
    QProcess plotproc, syncproc, graproc, graproc2, graproc3, hackrfproc, picoproc;
    QStringList syncargs, graargs, graargs2, graargs3, hackrfargs, picoargs;
    Counter* graCounter;
    QProgressDialog* progress;
    int pvalue;
    QSerialPort* syncport;
    QTimer* tmrForRF;
    bool is_canceled = false;
    int test;

    #ifdef _WIN32
    HANDLE semForRF;
    #endif

    SemaphoreWorkerThread* semThread;
};

#endif // MAINWINDOW_H
