#include "frmupload.h"
#include "ui_frmupload.h"
#include <QtDebug>
#include <QImageReader>

FrmUpload::FrmUpload(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::FrmUpload)
{
    ui->setupUi(this);
    initFrom();
    initWidget();
}

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

bool FrmUpload::eventFilter(QObject *watched, QEvent *event)
{
    static bool mousePressed = false;
    if (watched == ui->frmUpload) {
        QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
        if (mouseEvent->type() == QMouseEvent::MouseButtonPress && mouseEvent->button() == Qt::LeftButton) {
            mousePressed = true;
            return  true;
        }
        else if (mouseEvent->type() == QMouseEvent::MouseButtonRelease && mouseEvent->button() == Qt::LeftButton && mousePressed == true) {
            QStringList fileList = QFileDialog::getOpenFileNames(this, "选择图片", "C:\\", "Files (*);;"
                                                                                       "PNG (*.png);;"
                                                                                       "JPG (*.jpg);;"
                                                                                       "JPEG (*.jpeg);;"
                                                                                       "SVG (*.svg)");
            uploadFiles(fileList);
            mousePressed = false;
            return true;
        }
        else {
            return false;
        }
    }

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

void FrmUpload::dragEnterEvent(QDragEnterEvent *event)
{
    if (event->mimeData()->hasUrls()) {
        event->acceptProposedAction();
    }
    else {
        event->ignore();
    }
}

void FrmUpload::dropEvent(QDropEvent *event)
{
    QStringList uploadList;
    const QMimeData* mimeData = event->mimeData();
    if (!mimeData->hasUrls()) {
        return;
    }
    QList<QUrl> urlList = mimeData->urls();


#if DEBUG
    foreach (QUrl url, urlList) {
        qDebug() << url.toLocalFile();
    }
#endif

    foreach (QUrl url, urlList) {
        QFileInfo fileInfo(url.toLocalFile());
        if (fileInfo.isDir()) {
            continue;
        }
        else {
//            QImageReader reader(url.toLocalFile());
//            if (!reader.imageFormat()) {
//                uploadList << url.toLocalFile();
//            TCHttpService::getInstance()->apiMd5(fileInfo.absoluteFilePath());
            }
        }

}

void FrmUpload::initFrom()
{
    ui->frmUpload->setMinimumSize(QSize(400, 200));
}

void FrmUpload::initWidget()
{
    ui->frmUpload->installEventFilter(this);
    this->setAcceptDrops(true);
//    ui->frmUpload->setAcceptDrops(true);
}

void FrmUpload::uploadFiles(QStringList fileList)
{

}
