#ifndef AVPLAYER2_MAINWINDOWA_H
#define AVPLAYER2_MAINWINDOWA_H

#pragma once

#include <QActionGroup>
#include <QElapsedTimer>
#include <QFileDialog>
#include <QMainWindow>
#include <QMessageBox>
#include <QMimeData>
#include <QMouseEvent>
#include <QMutex>
#include <QSettings>
#include <QSizePolicy>
#include <QThread>
#include <QTimer>

#include "app_settings.h"
#include "audio_effect_gl.h"
#include "network_url_dlg.h"
#include "play_control_window.h"
#include "playercontroller.h"
#include "playlist_window.h"
// #include "read_thread.h"
// #include "start_play_thread.h"
// #include "stopplay_waiting_thread.h"
// #include "subtitle_decode_thread.h"
// #include "video_decode_thread.h"
// #include "audio_decode_thread.h"
// #include "audio_play_thread.h"
// #include "video_play_thread.h"
// #include "video_state.h"

QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindowA;
}
QT_END_NAMESPACE

class OpenGLVideoWidget;

#define MaxRecentFiles 20 // maximum recent play files
#define MaxSkinStlyes 20  // maximum sytyles
#define MaxPlaylist 5     // maximum playlist numbers

class MainWindowA : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindowA(QWidget* parent = Q_NULLPTR);
    ~MainWindowA();

public:
    float volume_settings(bool set = true, float vol = 0);
    void play_mute(bool mute);
    void play_seek();
    void play_start_seek();

    void set_volume(int volume);
    void set_play_speed();
    void show_msg_dlg(const QString& message,
                      const QString& windowTitle = "Warning",
                      const QString& styleSheet = "");
    QString get_playingfile() const;
    void show_fullscreen(bool bFullscreen = true);

    PlayerController* m_playerController;
public slots:
    void frameReady(AVFrame*);
    void subtitle_ready(const QString&);
    void audio_data(const AudioData& data);
    void open_recentFile();
    void clear_recentfiles();
    void update_play_time();
    void play_failed(const QString& file);
    void playlist_file_saved(const QString& file);

signals:
    // 删除所有与底层线程相关的信号

private:
    void keyPressEvent(QKeyEvent* event) override;
    void resizeEvent(QResizeEvent* event) override;
    void moveEvent(QMoveEvent* event) override;
    bool eventFilter(QObject* obj, QEvent* event) override;
    void dropEvent(QDropEvent* event) override;
    void dragEnterEvent(QDragEnterEvent* event) override;

private slots:
    void on_actionOpen_triggered();
    void on_actionQuit_triggered();
    void on_actionHelp_triggered();
    void on_actionAbout_triggered();
    void on_actionStop_triggered();
    void on_actionFullscreen_triggered();
    void on_actionHide_Play_Ctronl_triggered();
    void on_actionAspect_Ratio_triggered();

    void on_actionLoop_Play_triggered();
    void on_actionMedia_Info_triggered();
    void on_actionKeyboard_Usage_triggered();
    void on_actionPlayList_triggered();
    void on_actionOpenNetworkUrl_triggered();
    void on_actionOriginalSize_triggered();

    void onCvActionToggled();
    void onShowMessage(const QString& message,
                       const QString& windowTitle,
                       const QString& styleSheet);
    void onRequestFullscreen(bool bFullscreen);
    void onRequestHideStatusBar(bool bHide);

private:
    void print_decodeContext(const AVCodecContext* pVideo, bool bVideo = true) const;
    void about_media_info();
    void center_window(QRect screen_rec);
    bool label_fullscreen();
    void hide_statusbar(bool bHide = true);
    void hide_menubar(bool bHide = true);
    void check_hide_menubar(const QPoint& pt);
    void check_hide_play_control();
    void auto_hide_play_control(bool bHide = true);
    void displayStatusMessage(const QString& message);
    void hide_play_control(bool bHide = true);
    void set_paly_control_wnd(bool set = true);
    void update_paly_control_volume();
    void update_paly_control_status();
    void update_paly_control_muted();
    void print_size() const;
    void print_screen() const;
    void create_style_menu();
    inline QScreen* screen() const;
    QRect screen_rect() const;
    qreal screen_scale() const;
    QSize display_video_size(AVCodecContext* pCtxVideo) const;
    inline OpenGLVideoWidget* get_video_widget() const { return m_video_widget.get(); }
    inline PlayControlWnd* get_play_control() const { return m_play_control_wnd.get(); }
    inline QObject* get_object(const QString& name) const { return findChild<QObject*>(name); }
    void create_play_control();
    void update_play_control();
    void set_volume_updown(bool bUp = true, float unit = 0.05);
    void create_recentfiles_menu();
    void set_current_file(const QString& fileName);
    void remove_recentfiles(const QString& fileName);
    void update_recentfile_actions();
    QString stripped_name(const QString& fullFileName) const;
    void save_settings();
    void read_settings();
    QString get_selected_style() const;
    void set_style_action(const QString& style);
    void clear_subtitle_str();
    void set_subtitle(const QString& str);
    void create_cv_action_group();
    void play_speed_adjust(bool up = true);
    void create_video_label();
    void update_video_label();
    void create_audio_effect();
    void show_audio_effect(bool bShow = true);
    void play_control_key(Qt::Key key);
    void update_menus();
    void enable_menus(bool enable = true);
    void enable_v_menus(bool enable = true);
    void enable_a_menus(bool enable = true);

    bool get_avisual_format(BarHelper::VisualFormat& fmt) const;
    void popup_audio_effect();
    void set_audio_effect_format(const BarHelper::VisualFormat& fmt);
    void create_playlist_wnd();
    void add_to_playlist(const QString& file);
    void show_playlist(bool show = true);
    void playlist_hiden();
    void hide_cursor(bool bHide = true);
    bool cursor_in_window(QWidget* pWnd);

    void create_savedPlaylists_menu();
    void remove_playlist_file(const QString& fileName);
    void update_savedPlaylists_actions();
    bool read_playlist(const QString& playlist_file, QStringList& files) const;

public slots:
    void clear_savedPlaylists();
    void open_playlist();

private:
    // 新增：菜单栏、菜单、Action、状态栏、中央控件等成员变量
    QMenuBar* m_menuBar = nullptr;
    QMenu* m_menuFile = nullptr;
    QMenu* m_menuRecentFiles = nullptr;
    QMenu* m_menuSavedPlaylist = nullptr;
    QMenu* m_menuCV = nullptr;
    QMenu* m_menuStyle = nullptr;
    QMenu* m_menuAudioVisualize = nullptr;
    QMenu* m_menuHelp = nullptr;

    QAction* m_actionOpen = nullptr;
    QAction* m_actionQuit = nullptr;
    QAction* m_actionHelp = nullptr;
    QAction* m_actionAbout = nullptr;
    QAction* m_actionAbout_QT = nullptr;
    QAction* m_actionStop = nullptr;
    QAction* m_actionFullscreen = nullptr;
    QAction* m_actionHide_Play_Ctronl = nullptr;
    QAction* m_actionAspect_Ratio = nullptr;
    QAction* m_actionLoop_Play = nullptr;
    QAction* m_actionMedia_Info = nullptr;
    QAction* m_actionKeyboard_Usage = nullptr;
    QAction* m_actionPlayList = nullptr;
    QAction* m_actionOpenNetworkUrl = nullptr;
    QAction* m_actionOriginalSize = nullptr;
    QAction* m_actionHardware_decode = nullptr;
    QAction* m_actionSampling = nullptr;
    QAction* m_actionFrequency = nullptr;
    QAction* m_actionLine = nullptr;
    QAction* m_actionBar = nullptr;
    QAction* m_actionPie = nullptr;
    QAction* m_actionRemoveCV = nullptr;
    QAction* m_actionRotate = nullptr;
    QAction* m_actionRepeat = nullptr;
    QAction* m_actionEqualizeHist = nullptr;
    QAction* m_actionThreshold = nullptr;
    QAction* m_actionThreshold_Adaptive = nullptr;
    QAction* m_actionReverse = nullptr;
    QAction* m_actionColorReduce = nullptr;
    QAction* m_actionGamma = nullptr;
    QAction* m_actionContrastBright = nullptr;
    QAction* m_actionCanny = nullptr;
    QAction* m_actionBlur = nullptr;
    QAction* m_actionSobel = nullptr;
    QAction* m_actionLaplacian = nullptr;
    QAction* m_actionScharr = nullptr;
    QAction* m_actionPrewitt = nullptr;
    QAction* m_actionTest_CV = nullptr;
    QAction* m_actionGrayscale = nullptr;
    QAction* m_actionMirro = nullptr;
    QAction* m_actionTransform = nullptr;

    QStatusBar* m_statusBar = nullptr;
    QWidget* m_centralWidget = nullptr;

    QString m_videoFile;
    QTimer m_timer; // mouse moving checking timer
    AppSettings m_settings;
    QString m_subtitle;

    std::unique_ptr<OpenGLVideoWidget> m_video_widget;

    std::unique_ptr<PlayControlWnd> m_play_control_wnd;
    std::unique_ptr<AudioEffectGL> m_audio_effect_wnd;
    std::unique_ptr<PlayListWnd> m_playListWnd;

    std::unique_ptr<QAction> m_recentFileActs[MaxRecentFiles];
    std::unique_ptr<QAction> m_recentClear;
    QActionGroup* m_CvActsGroup; // cv menus group
    QActionGroup* m_AVisualTypeActsGroup;
    QActionGroup* m_AVisualGrapicTypeActsGroup;
    std::unique_ptr<QAction> m_savedPlaylists[MaxPlaylist];
    std::unique_ptr<QAction> m_PlaylistsClear;
};

#endif // AVPLAYER2_MAINWINDOWA_H
