#include "ui_HelpWidget.h"

#include "models/patchlist.h"

#include "widgets/helpwidget.h"

#include <QDebug>
#include <QScrollBar>

HelpWidget::HelpWidget(PatchList* legacy_patches, QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::HelpWidget)
    , legacy_patches_(legacy_patches)
{
    ui->setupUi(this);
    currently_active_entry = ui->menuentry_1;

//    moveMenuHoverWidget(ui->menuentry_1);
    qDebug() << ui->hover_button_widget->geometry();
    connect(ui->content_scroll_area->verticalScrollBar(), &QScrollBar::valueChanged, this, &HelpWidget::handleContentScrolling);
}

HelpWidget::~HelpWidget()
{
    delete ui;
}

void HelpWidget::updateFontsSizes()
{

}

void HelpWidget::resizeEvent(QResizeEvent *event)
{

}

void HelpWidget::handleContentScrolling(int value)
{
    if (scrollAreaContentsAnimation && scrollAreaContentsAnimation->state() == QAbstractAnimation::Running)
        return;

//    qDebug() << "VALUE CHANGED: " << value << ui->content_scroll_area->verticalScrollBar()->maximum();
    QPushButton* new_active_entry = ui->menuentry_1;
    if (value >= ui->sub_entry_1->y() + ui->sub_entry_1->height() * 2 / 3)
        new_active_entry = ui->menuentry_2;
    if (value >= ui->sub_entry_2->y() + ui->sub_entry_2->height() * 2 / 3)
        new_active_entry = ui->menuentry_3;
    if (value >= ui->sub_entry_3->y() + ui->sub_entry_3->height() * 2 / 3)
        new_active_entry = ui->menuentry_4;
    if (value >= ui->sub_entry_4->y() + ui->sub_entry_4->height() * 2 / 3 || value == ui->content_scroll_area->verticalScrollBar()->maximum())
        new_active_entry = ui->menuentry_5;
    if (value == 0)
        new_active_entry = ui->menuentry_1;

    moveMenuHoverWidget(new_active_entry);
}

void HelpWidget::moveMenuHoverWidget(QPushButton *target)
{
    if (ui->hover_button_widget->size() == QSize(0, 0)) {
        ui->hover_button_widget->resize(target->size() + QSize(10, 0));
        ui->hover_button_widget->move(ui->checkpoints_list->pos() + target->pos() + QPoint(-5, 0));
    } else {
        if (menuHoverWidgetAnimation == nullptr)
            menuHoverWidgetAnimation = new QPropertyAnimation(ui->hover_button_widget, "geometry");
        else
            menuHoverWidgetAnimation->stop();

        menuHoverWidgetAnimation->setDuration(200);
        menuHoverWidgetAnimation->setStartValue(QRect(ui->hover_button_widget->pos(), ui->hover_button_widget->size()));
        menuHoverWidgetAnimation->setEndValue(QRect(ui->checkpoints_list->pos() + target->pos() + QPoint(-5, 0), target->size() + QSize(10, 0)));
        menuHoverWidgetAnimation->start();
    }

    ui->menuentry_1->raise();
    ui->menuentry_2->raise();
    ui->menuentry_3->raise();
    ui->menuentry_4->raise();
}

void HelpWidget::scrollContentToWidget(QWidget *widget)
{
    if (scrollAreaContentsAnimation == nullptr)
        scrollAreaContentsAnimation = new QPropertyAnimation(ui->content_scroll_area->verticalScrollBar(), "value");
    else
        scrollAreaContentsAnimation->stop();

    scrollAreaContentsAnimation->setDuration(200);
    scrollAreaContentsAnimation->setStartValue(ui->content_scroll_area->verticalScrollBar()->value());
    scrollAreaContentsAnimation->setEndValue(widget->y());
    scrollAreaContentsAnimation->start();
}


void HelpWidget::on_menuentry_1_clicked()
{
    if (currently_active_entry != ui->menuentry_1) {
        currently_active_entry = ui->menuentry_1;
        moveMenuHoverWidget(ui->menuentry_1);
    }
    scrollContentToWidget(ui->sub_entry_1);
}

void HelpWidget::on_menuentry_2_clicked()
{
    if (currently_active_entry != ui->menuentry_2) {
        currently_active_entry = ui->menuentry_2;
        moveMenuHoverWidget(ui->menuentry_2);
    }
    scrollContentToWidget(ui->sub_entry_2);
}

void HelpWidget::on_menuentry_3_clicked()
{
    if (currently_active_entry != ui->menuentry_3) {
        currently_active_entry = ui->menuentry_3;
        moveMenuHoverWidget(ui->menuentry_3);
    }
    scrollContentToWidget(ui->sub_entry_3);
}

void HelpWidget::on_menuentry_4_clicked()
{
    if (currently_active_entry != ui->menuentry_4) {
        currently_active_entry = ui->menuentry_4;
        moveMenuHoverWidget(ui->menuentry_4);
    }
    scrollContentToWidget(ui->sub_entry_4);
}

void HelpWidget::on_menuentry_5_clicked()
{
    if (currently_active_entry != ui->menuentry_5) {
        currently_active_entry = ui->menuentry_5;
        moveMenuHoverWidget(ui->menuentry_5);
    }
    scrollContentToWidget(ui->sub_entry_5);
}
