#include "BaseWidgetMenu.h"
#include "../BaseWidget.h"
#include <QWheelEvent>
#include "../BackEnd/MainWidget.h"



Base_Widget_Menu::Base_Widget_Menu(QWidget* parent) : QWidget(parent){
    setObjectName("Base_Widget_Menu");
    setAttribute(Qt::WA_OpaquePaintEvent);
    main_layout = new QVBoxLayout(this);
    first_layout = new QHBoxLayout();
    main_layout->addLayout(first_layout, 1);
    second_layout = new QHBoxLayout();
    main_layout->addLayout(second_layout, 1);
    roll_object.enter_func = [](Abstract_Menu_Button* btn){
        btn->setChecked(true);
    };
    roll_object.leave_func = [](Abstract_Menu_Button* btn){
        btn->setChecked(false);
    };
    hide();
}

void Base_Widget_Menu::paintEvent(QPaintEvent *event) {
    QPainter painter(this);
    painter.setPen(Qt::NoPen);
    QPalette palette = QApplication::style()->standardPalette();
    const QBrush& base1_brush = palette.brush(QPalette::Window);
    painter.setBrush(base1_brush);
    painter.drawRect(rect());
    painter.end();
}

void Base_Widget_Menu::hideEvent(QHideEvent *event) {
    for(auto btn : sons){
        btn->hide();
    }
    emit hide_that();
    event->accept();
}

void Base_Widget_Menu::showEvent(QShowEvent *event) {
    if(!roll_object.widgets.empty()) roll_object.enter_func(roll_object.cur());
    emit show_that();
    event->accept();
}

void Base_Widget_Menu::add_menu_button_on_layout1(const std::vector<Abstract_Menu_Button *>& buttons, int stretch) {
    for(Abstract_Menu_Button* btn : buttons){
        first_layout->addWidget(btn, 1);
        roll_object.widgets.push_back(btn);
        btn->parent_menu = this;
    }
    if(stretch != 0) {
        first_layout->addStretch(stretch);
        first_layout->addSpacing(second_layout->spacing() * stretch);
    };
}

void Base_Widget_Menu::add_menu_button_on_layout2(const std::vector<Abstract_Menu_Button *>& buttons, int stretch) {
    for(Abstract_Menu_Button* btn : buttons){
        second_layout->addWidget(btn, 1);
        roll_object.widgets.push_back(btn);
        btn->parent_menu = this;
    }

    if(stretch != 0) {
        second_layout->addStretch(stretch);
        second_layout->addSpacing(second_layout->spacing() * stretch);
    };
}

void Base_Widget_Menu::add_menu_button_on_layout1(const std::vector<Abstract_Menu_Button *>& buttons) {
    add_menu_button_on_layout1(buttons, 5 - buttons.size());
}

void Base_Widget_Menu::add_menu_button_on_layout2(const std::vector<Abstract_Menu_Button *>& buttons) {
    add_menu_button_on_layout2(buttons, 5 - buttons.size());
}

Base_Widget_Menu* Base_Widget_Menu::create_sub_widget_menu(Abstract_Menu_Button *btn) {
    auto ret = new Base_Widget_Menu(parentWidget());
    ret->parent_btn = btn;
    ret->hide();
    ret->father = this;
    sons.push_back(ret);
    btn->own_menu = ret;
    return ret;
}





void Base_Widget_Menu::keyPressEvent(QKeyEvent *event) {
    Back_End* g = Back_End::instance();
    if (g->match(event, "右移")) {
        roll_object.roll_right();
    }
    else if (g->match(event, "左移")) {
        roll_object.roll_left();
    }
    else if (g->match(event, "增加")) {
        auto v = dynamic_cast<Value_Menu_Button*>(roll_object.cur());
        if(v){
            v->value_changed(v, true);
        }
    }
    else if (g->match(event, "减小")) {
        auto v = dynamic_cast<Value_Menu_Button*>(roll_object.cur());
        if(v){
            v->value_changed(v, false);
        }
    }
    else if (g->match(event, "确定")){
        confirm();
    }
    else if (g->match(event, "退出")){
        esc(this);
        return;
    }
    event->ignore();
}



void Base_Widget_Menu::confirm() {
    Base_Widget_Menu* m = roll_object.cur()->own_menu;
    if(m){
        m->resize(size());
        m->move(geometry().topLeft() + QPoint(0, -height()));
        m->raise();
        m->setFocus();
        m->show();
    } else {
        roll_object.cur()->emit clicked();
        if(roll_object.cur()->click_hide){
            root_node()->hide();
        }
    }
}


void Base_Widget_Menu::esc(Base_Widget_Menu* menu){
    if(menu->root()){
        Main_Widget::instance()->get_current_base_widget()->setFocus();
    } else {
        menu->father->setFocus();
    }
    menu->hide();
}


void Abstract_Menu_Button::hide_root() {
    Main_Widget::instance()->get_current_base_widget()->setFocus();
    parent_menu->root_node()->hide();
}
