#include "frmimgshow.h"

FrmImgShow::FrmImgShow(QWidget *parent) : QWidget(parent)
{
    initForm();
    connect(TCHttpService::getInstance(), &TCHttpService::signal_imageDownloaded,
            this, [this](const QString& requestId, const QPixmap& pixmap){
        if (requestId == m_requestId) {
            setLoadingState(false);
            setImage(pixmap);
        }
    });

    connect(TCHttpService::getInstance(), &TCHttpService::signal_downloadFailed,
            this, [this](const QString& requestId, const QString& error){
        if (requestId == m_requestId) {
            setLoadingState(true);
        }
    });
}

void FrmImgShow::loadImage(const QUrl &imageUrl)
{
    qDebug() << "loadImage";
    m_currentUrl = imageUrl;
    m_requestId = QUuid::createUuid().toString();
    setLoadingState(true);
    qDebug() << "downlaodImage";
    TCHttpService::getInstance()->downloadImage(m_requestId, imageUrl);
}

void FrmImgShow::setImage(const QPixmap &pixmap)
{
    qDebug() << "setImage";
    labImg->setPixmap(pixmap.scaled(200, 200, Qt::KeepAspectRatio, Qt::SmoothTransformation));
}

void FrmImgShow::setLoadingState(bool loading)
{

}


QSize FrmImgShow::sizeHint()
{
    return QSize(300, 300);
}

// bool FrmImgShow::eventFilter(QObject *watched, QEvent *event)
// {
    // if (watched == btnCopy) {
    //     if (event->type() == QEvent::Enter) {
    //         btnCopy->setIcon(QIcon(QPixmap(":/qrc/image/copy_blue.png")));
    //         return true;
    //     }
    //     else if (event->type() == QEvent::Leave) {
    //         btnCopy->setIcon(QIcon(QPixmap(":/qrc/image/copy_white.png")));
    //         return true;
    //     }
    //     else {
    //         return false;
    //     }
    // }

    // if (watched == btnDelete) {
    //     if (event->type() == QEvent::Enter) {
    //         btnDelete->setIcon(QIcon(QPixmap(":/qrc/image/delete_red.png")));
    //         return true;
    //     }
    //     else if (event->type() == QEvent::Leave) {
    //         btnDelete->setIcon(QIcon(QPixmap(":/qrc/image/delete_white.png")));
    //         return true;
    //     }
    //     else {
    //         return false;
    //     }
    // }

    // return QWidget::eventFilter(watched, event);
// }

void FrmImgShow::onFinished(QNetworkReply *reply)
{
    if (reply->error() == QNetworkReply::NoError) {
        QByteArray imageData = reply->readAll();
        QPixmap pix;
        pix.loadFromData(imageData);
        this->labImg->setPixmap(pix);
    }
}

void FrmImgShow::initForm()
{
    labImg = new QLabel(this);
    // btnCopy = new QPushButton(this);
    // btnCopy->setMaximumHeight(18);
    // btnDelete = new QPushButton(this);
    // btnDelete->setMaximumHeight(18);
    // horizenSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
    // hLayout = new QHBoxLayout();

    // ckbSelect = new QCheckBox(this);
    // ckbSelect->setMaximumHeight(18);
    // hLayout->addWidget(btnCopy);
    // hLayout->addWidget(btnDelete);
    // hLayout->setContentsMargins(0, 0, 0, 0);
    // hLayout->addSpacerItem(horizenSpacer);
    // hLayout->addWidget(ckbSelect);
    vLayout = new QVBoxLayout();
    vLayout->addWidget(labImg);
    // vLayout->addLayout(hLayout);
    vLayout->setContentsMargins(0, 0, 0, 0);
    // vLayout->addLayout(vLayout);

    this->setLayout(vLayout);

    // btnCopy->setIcon(QIcon(QPixmap(":/qrc/image/copy_white.png")));
    // btnDelete->setIcon(QIcon(QPixmap(":/qrc/image/delete_white.png")));
    // btnCopy->setFixedSize(32, 32);
    // btnCopy->setCursor(Qt::PointingHandCursor);

    setStyleSheet("background-color: #FF0000;border: 1px solid white");

    setFixedSize(140, 160);

    this->labImg->setFixedSize(140, 140);
}

void FrmImgShow::initWidget()
{
    // btnCopy->installEventFilter(this);
    // btnDelete->installEventFilter(this);
}
