#include "wait_dialog.h"

CWaitDialog::CWaitDialog(QWidget *parent)
    : QDialog(parent),
      frame_center_(nullptr),
      movie_lable_(nullptr),
      load_movie_(nullptr),
      tip_lable_(nullptr),
      cancel_btn_(nullptr) {
    setWindowFlags(Qt::FramelessWindowHint |
                   Qt::Tool /* | Qt::WindowStaysOnTopHint*/);
    setAttribute(Qt::WA_TranslucentBackground, true);

    frame_center_ = new QFrame(this);
    movie_lable_ = new QLabel(frame_center_);
    tip_lable_ = new QLabel(frame_center_);
}

CWaitDialog::~CWaitDialog() {
    delete load_movie_;
    delete movie_lable_;
    delete tip_lable_;
    delete cancel_btn_;
    delete frame_center_;
}

void CWaitDialog::Init(const QString &gif, int size) {
    base_size = size;
    int extend_unit_size = 20;
    load_movie_ = new QMovie(gif);
    movie_lable_->setMovie(load_movie_);

    base_heigh_ = base_size + extend_unit_size * 3.2;
    int dia_width = base_size + margin_;
    int dia_heigh = base_heigh_ + margin_;
    setFixedSize(dia_width, dia_heigh);

    frame_center_->setGeometry(margin_, margin_, dia_width - margin_ * 2,
                               dia_heigh - margin_);
    movie_lable_->setGeometry(margin_, margin_, dia_width - margin_ * 4,
                              base_size);
    movie_lable_->setScaledContents(true);
    load_movie_->start();

    tip_lable_->setGeometry(margin_, base_size + margin_ * 2, dia_width,
                            extend_unit_size);
    tip_lable_->setAlignment(Qt::AlignCenter | Qt::AlignHCenter);
    tip_lable_->setObjectName("tips");
    tip_lable_->setText(u8"处理中......");
    tip_lable_->setStyleSheet(
        "QLabel#tips{font-family:\"Microsoft "
        "YaHei\";font-size: 15px;color: #333333;}");

    cancel_btn_ = new QPushButton(frame_center_);
    cancel_btn_->setObjectName("cancelBtn");
    cancel_btn_->setText(u8"取消");
    cancel_btn_->setGeometry(
        margin_, base_size + margin_ * 3 + extend_unit_size * 1.2,
        base_size - margin_ * 3, extend_unit_size + margin_);
    cancel_btn_->setStyleSheet(
        "QPushButton#cancelBtn{"
        "background-color: #edeef6;"
        "border-radius: 4px;"
        "font-family: \"Microsoft YaHei\";"
        "font-size: 15px;"
        "color: #333333;"
        "}"
        "QPushButton#cancelBtn::hover{"
        "background:#dcdeea"
        "}");

    connect(cancel_btn_, &QPushButton::clicked, this, &CWaitDialog::StopWait);

    // 实例阴影shadow
    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
    shadow->setOffset(0, 0);
    shadow->setColor(QColor(32, 101, 165));
    shadow->setBlurRadius(10);
    this->setGraphicsEffect(shadow);
}

void CWaitDialog::setTipsText(QString strTipsText) {
    tip_lable_->setText(strTipsText);
}

void CWaitDialog::setCanCancel(bool show) {
    cancel_btn_->setEnabled(show);
    if (!show) {
        cancel_btn_->setText(u8"取消已禁用");
    } else {
        cancel_btn_->setText(u8"取消");
    }
}

void CWaitDialog::moveToCenter(QWidget *pParent) {
    if (pParent != nullptr && pParent != nullptr) {
        int nParentWidth = pParent->width();
        int nParentHeigth = pParent->height();
        int nWidth = this->width();

        int nHeight = this->height();
        int nParentX = pParent->x();
        int nParentY = pParent->y();

        int x = (nParentX + (nParentWidth - nWidth) / 2);
        int y = (nParentY + (nParentHeigth - nHeight) / 2);
        this->move(x, y);
    }
}

void CWaitDialog::registerFunc(std::function<void()> call) { stopCall_ = call; }

void CWaitDialog::paintEvent(QPaintEvent *event) {
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setBrush(QBrush(Qt::white));
    painter.setPen(Qt::transparent);
    QRect rect = this->rect();
    rect.setLeft(margin_);
    rect.setTop(margin_);
    rect.setWidth(rect.width() - margin_);
    rect.setHeight(rect.height() - margin_);
    painter.drawRoundedRect(rect, margin_, margin_);
    QWidget::paintEvent(event);
}

void CWaitDialog::StopWait() {
    this->close();

    if (stopCall_) {
        stopCall_();
    }
}