#ifndef VIDEOWIN_H
#define VIDEOWIN_H
#include "basewin.h"

#define HAVE_REMOTE
#include "pcap.h"
#include <winsock2.h>
#include <QThread>
#include <QSpinBox>
#include <QLabel>
#include <QRadioButton>
#include <QCheckBox>
#include "Win32-Extensions.h"

class VideoSendThread : public QThread {
    Q_OBJECT
public:
    explicit VideoSendThread(pcap *pcap);
    ~VideoSendThread() {
        pcap_close(pcap);
    }
    pcap *pcap;
    std::atomic<char> status{0};
    QList<QImage> imgs;
    std::mutex mtx;
    quint16 frameIdx{0};
    bool useOldProto{false};
protected:
    void run();
signals:
    void onErr(QString);
};
class VideoRecThread : public QThread {
    Q_OBJECT
public:
    explicit VideoRecThread(pcap *pcap);
    ~VideoRecThread() {
        pcap_close(pcap);
    }
    pcap *pcap;
    std::atomic<char> status{0};
    bool noReview{false};
    bool useOldProto{false};
protected:
    void run();
signals:
    void onMsg(const QByteArray data);
    void onImg(QImage, int, int);
    void onErr(char *);
};

class Canvas : public QWidget {
public:
    QImage img;
protected:
    void paintEvent(QPaintEvent *event);
};

class VideoWin : public BaseWin {
    Q_OBJECT
public:
    static VideoWin *newIns(QByteArray &, QWidget *);

    ~VideoWin() {
        if(thdRece) thdRece->status = 2;
        if(thdSend) thdSend->status = 2;
        if(timerId) killTimer(timerId);
    }
    VideoRecThread *thdRece{0};
    VideoSendThread *thdSend{0};
    QList<QByteArray> imgLines;
    int imgWidth{0}, imgHeight{0};
    QScreen *screen;
    int timerId{0};
    QSpinBox *fdWidth, *fdHeight, *fdMsecCapScr,*fdYxj;
    QCheckBox *fdNoReview;
    bool noReview{false};
    bool useOldProto{false};
    QRadioButton *fdEnd;
    QLabel *fdInfo, *fdLostTimes, *fdLostPkts;
    QString info;
    qint64 last_epoch{0};
    int lostTimes{0}, lostPkts{0};
protected:
    explicit VideoWin(pcap *, pcap *, QWidget *parent = nullptr);
    void timerEvent(QTimerEvent *event) override;
};

#endif // VIDEOWIN_H
