#include "OpenGLWidgetAPI.h"

#include "PlayerGLWidget.h"

#include "spdlog/spdlog.h"

/* 创建窗口，返回QWidget指针 */
QWidget* createOpenGLWidget(QWidget* parent)
{
    PlayerGLWidget* widget = new PlayerGLWidget(parent);
    if(widget == nullptr)
    {
        return nullptr; // 创建失败
    }
    
    return widget;
}

/* 销毁窗口 */
void destroyOpenGLWidget(QWidget* widget)
{
    if(widget == nullptr)
    {
        SPDLOG_ERROR("destroyOpenGLWidget: widget is nullptr");
        return;
    }
    
    PlayerGLWidget* playerWidget = qobject_cast<PlayerGLWidget*>(widget);
    if(playerWidget == nullptr)
    {
        SPDLOG_WARN("destroyOpenGLWidget: widget is not a PlayerGLWidget");
        return;
    }
    delete playerWidget;
    playerWidget = nullptr;
    widget = nullptr;
}

/* 刷新一帧RGBA图片 */
void refreshRGBAImage(QWidget* widget, QImage& image)
{
    if(widget == nullptr)
    {
        SPDLOG_ERROR("refreshRGBAImage: widget is nullptr");
        return;
    }
    
    PlayerGLWidget* playerWidget = qobject_cast<PlayerGLWidget*>(widget);
    if(playerWidget == nullptr)
    {
        SPDLOG_WARN("refreshRGBAImage: widget is not a PlayerGLWidget");
        return;
    }
    
    playerWidget->showOneRGBAImage(image);
}

/* 刷新一帧YUV420图片 */
void refreshYUV420Image(QWidget* widget, Image_YUV420P* yuvData)
{
    if(widget == nullptr)
    {
        SPDLOG_ERROR("refreshYUV420Image: widget is nullptr");
        return;
    }
    
    PlayerGLWidget* playerWidget = qobject_cast<PlayerGLWidget*>(widget);
    if(playerWidget == nullptr)
    {
        SPDLOG_WARN("refreshYUV420Image: widget is not a PlayerGLWidget");
        return;
    }
    
    if(yuvData == nullptr)
    {
        SPDLOG_WARN("refreshYUV420Image: yuvData is nullptr");
        return;
    }
    
    playerWidget->showOneYUV420Image(*yuvData);
}




