#ifndef MYWIDGET_H
#define MYWIDGET_H

#include <qpushbutton.h>
#include <qvariant.h>
#include <pthread.h>

#include <aviplay.h>
#include <playerwidget.h>

class MyConfigDialog;
class VideoRenderer;
class QPixmap;
class QTimer;
class QLabel;
class QSlider;
class QProgressBar;

#define Q_MenuEvent QEvent::User
#define Q_PosEvent QEvent::Type(QEvent::User+1)
#define Q_DrawEvent QEvent::Type(QEvent::User+2)
#define Q_MyCloseEvent QEvent::Type(QEvent::User+4)
#define Q_MyResizeEvent QEvent::Type(QEvent::User+5)
#define Q_MyRefreshEvent QEvent::Type(QEvent::User+6)

class PixmapButton: public QPushButton
{
    QPixmap m_pm;
public:
    PixmapButton(const char* ficon, const char* name, QWidget* parent);
protected:
    void paintEvent(QPaintEvent *qp);
};

struct AviPlayerInitParams
{
    const char* filename;
    const char* subname;
    int width;
    int height;
    int quality;
    double seek;
    int fullscreen;
    int maximize;

    AviPlayerInitParams()
    {
        filename = subname = 0;
	width = height = 0;
	maximize = fullscreen = false;
        seek = 0.0;
    }
};


class MyWidget : public QWidget, public PlayerWidget
{
    static const int MAX_VBAR = 100;
    Q_OBJECT

public:
    static void drawfunc(const CImage*);
    static void stopfunc(int);
public:
    virtual void PW_showconf_func() {return showconf_func();}
    virtual void PW_stop_func() { return send_key_event(Qt::Key_X, 'X'); }
    virtual void PW_middle_button() {return middle_button();}
    virtual void PW_pause_func() { return send_key_event(Qt::Key_C, 'C'); }
    virtual void PW_play_func() { return send_key_event(Qt::Key_V, 'V'); }
    virtual void PW_quit_func() { return send_event(Q_MyCloseEvent); }
    virtual void PW_menu_slot() { return send_event(Q_MenuEvent); }
    virtual void PW_fullscreen() { return send_fullscreen(); }
    virtual void PW_key_func(int sym, int mod) { return key_func(sym, mod); }
    virtual void PW_resize(int w, int h) { return send_resize_event(w, h); }
    virtual void PW_refresh() { return send_event(Q_MyRefreshEvent); }
    virtual void PW_maximize_func() { return send_maximize(); }

public:
    MyWidget(int, int);
    ~MyWidget();
    int initPlayer(AviPlayerInitParams& avip);
    void endPlayer();
    void updatePosDisplay(int pos);
    void zoom(double scale);
    void zoomWH(int width, int height, int original_w, int original_h);
    bool isValid() { return (player) ? true : false; }
    bool isStopped() { return (player) ? player->IsStopped() : true; }
    bool isPlaying() { return (player) ? player->IsPlaying() : false; }
    bool isPaused() { return (player) ? player->IsPaused() : false; }
    //void setQuality(int q) { quality = q; }
    void setDisplayFramePos(bool b) { m_bDisplayFramePos = b; }
    void setAutorepeat(bool b) { m_bAutorepeat = b; }
    void send_key_event(int qtkey, int qtascii = 0, int qtstate = 0);
    void send_resize_event(int x, int y);
    void send_refresh();
    double GetPos() { return (player) ? player->GetPos() : 0.; }
    double GetLengthTime();
    IAviPlayer2* GetPlayer() { return player; }
    int is_tracking;
    int _in_reseek;

    public slots:
	void send_event(QEvent ev);
	void updatePos();
	void volume_func(int);
	void mute_func();
	void changed_func(int);
	void nextpage_func();
	void prevpage_func();
	void slider_func(){is_tracking=1;}
	void reseek_func();
	void moved_func(int);
	void rcreseek(double pos);
	void pause_func();
	void open_func();
	void opensub_func();
	void stop_func();
	void play_func();
	void showconf_func();
	void menu_func();
	void zoom_2x() { zoom(2.0); }
	void zoom_1x() { zoom(1.0); }
	void zoom_0_5x() { zoom(0.5); }
	void send_fullscreen();
	void fullscreen();
	void key_func(int sym, int mod);
	void send_maximize();
	void maximize();
	void maximize_mode(int);
	void about_func();
	void middle_button();
	void Resize(int, int);
	void Refresh();

	void remote_command();
	bool event(QEvent* e);

	void help();
	void about();
	void about_movie();
	void decoder_config();
	void dropEvent(QDropEvent*);
	void dragEnterEvent(QDragEnterEvent* event);
	void draw_slot();
	void starter_slot();
protected:
    void paintEvent(QPaintEvent *);
    void keyPressEvent(QKeyEvent *);
    void mouseDoubleClickEvent(QMouseEvent*);
    void mouseReleaseEvent( QMouseEvent * );
    void readConfig();
    void resizeEvent(QResizeEvent *);

    IAviPlayer2* player;
    pthread_mutex_t mutex;
    int vollast;
    int resize_count;
    QPopupMenu* popup;
    QPopupMenu* modes;
    QPopupMenu* about_popup;
    QSlider* scroller;
    QSlider* vbar;
    QProgressBar* pbar;
    PixmapButton* b_play;
    PixmapButton* b_open;
    PixmapButton* b_pause;
    PixmapButton* b_stop;
    PixmapButton* b_about;
    PixmapButton* b_mute;
    PixmapButton* b_opensub;
    QLabel* l_status;
    QLabel* l_time;
    MyConfigDialog* dlg;

    QTimer* _timer;
    QTimer* _start_timer;
    int last_slider;
    std::string _filename;
    std::string _subname;
    // pick better name here
    AviPlayerInitParams apip;

    bool m_bSettingScroller;
    bool m_bKeyProcessed;
    bool m_bDisplayFramePos;
    bool m_bAutorepeat;
};

struct DrawEvent
{
    CImage* data;
    pthread_mutex_t mutex;
    pthread_cond_t cond;
    DrawEvent():data(0)
    {
	pthread_mutex_init(&mutex, 0);
	pthread_cond_init(&cond, 0);
    }
    ~DrawEvent()
    {
	pthread_mutex_destroy(&mutex);
	pthread_cond_destroy(&cond);
    }
};

#endif // MYWIDGET_H
