#ifndef Base_Widget_Menu_H
#define Base_Widget_Menu_H
#include "Renderive/export.h"
#include <QPushButton>
#include <QWidget>
#include <vector>
#include "Widget/base/Node.hpp"
#include "Widget/base/Roll_Object.h"
class QHBoxLayout;
class QVBoxLayout;
class Abstract_Text_Button;
class Abstract_Menu_Button;
class Base_Widget_Menu : public QWidget, public renderive::Node<Base_Widget_Menu> {
    Q_OBJECT
public:
    explicit Base_Widget_Menu(QWidget* parent);
    QVBoxLayout* main_layout;
    QHBoxLayout* first_layout;
    QHBoxLayout* second_layout;
    Abstract_Menu_Button* parent_btn{};
    renderive::Roll_Object<Abstract_Menu_Button> roll_object;
    Base_Widget_Menu* create_sub_widget_menu(Abstract_Menu_Button* btn);
    void esc(Base_Widget_Menu* menu);
    void confirm();
    void add_menu_button_on_layout1(const std::vector<Abstract_Menu_Button*>& buttons, int stretch);
    void add_menu_button_on_layout2(const std::vector<Abstract_Menu_Button*>& buttons, int stretch);
    void add_menu_button_on_layout1(const std::vector<Abstract_Menu_Button*>& buttons);
    void add_menu_button_on_layout2(const std::vector<Abstract_Menu_Button*>& buttons);
    signals :

    void show_that();
    void hide_that();
protected:
    void paintEvent(QPaintEvent* event) override;
    void keyPressEvent(QKeyEvent* event) override;
    void showEvent(QShowEvent* event) override;
    void hideEvent(QHideEvent* event) override;
};
class Abstract_Menu_Button : public QPushButton {
public:
    Abstract_Menu_Button() = default;
    Base_Widget_Menu* parent_menu{};
    Base_Widget_Menu* own_menu{};
    bool click_hide = true;
    void init(int font_size) {
        setFocusPolicy(Qt::NoFocus);
        setFlat(true);
        setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        setCheckable(true);
        setChecked(false);
        QFont font;
        font.setPixelSize(font_size);
        setFont(font);
    }
    void mousePressEvent(QMouseEvent* e) override {
        parent_menu->roll_object.set_cur(this);
        parent_menu->confirm();
    }
    void hide_root();
};
class Menu_Button : public Abstract_Menu_Button {
public:
    std::function<void(void)> click = nullptr;
    Menu_Button(const QString& text, int font_size, const std::function<void(void)>& click) {
        this->click = click;
        setText(text);
        init(font_size);
        connect(this, &Abstract_Menu_Button::clicked, [this]() {
            this->click();
        });
    }
};
class Double_Text_Menu_Button : public Abstract_Menu_Button {
public:
    std::function<void(bool)> switch_click = nullptr;
    QString text1, text2;
    bool is_text1 = false;
    Double_Text_Menu_Button(const QString& text1, const QString& text2, int font_size, const std::function<void(bool)>& click) {
        click_hide = false;
        this->text1 = text1;
        this->text2 = text2;
        switch_click = click;
        setText(text1);
        init(font_size);
        connect(this, &Abstract_Menu_Button::clicked, [this]() {
            is_text1 = !is_text1;
            switch_click(is_text1);
            if (is_text1) {
                setText(this->text2);
            }
            else {
                setText(this->text1);
            }
        });
    }
};
class Value_Menu_Button : public Abstract_Menu_Button {
public:
    double value{};
    QString text;
    std::function<void(Value_Menu_Button*, bool add)> value_changed;
    void setValue(double value) {
        this->value = value;
        setText(text.arg(this->value));
    }
    Value_Menu_Button(const QString& text, int font_size,
                    const std::function<void(Value_Menu_Button*, bool)>& valueChanged, double first_value) {
        this->text = text;
        init(font_size);
        value_changed = valueChanged;
        setValue(first_value);
    }
};
// class Value_Menu_Button : public Abstract_Menu_Button{
// public:
//     std::function<void(Value_Menu_Button*)> handle_up = nullptr;
//     std::function<void(Value_Menu_Button*)> handle_down = nullptr;
//     QString text;
//     double value{};
//     Value_Menu_Button(const QString &text, int font_size,
//                     const std::function<void(Value_Menu_Button*)> &handleUp,
//                     const std::function<void(Value_Menu_Button*)> &handleDown){
//         click_hide = false;
//         text = text;
//         handle_up = handleUp;
//         handle_down = handleDown;
//         setValue(value);
//         init(font_size);
//     }
//     void setValue(double value){
//         value = value;
//         setText(text.arg(value));
//     }
// };
#endif
