#include "elevideo.h"
#include "tools.h"
#include <QPainter>
#include <QMessageBox>
#include <QOpenGLWidget>

class GL : public QOpenGLWidget {
public:
    explicit GL(QWidget *parent) : QOpenGLWidget{parent} {
        setGeometry(-1,-1,1,1);
        connect(this, &QOpenGLWidget::frameSwapped, this, (void(QOpenGLWidget::*)())&QOpenGLWidget::update, Qt::QueuedConnection);
    }
};

EleVideo::EleVideo(QString path, QWidget *parent) : QWidget{parent} {
    player = new FFPlayer();
    connect(new GL(this), &QOpenGLWidget::frameSwapped, player, &FFPlayer::updFrame, Qt::QueuedConnection);
    connect(player, &FFPlayer::emUpd, this, [this](QImage img) {
        this->img = img;
        update();
    });
    connect(player, &FFPlayer::emError, this, [this](QString err) {
        QMessageBox::critical(this, "Video Error", err);
    });
    player->open(path.toUtf8());
}

//void EleVideo::mouseDoubleClickEvent(QMouseEvent *) {
//    qDebug()<<"img"<<img;
//    qDebug()<<"img"<<player->img;
//    qDebug()<<"viCurTime"<<player->viCurTime;
//    qDebug()<<"viSize"<<player->viSize;
//}
void EleVideo::showEvent(QShowEvent *) {
    if(player) player->play();
}
void EleVideo::hideEvent(QHideEvent *) {
    if(player) player->stop();
}

void EleVideo::paintEvent(QPaintEvent *e) {
    QWidget::paintEvent(e);
    QPainter painter(this);
    painter.drawImage(QRectF(0, 0, width(), height()), img);
}
