#include "ServerSettingWidget.h"
#include <QPainter>
#include <QLabel>
#include <QLineEdit>
#include <QStyleOption>
#include "../component/ExpandInput.h"
#include "../component/ShowHelpInfo.h"
#include "../BackEnd/MainWidget.h"
#include "../BaseWidget.h"


Server_Setting_Widget::Server_Setting_Widget(QWidget *parent) : QWidget(parent) {


    setObjectName("服务端设置界面");
    hide();
    auto main_layout = new QVBoxLayout(this);
    main_layout->setAlignment(Qt::AlignTop);
    rig_name_label = create_label("Rig Name");
    user_name_label = create_label("User Name");
    password_label = create_label("Password");
    ctrl_port_label = create_label("Ctrl Port");
    civ_port_label = create_label("Civ Port");
    audio_port_label = create_label("Audio Port");

    rig_name_edit = new Expand_Input();
    user_name_edit = new Expand_Input();
    password_edit = new Expand_Input();
    ctrl_port_edit = new Expand_Input();
    civ_port_edit = new Expand_Input();
    audio_port_edit = new Expand_Input();

    main_layout->addLayout(create_hbox_layout({rig_name_label, rig_name_edit}), 1);
    main_layout->addLayout(create_hbox_layout({user_name_label, user_name_edit}), 1);
    main_layout->addLayout(create_hbox_layout({password_label, password_edit}), 1);
    main_layout->addLayout(create_hbox_layout({ctrl_port_label, ctrl_port_edit}), 1);
    main_layout->addLayout(create_hbox_layout({civ_port_label, civ_port_edit}), 1);
    main_layout->addLayout(create_hbox_layout({audio_port_label, audio_port_edit}), 1);
    show_help_info = new Show_Help_Info;
    show_help_info->font.setPixelSize(40);
    main_layout->addWidget(show_help_info, 3);
    int font_size = 40;
    start_button = create_click_button("Start", font_size, [&]() {
        show_help_info->setText("Start 帮助信息");
        show_help_info->update();
    }, &roll_object);
    default_button = create_click_button("Default", font_size, [&]() {
        show_help_info->setText("Default 帮助信息");
        show_help_info->update();
    }, &roll_object);
    clear_button = create_click_button("Clear", font_size, [&]() {
        show_help_info->setText("Clear 帮助信息");
        show_help_info->update();
    }, &roll_object);
    save_button = create_click_button("Save", font_size, [&]() {
        show_help_info->setText("Save 帮助信息");
        show_help_info->update();
    }, &roll_object);
    exit_button = create_click_button("Exit", font_size, [&]() {
        Main_Widget::instance()->hide_cur_hover_widget();
        Main_Widget::instance()->get_current_base_widget()->setFocus();
    }, &roll_object);
    roll_object.widgets = {start_button, default_button, clear_button, save_button, exit_button};
    roll_object.enter_func = [](QPushButton* btn){
        btn->setChecked(true);
    };
    roll_object.leave_func = [](QPushButton* btn){
        btn->setChecked(false);
    };
    main_layout->addLayout(roll_object.hbox_layout(8), 2);
}

void Server_Setting_Widget::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();
}

QLabel *Server_Setting_Widget::create_label(const QString& text) {
    auto ret = new QLabel(text);
    QFont font;
    font.setBold(true);
    font.setPixelSize(Global::instance()->adapt_height(50));
    ret->setFont(font);
    QPalette pe;
    pe.setColor(QPalette::WindowText, Qt::black);
    ret->setPalette(pe);
    return ret;
}

void Server_Setting_Widget::keyPressEvent(QKeyEvent *event) {
    if (event->type() == QEvent::KeyPress) {
        Back_End* g = Back_End::instance();
        auto *key_event = dynamic_cast<QKeyEvent *>(event);
        if(g->match(key_event, "左移")){
            roll_object.roll_left();
            event->accept();
            return;
        }
        if(g->match(key_event, "右移")){
            roll_object.roll_right();
            event->accept();
            return;
        }
        if(g->match(key_event, "确定")){
            roll_object.cur()->emit clicked();
            event->accept();
            return;
        }
    }
    event->ignore();
}

void Server_Setting_Widget::showEvent(QShowEvent *event) {
    roll_object.enter_func(roll_object.cur());
    event->accept();
}



