#include "wPictureItem.h"
#include "ui_wPictureItem.h"
#include "CAppInfo.h"
#include "awMsgBox.h"
#include <QFile>

wPictureItem::wPictureItem(QString sUrl, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::wPictureItem),
    m_sUrl(sUrl)
{
    ui->setupUi(this);

    setStyleSheet(g_appInfoPtr->m_sQssStr);

    qRegisterMetaType<CDownLoadFileInfo>("CDownLoadFileInfo");
    connect(g_httpBllPtr.get(), &CHttpBll::sgnDownLoadFile, this, &wPictureItem::onDownLoadFile);
   
    this->installEventFilter(this);
}

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

void wPictureItem::setUI(int nLeft, int nTop, int nWidth, int nHeight)
{
    setGeometry(nLeft, nTop, nWidth, nHeight);
    ui->label_picture->setGeometry(0, 0, nWidth, nHeight);
    ui->widget_operation->setGeometry(0, 0, nWidth, nHeight);
    ui->btn_delete->setGeometry(width() - g_appInfoPtr->m_fRate*16, 0, g_appInfoPtr->m_fRate*16, g_appInfoPtr->m_fRate*16);
    ui->btn_view->setGeometry((width() - g_appInfoPtr->m_fRate*16)/2, (height() - g_appInfoPtr->m_fRate*11)/2,
                              g_appInfoPtr->m_fRate*16, g_appInfoPtr->m_fRate*11);
    ui->widget_operation->setVisible(false);

    QString sLocalFile = g_appInfoPtr->m_FileCacheMap[m_sUrl];
    if(sLocalFile.isEmpty() || !QFile::exists(sLocalFile))
    {
        QString sFileName = m_sUrl.right(m_sUrl.length() - m_sUrl.lastIndexOf("/") - 1);
        sFileName = g_appInfoPtr->m_sCacheFileDir + sFileName;
        CHttpRequestPackage hrp;
        hrp.sUri = m_sUrl;
        hrp.sCommonStr = sFileName;
        hrp.sCommonStr1 = __FILE__;
        hrp.nRequestType = RequestType::rtDownLoadFile;
        hrp.nRetryCount = 3;
        g_httpBllPtr->downLoad(hrp);
    }
    else
    {
        ui->label_picture->setPixmap(QPixmap(sLocalFile).scaled(ui->label_picture->width(), ui->label_picture->height(), Qt::IgnoreAspectRatio));
    }
}

bool wPictureItem::eventFilter(QObject *obj, QEvent *ev)
{
    if(ev->type() == QEvent::Enter)
    {
        ui->widget_operation->setVisible(true);
    }
    else if(ev->type() == QEvent::Leave)
    {
        ui->widget_operation->setVisible(false);
    }

    return QWidget::eventFilter(obj, ev);
}

QString wPictureItem::getUrl()
{
    return m_sUrl;
}

void wPictureItem::onDownLoadFile(CDownLoadFileInfo downLoadFileInfo)
{
    QString sFileName = m_sUrl.right(m_sUrl.length() - m_sUrl.lastIndexOf("/") - 1);
    sFileName = g_appInfoPtr->m_sCacheFileDir + sFileName;
    if (downLoadFileInfo.sFileName == sFileName &&
           downLoadFileInfo.sModuleName == __FILE__)
    {
        if (downLoadFileInfo.nCode == 200)
        {
            g_appInfoPtr->m_FileCacheMap[m_sUrl] = sFileName;
            ui->label_picture->setPixmap(QPixmap(sFileName).scaled(ui->label_picture->width(), ui->label_picture->height(), Qt::IgnoreAspectRatio));
        }
        else
        {
            if(downLoadFileInfo.sMessage.isEmpty())
            {
                ShowMsg(QString::fromLocal8Bit("下载失败"), g_appInfoPtr->m_pAnsBgWidget, MSG_ICON_TYPE::mit_error);
            }
            else
            {
                ShowMsg(downLoadFileInfo.sMessage, g_appInfoPtr->m_pAnsBgWidget, MSG_ICON_TYPE::mit_error);
            }
        }
    }
}

void wPictureItem::on_btn_delete_clicked()
{
    emit delFile(m_sUrl);
}

void wPictureItem::on_btn_view_clicked()
{
    emit viewPhoto(m_sUrl);
}
