#ifndef QVIDEOWIDGET_H
#define QVIDEOWIDGET_H

#include <QOpenGLWidget>
#include <QOpenGLFunctions>
#include <QOpenGLBuffer>
#include <QVector3D>
#include <QMatrix4x4>
#include <QTime>
#include <QVector>
#include <QPushButton>
#include <QTimer>

#include <QElapsedTimer>

#include <unistd.h>
#include <sys/wait.h>
#include <stdio.h>

#include <QMutex>

#include <vector>
using namespace std;

#include "qglslshader.h"

#include <qopenglframebufferobject.h>
#include <QOffscreenSurface>

//#include "qthreadthumbnail.h"

#include "qopengltexturebutton.h"

#include <QMouseEvent>

#include "qopenglbufferthumbnail.h"
//#include "qglslshadercaptureevent.h"
#include "qopenglbuffermenu.h"
using namespace SOPENGL;

class QVideoWidget : public QOpenGLWidget, protected QOpenGLFunctions
{
    Q_OBJECT
public:
    explicit QVideoWidget(QWidget* parent = 0, Qt::WindowFlags f = 0);
    ~QVideoWidget();

    virtual void Init();
    virtual void Exit();

    void ClearQueueData();

    void UpdateTexture();

    void SetTextureData(uint8_t* pData, int nSize);
    QMutex* GetMutex();

    uint8_t* GetDataPtr();

    int GetTextureWidth();
    int GetTextureHeight();

    void SetTextureSize(int nWidth, int nHeight);

    void SetSmallDisplay(int nX, int nY, int nWidth, int nHeight);

    void ResetThumbnailTexture();

    void SaveBitmap(QString strFilename);

    //QOpenGLTexture* m_pThumbnailTexture;


    float MakeNormalize(int nPos, int nRange);

    void MakeRectNormal(const QRect& rect, QSize rectSize);


    virtual void InitState();

    void DrawRecord();
    void DrawCapture();

    void SetCapturing();

    uint8_t* GetCaptureImage();

    void UpdateObjectDisplay(vector<QRect>& listRectDisplay);

    bool Lock();
    void Unlock();

    void Reset();

    void SetChangeTexture(bool bUpdate);



public slots:
    void setTransparent(bool transparent);

    void SetButton(QPushButton* pButton);

    void CreatedThumbnail(QString strFilenameThumbnail);

    void ReceivedSaveBitmap();
    void ReceivedRecord(bool bRecord);
    void ReceivedViewThumbnail(bool bView);

    void Update();

signals:
    void SendSaveBitmap();
    void SendRecord(bool bRecord);



protected:
    void resizeGL(int w, int h) Q_DECL_OVERRIDE;
    void paintGL() Q_DECL_OVERRIDE;
    virtual void initializeGL() Q_DECL_OVERRIDE;
    virtual void leaveEvent(QEvent * pEvent);

    virtual void mouseDoubleClickEvent(QMouseEvent *);
    virtual void mouseMoveEvent(QMouseEvent * pEvent);
    virtual void mousePressEvent(QMouseEvent * pEvent);

protected:

    bool m_bCheckFrameUpdateCount;

    int m_nCountQueue;
    vector<QRect> m_listRectObject;

    //bool m_bViewThumbnail;
    //QOpenGLBufferThumbnail* m_pThumbnail;
    //QOpenGLBufferMenu* m_pMenu;
    QMutex m_MutexPaint;
    bool m_bRecording;
    bool m_bCapturing;
    GLfloat m_fNormalRect[18];
    GLfloat m_fNormalRectTexture[12];

    void paintTexturedCube();
    //void createGeometry();
    //void quad(qreal x1, qreal y1, qreal x2, qreal y2, qreal x3, qreal y3, qreal x4, qreal y4);
    //void extrude(qreal x1, qreal y1, qreal x2, qreal y2);

    QTime m_StartTime;

    int m_nCaptureEventTime;

    QRect m_rectSmallDisplay;
    bool m_bFullScreen;
    int m_nUpdateFrameCount;
    //QTime m_time;
    QElapsedTimer m_TimerFPS;

    QGLSLShader* m_pVideoShader;

    QTimer m_Timer;


    bool m_bInitDisplay;

    QOpenGLBuffer m_VBOVideo;


    bool m_transparent;
    QPushButton *m_btn;
    bool m_hasButton;
    QColor m_background;



    int m_nTmpDisplayType;

    pid_t m_pidChild;

    bool m_bTestSaveMode;

    int m_nTextureWidth;
    int m_nTextureHeight;

    QMutex m_Lock;

    uint8_t* m_pData;

    vector<uint8_t*> m_QueueData;
    int m_nIndexCurrent;
    int m_nIndexDisplay;

    bool m_bChange;

    //QThreadThumbnail* m_pThreadThumbnail;

};

#endif // QVIDEOWIDGET_H
