#ifndef AVPLAYERWIDGET_H
#define AVPLAYERWIDGET_H

#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QSlider>
#include <QLineEdit>
#include <QSharedPointer>
#include <QFutureWatcher>
#include <QProgressBar>
#include <QMovie>

class AVPlayer;
class AVOpenGLWidget;
class VideoFrame;

class AVPlayerWidget : public QWidget
{
    Q_OBJECT
public:
    explicit AVPlayerWidget(QWidget *parent = nullptr);
    ~AVPlayerWidget();

    void play(const QString &url);
    void stop();
    void pause();
    void resume();
    
    // 兼容旧接口：基于房间ID控制播放
    void stopPlay();
    void setPlayRoomId(const QString &id);
    void startPlay();
    
    // 异步播放接口
    void playAsync(const QString &url);
    void startPlayAsync();
    
private Q_SLOTS:
    void ptsChangedSlot(unsigned int duration);
    void durationChangedSlot(unsigned int pts);
    void terminateSlot();
    
    // UI控制槽函数
    void onPlayButtonClicked();
    void onStopButtonClicked();
    void onPauseButtonClicked();
    void onTestPlayButtonClicked();
    void onVolumeChanged(int volume);
    
    // 视频帧处理
    void onFrameChanged(QSharedPointer<VideoFrame> frame);
    
    // 异步播放相关槽函数
    void onAsyncPlayFinished();
    void onAsyncPlayError(const QString &error);
    
signals:
    void playStateChanged(bool isPlaying);
    void playLoadingStarted();
    void playLoadingFinished();
    void playError(const QString &error);

private:
    void setupUI();
    void connectSignals();
    void showLoadingUI();
    void hideLoadingUI();
    
    AVPlayer *m_player;
    AVOpenGLWidget *m_openglWidget;
    
    // UI组件
    QVBoxLayout *m_mainLayout;
    QHBoxLayout *m_controlLayout;
    QPushButton *m_playButton;
    QPushButton *m_pauseButton;
    QPushButton *m_stopButton;
    QPushButton *m_testPlayButton;
    QLabel *m_timeLabel;
    QSlider *m_volumeSlider;
    QLineEdit *m_urlEdit;
    
    bool m_isPlaying;
    bool m_isPaused;
    bool m_isLoading;
    
    // 异步播放相关
    QFutureWatcher<bool> *m_playWatcher;
    
    // 加载状态UI
    QLabel *m_loadingLabel;
    QProgressBar *m_loadingProgress;
    QMovie *m_loadingMovie;
    
    QString m_pendingUrl;
};

#endif // AVPLAYERWIDGET_H
