#include "setupwaitprogressbar.h"
#include "colorprogressbar.h"

#include <QTimer>
#include <QVBoxLayout>
#include <QFile>
#include <QPainter>
#include <QLabel>
#include <QTextBrowser>
#include <QDebug>

#include <cmath>

SetupWaitProgressBar::SetupWaitProgressBar(QWidget *parent)
    :QWidget(parent)
{
    initialize();
    createConnections();
}

SetupWaitProgressBar::~SetupWaitProgressBar()
{
    if(mTimer->isActive())
        mTimer->stop();
}

void SetupWaitProgressBar::setValue(int value)
{
    mColorProgressBar->setValue(value);
}

void SetupWaitProgressBar::setBgImagePath(const QString &path)
{
    m_sBgImagePath = path;
    update();
}

void SetupWaitProgressBar::setBgImageOpacity(float opacity)
{
    m_fBgImageOpacity = opacity;
    update();
}


void SetupWaitProgressBar::show()
{
    m_bSetup = true;
    // this->showFullScreen();
    mColorProgressBar->setValue(0);
    mTimer->start();
    QWidget::show();
}

void SetupWaitProgressBar::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QFile imageFile(m_sBgImagePath);
    if(!imageFile.exists())
        return;

    QPainter painter(this);
    painter.setOpacity(m_fBgImageOpacity);
    //    painter.setPen(Qt::NoPen);
    //    painter.setBrush(Qt::gray);
    painter.drawPixmap(QRect(0, 0, this->width(), this->height()), QPixmap(m_sBgImagePath));
}

void SetupWaitProgressBar::initialize()
{
    m_fBgImageOpacity = 1.0;
    mTitle = tr("Starting, please wait");
    mDotIndex = 0;

    mMainVLayout = new QVBoxLayout;
    this->setLayout(mMainVLayout);

    mTitleLabel = new QLabel;
    mTitleLabel->setObjectName("SetupWaitProgressBarTitleLabel");
    mTitleLabel->setText(mTitle);
    mTitleLabel->setAlignment(Qt::AlignHCenter);

    QGradientStops colorGradientPoints;
    //colorGradientPoints << QGradientStop(0, Qt::green) << QGradientStop(0.5, Qt::yellow) << QGradientStop(1, Qt::red);
    colorGradientPoints << QGradientStop(0, "#ECF1F3") << QGradientStop(0.5, "#868C8E") << QGradientStop(1, "#1B9ACD");
    mColorProgressBar = new ColorProgressBar(ColorProgressBar::StyleGradual);
    mColorProgressBar->setTextPosition(ColorProgressBar::TextOnBar);
    mColorProgressBar->setDecimals(1);
    mColorProgressBar->setBarBackColor(QColor(250, 250, 250));
    mColorProgressBar->setTextForeColor(Qt::white);
    mColorProgressBar->setTextBackColor(QColor(24, 189, 155));
    mColorProgressBar->setBarForeDataColors(colorGradientPoints);
    mColorProgressBar->setValue(0);
    mColorProgressBar->setFixedHeight(60);

    mInfoTextBrowser = new QTextBrowser;
    mInfoTextBrowser->setFixedHeight(100);
    mInfoTextBrowser->setObjectName("SetupWaitProgressBarInfoTextBrowser");

    mMainVLayout->addStretch();
    mMainVLayout->addWidget(mTitleLabel);
    mMainVLayout->addWidget(mColorProgressBar);
    mMainVLayout->addSpacerItem(new QSpacerItem(10, 20, QSizePolicy::Policy::Fixed, QSizePolicy::Policy::Fixed));
    mMainVLayout->addWidget(mInfoTextBrowser);
    mMainVLayout->addStretch();

    mTimer = new QTimer(this);
    mTimer->setInterval(100);
    //mTimer->start();

    this->setFixedSize(700, 350);
    this->setWindowFlags(Qt::Tool | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    m_bSetup = false;

}

void SetupWaitProgressBar::createConnections()
{
    connect(mTimer, &QTimer::timeout, this, &SetupWaitProgressBar::slotUpdateValue);
}

void SetupWaitProgressBar::slotUpdateValue()
{
    if(m_bSetup)
    {
        int value = int(mColorProgressBar->value());
        if(value < 100)
        {
            value++;
            mColorProgressBar->setValue(value);
            QString titleIndex;
            for(int i = 0; i < mDotIndex; i++)
            {
                titleIndex = titleIndex + ".";
            }

            mTitleLabel->setText(mTitle + titleIndex);
            mInfoTextBrowser->append(mTitle + titleIndex);

            if(mDotIndex < 6)
            {
                mDotIndex++;
            }
            else
            {
                mDotIndex = 0;
            }
        }
        else
        {
            mTimer->stop();
            this->hide();
            m_bSetup = false;
            emit completed();
        }
    }
//    else
//    {
//        int value = int(mColorProgressBar->value());
//        if(value < 80) {
//            value++;
//            mColorProgressBar->setValue(value);
//        }
//    }

}
