/*
    SPDX-FileCopyrightText: 2017 Nicolas Carion
    SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/

#include "splash.hpp"
#include <KLocalizedString>
#include <QDebug>
#include <QPainter>
#include <QStyle>

Splash::Splash()
    : QSplashScreen()
    , m_progress(0)
{
    qreal scalingFactor = devicePixelRatioF();
    m_pixmap = QPixmap(":/pics/splash-background.png");
    m_pixmap = m_pixmap.scaled(m_pixmap.width() * scalingFactor, m_pixmap.height() * scalingFactor, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
    m_pixmap.setDevicePixelRatio(scalingFactor);

    // Set style for progressbar...
    m_pbStyle.initFrom(this);
    m_pbStyle.state = QStyle::State_Enabled;
    m_pbStyle.textVisible = false;
    m_pbStyle.minimum = 0;
    m_pbStyle.maximum = 100;
    m_pbStyle.progress = 0;
    m_pbStyle.invertedAppearance = false;
    m_pbStyle.rect = QRect(4, m_pixmap.height() - 24, m_pixmap.width() / 2, 20); // Where is it.

    // Add KDE branding to pixmap
    QPainter *paint = new QPainter(&m_pixmap);
    paint->setPen(Qt::white);
    QPixmap kde(":/pics/kde-logo.png");
    kde = kde.scaled(kde.width() * scalingFactor, kde.height() * scalingFactor, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
    kde.setDevicePixelRatio(scalingFactor);
    const int logoSize = 32;
    QPoint pos(12, 12);
    paint->drawPixmap(pos.x(), pos.y(), logoSize, logoSize, kde);
    paint->drawText(pos.x() + logoSize, pos.y() + (logoSize / 2) + paint->fontMetrics().strikeOutPos(), i18n("Made by KDE"));
    setPixmap(m_pixmap);
}

void Splash::increaseProgressMessage()
{
    m_progress++;
    repaint();
}

void Splash::showProgressMessage(const QString &message, int max)
{
    if (max > -1) {
        m_pbStyle.maximum = max;
        m_progress = 0;
    }
    if (!message.isEmpty()) {
        showMessage(message, Qt::AlignRight | Qt::AlignBottom, Qt::white);
    }
    repaint();
}

void Splash::drawContents(QPainter *painter)
{
    QSplashScreen::drawContents(painter);

    if (m_progress > 0) {
        // Set style for progressbar and draw it
        m_pbStyle.progress = m_progress;
        // Draw it...
        style()->drawControl(QStyle::CE_ProgressBar, &m_pbStyle, painter, this);
    }
}
