#ifndef _TITLE_BAR_H_
#define _TITLE_BAR_H_

#include <QtWidgets>

class StateButton;
class Skin;

class TitleBar : public QWidget
{
    Q_OBJECT
public:
    enum Show_Style
    {
        SHOW_ICON = 0x01,
        SHOW_TITLE = 0x02,
        SHOW_MIN_BUTTON = 0x04,
        SHOW_MAX_BUTTON = 0x08,
        SHOW_CLOSE_BUTTON = 0x10,
        DRAW_BACKGROUND = 0x20, // 是否绘制标题栏的背景
        SHOW_SKIN = 0x40,       // 皮肤按钮
        SHOW_MENU = 0x80,       // 菜单按钮

        FORM_STYLE = SHOW_ICON | SHOW_TITLE | SHOW_MIN_BUTTON | SHOW_MAX_BUTTON | SHOW_CLOSE_BUTTON,
        FORM_BG_STYLE = FORM_STYLE | DRAW_BACKGROUND,
        DIALOG_STYLE = SHOW_TITLE | SHOW_CLOSE_BUTTON,
        DIALOG_BG_STYLE = DIALOG_STYLE | DRAW_BACKGROUND,
        DIALOG_ICON_STYLE = SHOW_ICON | DIALOG_STYLE,
        DIALOG_ICON_BG_STYLE = DIALOG_ICON_STYLE | DRAW_BACKGROUND,
        MAIN_FORM_STYLE = FORM_STYLE | SHOW_MENU,
        MAIN_FORM_BG_STYLE = MAIN_FORM_STYLE | DRAW_BACKGROUND,
        MAIN_FORM_SKIN_STYLE = MAIN_FORM_STYLE | SHOW_SKIN,
        MAIN_FORM_SKIN_BG_STYLE = MAIN_FORM_SKIN_STYLE | DRAW_BACKGROUND
    };
    Q_ENUMS(Show_Style)

public:
    TitleBar(QWidget *parent, Skin *skin = NULL);
    /*virtual*/ ~TitleBar();

    int get_show_style() const { return show_style_; };
    void set_show_style(int style);

    QString text() { return title_->text(); }
    void set_text(const QString &text) { title_->setText(text); }
    void set_text_style_sheet(const QString &qss) { title_->setStyleSheet(qss); }
    bool is_window_maximized() const { return is_maxed_; }

    // 将高度设置为背景图片的大小, 最好在show_style_设置为DRAW_BACKGROUND时使用
    void fix_height_to_background();

    void load_skin(Skin *skin = NULL);

    // 将字符串转为枚举值
    static int str2showstyle(const char *str);

public:
    QHBoxLayout *layout() const;
    QLabel *icon() const;
    QLabel *title() const;
    StateButton *skin_button() const;
    StateButton *menu_button() const;
    StateButton *min_button() const;
    StateButton *max_button() const;
    StateButton *close_button() const;

signals:
    // on skin button clicked
    void skin_clicked();
    // on menu button clicked
    void menu_clicked();

public slots:
    void show_small();
    void show_max_restore();

private:
    int init();
    void load_skin_internal();
    void widget_set_visible();

protected:
    void mousePressEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void mouseDoubleClickEvent(QMouseEvent *event);
    void paintEvent(QPaintEvent *);

protected:
    QHBoxLayout *hbox_layout_;
    QLabel *icon_;
    QLabel *title_;
    StateButton *skin_button_;
    StateButton *menu_button_;
    StateButton *min_button_;
    StateButton *max_button_;
    StateButton *close_button_;

    int show_style_;
    QRect normal_rect_;
    QPoint click_pos_;  //
    bool left_pressed_; // left mouse pressed
    bool is_maxed_;     // window Maximized

    Skin *skin_;
};

#endif
