#include "waitingdlg.h"
#include <QBoxLayout>
#include <QTimerEvent>
#include <QPushButton>

WaitingDlg::WaitingDlg(QWidget *parent, QString text, QString sucText) : BaseDlg{parent}, sucText(sucText) {
    setAttribute(Qt::WA_DeleteOnClose);
    setModal(true);

    auto pal = palette();
    pal.setBrush(QPalette::Window, QColor(0xdddddd));
    setPalette(pal);

    auto vBox = new QVBoxLayout(this);
    vBox->setContentsMargins(6, 3, 6, 6);
    vBox->addStretch();

    btnAbort = new QPushButton("X");
    btnAbort->setStyleSheet(R"rrr(
QPushButton {border-radius: 4px; padding: 2px 6px; background: transparent;}
QPushButton:hover {background: rgba(0,0,0,0.2);}
QPushButton:pressed {background: rgba(0,0,0,0.3);}
)rrr");
    connect(btnAbort, &QPushButton::clicked, this, &QDialog::reject);
    vBox->addWidget(btnAbort, 0, Qt::AlignRight);

    mIndicator = new CustomProgressIndicator(this);
    mIndicator->setDisplayModel(1);
    mIndicator->setColor(QColor(0x0088dd));
    mIndicator->startAnimation();
    vBox->addWidget(mIndicator, 0, Qt::AlignCenter);

    vBox->addStretch();

    fdText = new QLabel(text);
    fdText->setAlignment(Qt::AlignCenter);
    auto font = fdText->font();
    font.setPixelSize(18);
    font.setBold(true);
    fdText->setFont(font);
    pal = fdText->palette();
    pal.setBrush(QPalette::WindowText, QColor(0x0088dd));
    fdText->setPalette(pal);
    vBox->addWidget(fdText);

    vBox->addStretch();
}

void WaitingDlg::timerEvent(QTimerEvent *event) {
    if(closeTimerId==event->timerId()) {
        killTimer(closeTimerId);
        closeTimerId = 0;
        close();
    } else BaseDlg::timerEvent(event);
}
void WaitingDlg::success() {
    fdText->setText(sucText.isEmpty() ? tr("Success") : sucText);
    mIndicator->setBackground(":/res/success.png");
    mIndicator->stopAnimation();
    closeTimerId = startTimer(800);
}
