#include "myqtengine.h"

#include <QDesktopServices>
#include <QDir>
#include <QDebug>
#include <QImageReader>
#include <QTimer>

MyQtEngine::MyQtEngine(QObject *parent) :
    QObject(parent)
{
    searchPictures();

    QTime time = QTime::currentTime();
    qsrand((uint)time.msec());
}

MyQtEngine::~MyQtEngine()
{
}


QString MyQtEngine::selectedPicPath()
{
    return m_selectedPicPath;
}

void MyQtEngine::setSelectedPicPath(QString s)
{
    m_selectedPicPath = s;
    emit selectedPicPathChanged();
}

void MyQtEngine::searchPicturesFromDevice(QString path)
{
    // Read all JPG JPEG image paths from the given path
    // Read images from the subfolders
    if (path.isNull()) {
        path = QDesktopServices::storageLocation(QDesktopServices::PicturesLocation);
    }
    QDir dir;
    dir.setFilter(QDir::Files | QDir::Dirs);
    dir.setPath(path);

    QFileInfoList list = dir.entryInfoList();
    for (int i = 0; i < list.size(); ++i) {
        QFileInfo fileInfo = list.at(i);
        if (fileInfo.isFile()) {
            QString s = fileInfo.filePath();
            if (s.indexOf(QString(".jpg"), 0, Qt::CaseInsensitive) > 0) {
                m_picturePaths.append(s);
            }
            else if (s.indexOf(QString(".jpeg"), 0, Qt::CaseInsensitive) > 0) {
                m_picturePaths.append(s);
            }
        }
        else {
            QString s = fileInfo.filePath().mid(fileInfo.filePath().length() - 1, 1);
            if (!s.contains(".") && !s.contains(".."))
                searchPicturesFromDevice(fileInfo.filePath());
        }
    }
}

void MyQtEngine::searchPictures()
{
#if defined Q_OS_SYMBIAN || defined Q_WS_SIMULATOR
    QString path = QDesktopServices::storageLocation(QDesktopServices::PicturesLocation);
    // Search from default picture folder c:/data/images
    searchPicturesFromDevice(path);
    // Search from E drive
    path = "E:/images";
    searchPicturesFromDevice(path);
#elif defined Q_WS_MAEMO_5
    // Camera pictures
    searchPicturesFromDevice("/home/user/MyDocs/DCIM");
    // Images folder
    QString path = QDesktopServices::storageLocation(QDesktopServices::PicturesLocation);
    searchPicturesFromDevice(path);
#elif defined Q_OS_WIN32
    QString path = QDesktopServices::storageLocation(QDesktopServices::PicturesLocation);
    searchPicturesFromDevice(path);
#endif
}

QStringList MyQtEngine::picturePaths()
{
    return m_picturePaths;
}

QVariant MyQtEngine::imageName(QVariant path)
{
    QString s = path.toString();
    QVariant ret(s.mid(s.lastIndexOf("/")+1));
    return ret;
}

QVariant MyQtEngine::pictureAt(QVariant index)
{
    QVariant ret (m_picturePaths[index.toInt()]);
    return ret;
}

QVariant MyQtEngine::randInt(QVariant low, QVariant high)
    {
    // Random number between low and high
    return qrand() % ((high.toInt() + 1) - low.toInt()) + low.toInt();
    }

void MyQtEngine::removeImage(QString path)
{
    for(int i=0;i<m_picturePaths.count();i++) {
        if (m_picturePaths[i]==path) {
            m_picturePaths.removeAt(i);
            break;
        }
    }
}

Q_INVOKABLE QVariant MyQtEngine::scaledPicture(QVariant picturePath, QVariant preferredWidth, QVariant preferredHeight)
{
    QVariant ret;
    m_imageReader.setFileName(picturePath.toString());
    QSize imageSize = m_imageReader.size();
    imageSize.scale(preferredWidth.toInt(),preferredHeight.toInt(), Qt::KeepAspectRatio);
    ret.setValue(imageSize);
    return ret;
}

