#include "widget.h"
#include "./ui_widget.h"

#include <QFileDialog>
#include "spdlog/spdlog.h"




Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    /* 加载动态库 */
    loadVideoPlayerLibrary();
    /* 创建播放器，并设置窗口 */
    m_player = createPlayer();
    if(m_player == nullptr) {
        SPDLOG_ERROR("创建播放器失败");
        return;
    }
    m_player->setWidgetParent(ui->widget_display, true);
    m_playerWidget = m_player->getPlayWidget();
    if(m_playerWidget == nullptr) {
        SPDLOG_ERROR("获取播放器窗口失败");
        return;
    }
}

Widget::~Widget()
{
    if(m_player != nullptr) {
        /* 销毁播放器 */
        destroyPlayer(m_player);
        m_player = nullptr;
    }
    delete ui;
}

void Widget::on_pBtn_openFile_clicked()
{
    // 打开文件对话框
    QString fileName = QFileDialog::getOpenFileName(this, tr("打开视频文件"), "", tr("视频文件 (*.mp4 *.avi *.mkv *.flv *.mov)"));
    if (fileName.isEmpty()) {
        SPDLOG_WARN("未选择视频文件");
        return;
    }
    m_player->openPlayVedio(fileName);
    SPDLOG_INFO("打开视频文件: {}", fileName.toStdString());
}

void Widget::on_pBtn_play_clicked()
{
    m_player->play();
}

void Widget::on_pBtn_playNetwork_clicked()
{
    QString url = ui->lineEdit->text();
    if (url.isEmpty()) {
        SPDLOG_WARN("未输入网络视频地址");
        return;
    }
    if(!m_player->openPlayVedio(url))
    {
        SPDLOG_ERROR("打开网络视频失败: {}", url.toStdString());
        return;
    }
    m_player->play();
    SPDLOG_INFO("播放网络视频: {}", url.toStdString());
}

void Widget::resizeEvent(QResizeEvent *event) 
{
    if (m_playerWidget) {
        m_playerWidget->setGeometry(0, 0, ui->widget_display->width(), ui->widget_display->height());
    }
    QWidget::resizeEvent(event);
}





