#include "FormAuthentication.h"
#include "qdebug.h"
#include "ui_FormAuthentication.h"

#include "DataTypeDef.h"

FormAuthentication::FormAuthentication(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::FormAuthentication)
    , m_cur_time(AUTHENTICATION_TIMEOUT)
{
    ui->setupUi(this);

    m_timer = new QTimer();
    m_timer->setInterval(1000);

    ui->count_time->setText(QString("%1S").arg(m_cur_time));

    initWidgets();
    initConnect();
}

FormAuthentication::~FormAuthentication()
{
    delete ui;
}

void FormAuthentication::setBackground(QLabel *label, const QString &imageUrl)
{
    label->setPixmap(QPixmap(imageUrl));
}

void FormAuthentication::initWidgets()
{
    // this->setAttribute(Qt::WA_TranslucentBackground);

    // // 设置QLabel不接收鼠标事件
    // ui->countdown_time->setAttribute(Qt::WA_TransparentForMouseEvents);
    // ui->cancel->setAttribute(Qt::WA_TransparentForMouseEvents);

    // posBackgroundImage = new QLabel();
    // authBackgroundImage = new QLabel();
    // cancelBackgroundImage = new QToolButton();
    // cancelBackgroundImage->setObjectName("cancel_auth_btn");
    // tipsBackGroundImage = new QLabel();

    // // 将背景 Label 添加到布局，并占据全部单元格
    // ui->pos_layout->addWidget(posBackgroundImage, 0, 0, ui->pos_layout->rowCount(), ui->pos_layout->columnCount());
    // ui->auth_layout->addWidget(authBackgroundImage, 0, 0, ui->auth_layout->rowCount(), ui->auth_layout->columnCount());
    // ui->cancel_layout->addWidget(cancelBackgroundImage, 0, 0, ui->cancel_layout->rowCount(), ui->cancel_layout->columnCount());
    // ui->tips_layout->addWidget(tipsBackGroundImage, 0, 0, ui->tips_layout->rowCount(), ui->tips_layout->columnCount());

    // // 叠加显示背景图，将背景图显示在底层
    // setBackground(posBackgroundImage, ":/images/qa/pos_background.png");
    // posBackgroundImage->lower();
    // posBackgroundImage->setScaledContents(true);
    // setBackground(authBackgroundImage, ":/images/qa/auth_background.png");
    // authBackgroundImage->lower();
    // authBackgroundImage->setScaledContents(true);
    // // setBackground(cancelBackgroundImage, ":/images/qa/pos_background.png");
    // cancelBackgroundImage->lower();
    // cancelBackgroundImage->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    // // cancelBackgroundImage->setScaledContents(true);
    // setBackground(tipsBackGroundImage, ":/images/qa/tips_background.png");
    // tipsBackGroundImage->lower();
    // tipsBackGroundImage->setScaledContents(true);
}

void FormAuthentication::initConnect()
{
    connect(m_timer, &QTimer::timeout, this, &FormAuthentication::authTimeout);
}

int FormAuthentication::getNFCCardId(int timeout, QByteArray &cardId)
{
    qDebug() << timeout;
    qDebug() << cardId;
    return 0;
}

void FormAuthentication::authTimeout()
{
    if (m_cur_time > 1) {
        m_cur_time--;
    } else {
        emit signalCancelAuth();
        m_timer->stop();
        m_cur_time = AUTHENTICATION_TIMEOUT;
    }
    ui->count_time->setText(QString("%1S").arg(m_cur_time));
}

void FormAuthentication::showEvent(QShowEvent *event)
{
    QWidget::showEvent(event);
    m_timer->start();
}
