#include "progress.h"

#include <QPainter>
#include <QPalette>
#include <QStyleOptionProgressBar>
#include <QStyleOptionViewItem>
#include <QProxyStyle>
#include <QMessageBox>

AnimeProgressBar::AnimeProgressBar() {
#if (defined(WIN32) || defined(MACOSX))
    auto *fusionStyle = new QProxyStyle {"fusion"};
    fusionStyle->setParent(&dummy_progress);
    dummy_progress.setStyle(fusionStyle);
#endif
}

void AnimeProgressBar::paint(QPainter *painter, const QStyleOptionViewItem &option, const QString &text, const int progress, const int episodes) const {
    QStyleOptionProgressBar styleOption;
    styleOption.initFrom(&dummy_progress);

    styleOption.maximum = episodes;
    styleOption.minimum = 0;
    styleOption.progress = progress;
    styleOption.text = text;
    styleOption.textVisible = true;

    styleOption.rect = option.rect;
    styleOption.state = option.state;

    const bool enabled = option.state.testFlag(QStyle::State_Enabled);
    styleOption.palette.setCurrentColorGroup(enabled ? QPalette::Active : QPalette::Disabled);

    painter->save();
    const QStyle *style = dummy_progress.style();
    style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, option.widget);
    style->drawControl(QStyle::CE_ProgressBar, &styleOption, painter, &dummy_progress);
    painter->restore();
}