#include "menuentry.h"
#include <QApplication>
#include <QTimer>

MenuEntry *MenuEntry::active_label = nullptr;
MenuEntry *MenuEntry::hover_label = nullptr;

MenuEntry::MenuEntry(QWidget* parent, Qt::WindowFlags)
    : QLabel(parent) {

    connect(this, &MenuEntry::clicked, this, &MenuEntry::on_clicked);
}

MenuEntry::~MenuEntry() {}

MenuEntry *MenuEntry::getActiveLabel()
{
    return active_label;
}

void MenuEntry::setActiveLabel(MenuEntry *label)
{
    active_label = label;
}

MenuEntry *MenuEntry::getHoverLabel()
{
    return hover_label;
}

void MenuEntry::setHoverLabel(MenuEntry *label)
{
    hover_label = label;
}

void MenuEntry::mousePressEvent(QMouseEvent* event) {
    event->accept();
    if (active_label != this) {
        active_label = this;
        emit active_label_changed();
        emit clicked();
    }
}

void MenuEntry::mouseMoveEvent(QMouseEvent *)
{
    if (hover_label != this) {
        hover_label = this;
        emit hover_label_changed();
    }
}

void MenuEntry::on_clicked()
{
    setStyleSheet(active_stylesheet);
    QTimer::singleShot(200, [this](){
        setStyleSheet(default_stylesheet);
    });
}
