#include <QApplication>
#include <QMainWindow>
#include <QVBoxLayout>
#include <QWidget>
#include <QFileDialog>
#include <QMenuBar>
#include <QAction>
#include <QMessageBox>
#include "avplayerwidget.h"

class TestMainWindow : public QMainWindow
{
    Q_OBJECT

public:
    TestMainWindow(QWidget *parent = nullptr)
        : QMainWindow(parent)
    {
        setupUI();
        setupMenus();
    }

private slots:
    void openFile()
    {
        QString fileName = QFileDialog::getOpenFileName(this,
            tr("打开视频文件"), "",
            tr("视频文件 (*.mp4 *.avi *.mkv *.mov *.wmv *.flv *.webm);;所有文件 (*)"));
        
        if (!fileName.isEmpty()) {
            m_playerWidget->play(fileName);
        }
    }
    
    void showAbout()
    {
        QMessageBox::about(this, tr("关于"), 
            tr("AVPlayer 测试程序\n\n"
               "这是一个基于Qt和FFmpeg的视频播放器测试程序。\n"
               "支持多种视频格式，使用OpenGL进行硬件加速渲染。"));
    }

private:
    void setupUI()
    {
        m_playerWidget = new AVPlayerWidget(this);
        setCentralWidget(m_playerWidget);
        
        setWindowTitle(tr("AVPlayer 测试程序"));
        resize(800, 600);
        
        // 连接播放状态信号
        connect(m_playerWidget, &AVPlayerWidget::playStateChanged,
                this, [this](bool isPlaying) {
                    QString title = isPlaying ? 
                        tr("AVPlayer 测试程序 - 播放中") : 
                        tr("AVPlayer 测试程序");
                    setWindowTitle(title);
                });
    }
    
    void setupMenus()
    {
        // 文件菜单
        QMenu *fileMenu = menuBar()->addMenu(tr("文件(&F)"));
        
        QAction *openAction = new QAction(tr("打开文件(&O)"), this);
        openAction->setShortcut(QKeySequence::Open);
        connect(openAction, &QAction::triggered, this, &TestMainWindow::openFile);
        fileMenu->addAction(openAction);
        
        fileMenu->addSeparator();
        
        QAction *exitAction = new QAction(tr("退出(&X)"), this);
        exitAction->setShortcut(QKeySequence::Quit);
        connect(exitAction, &QAction::triggered, this, &QWidget::close);
        fileMenu->addAction(exitAction);
        
        // 帮助菜单
        QMenu *helpMenu = menuBar()->addMenu(tr("帮助(&H)"));
        
        QAction *aboutAction = new QAction(tr("关于(&A)"), this);
        connect(aboutAction, &QAction::triggered, this, &TestMainWindow::showAbout);
        helpMenu->addAction(aboutAction);
    }
    
    AVPlayerWidget *m_playerWidget;
};

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    
    app.setApplicationName("AVPlayer Test");
    app.setApplicationVersion("1.0");
    app.setOrganizationName("Test Organization");
    
    TestMainWindow window;
    window.show();
    
    return app.exec();
}

#include "test_avplayer.moc"