#include "preferences.h"
#include <QMenu>
#include <QBoxLayout>
#include <backend.hpp>
#include <options.hpp>
using namespace Looper::Options;
PrefsWindow::PrefsWindow() {
    auto *root_layout = new QBoxLayout(QBoxLayout::TopToBottom, this);
    this->setLayout(root_layout);
    restart_warning = new QLabel("A restart is needed to apply some changes.", this);
    restart_warning->hide();
    root_layout->addWidget(restart_warning);
    frontend_btn = new QPushButton(this);
    frontend_menu = new QMenu();
    for (auto &kv : UIBackend::backends) {
        UIBackend *backend = kv.second;
        const char *name = strdup(backend->get_name().c_str());
        QAction *action = new QAction(name);
        action->connect(action, &QAction::triggered, [=,this]() {
            this->new_frontend = backend->get_id();
            frontend_btn->setText(strdup(this->new_frontend.c_str()));
            this->set_options_changed(true);
        });
        frontend_menu->addAction(action);
        frontend_options.push_back(action);
    }
    frontend_btn->setMenu(frontend_menu);
    root_layout->addWidget(frontend_btn);
    QFrame *frame = new QFrame(this);
    frame->setWindowTitle("Labels and Icons");
    auto *label_settings_group = new QBoxLayout(QBoxLayout::TopToBottom, this);
    frame->setLayout(label_settings_group);
    labels_only = new QRadioButton("Labels Only", frame);
    labels_only->connect(labels_only, &QRadioButton::pressed, [=,this]() {
        this->new_label_setting = "labels";
        this->set_options_changed(true);
    });
    icons_only = new QRadioButton("Icons Only", frame);
    icons_only->connect(icons_only, &QRadioButton::pressed, [=,this]() {
        this->new_label_setting = "icons";
        this->set_options_changed(true);
    });
    both_labels_icons = new QRadioButton("Both", frame);
    both_labels_icons->connect(both_labels_icons, &QRadioButton::pressed, [=,this]() {
        this->new_label_setting = "both";
        this->set_options_changed(true);
    });
    label_settings_group->addWidget(labels_only);
    label_settings_group->addWidget(icons_only);
    label_settings_group->addWidget(both_labels_icons);
    root_layout->addWidget(frame);
    QWidget *btn_view = new QWidget(this);
    QBoxLayout *btn_box = new QBoxLayout(QBoxLayout::LeftToRight, this);
    revert_btn = new QPushButton("Revert", btn_view);
    QObject::connect(revert_btn, &QPushButton::pressed, this, &PrefsWindow::revert);
    apply_btn = new QPushButton("Apply", btn_view);
    QObject::connect(apply_btn, &QPushButton::pressed, this, &PrefsWindow::apply);
    btn_view->setLayout(btn_box);
    btn_box->addWidget(revert_btn);
    btn_box->addWidget(apply_btn);
    root_layout->addWidget(btn_view);
    revert();
    setWindowTitle("Looper Preferences");
}
void PrefsWindow::set_options_changed(bool changed) {
    this->revert_btn->setEnabled(changed);
    this->apply_btn->setEnabled(changed);
}
void PrefsWindow::update_label_setting() {
    bool labels_enabled = true;
    bool icons_enabled = true;
    if (new_label_setting == "icons") {
        labels_enabled = false;
    } else if (new_label_setting == "labels") {
        icons_enabled = false;
    }
    emit(settings_changed(labels_enabled, icons_enabled));
}
void PrefsWindow::revert() {
    set_options_changed(false);
    new_label_setting = get_option<std::string>("ui.label_setting", "icons");
    new_frontend = get_option<std::string>("ui.frontend", "qt");
    if (new_frontend != "qt") restart_warning->show();
    else restart_warning->hide();
    frontend_btn->setText(new_frontend.c_str());
    update_label_setting();
}
void PrefsWindow::apply() {
    set_options_changed(false);
    set_option<std::string>("ui.label_setting", new_label_setting);
    set_option<std::string>("ui.frontend", new_frontend);
    if (new_frontend != "qt") restart_warning->show();
    else restart_warning->hide();
    frontend_btn->setText(new_frontend.c_str());
    update_label_setting();
}