#ifndef KONAIMAGE_H
#define KONAIMAGE_H

#include <QObject>
#include <QStringList>
#include <QHttp>
#include <QFile>
#include <QCryptographicHash>

#include "konaclient.h"


#define KONAIMAGE_PREVIEWPATH kc->getCacheDir() + SLASH + "preview_" + QString::number(id) + ".jpg"
#define KONAIMAGE_LOCALPATH kc->getArchiveDir() + SLASH + QString::number(id) + fileUrl.right(4)
#define KONAIMAGE_JPEGPATH kc->getCacheDir() + SLASH + "jpeg_" + QString::number(id) + ".jpg"
#define KONAIMAGE_SAMPLEPATH kc->getCacheDir() + SLASH + "sample_" + QString::number(id) + sampleUrl.right(4)

class KonaImage : public QObject
{
    Q_OBJECT
public:
    KonaImage(KonaClient *kc, int Id, QStringList Tags, int CreatedAt, int CreatorId, QString Author, int Change, QString Source, int Score, QString Md5, int FileSize, QString FileUrl, bool IsShownInIndex, QString PreviewUrl, int PreviewWidth, int PreviewHeight, int ActualPreviewWidth, int ActualPreviewHeight, QString SampleUrl, int SampleWidth, int SampleHeight, int SampleFileSize, QString JpegUrl, int JpegWidth, int JpegHeight, int JpegFileSize, QString Rating, bool HasChildren, int ParentID, QString Status, int Width, int Height, bool IsHeld, QString FramesPendingString, QString FramesString);
    
    int getId() {return id;}
    int getWidth() {return width;}
    int getHeight() {return height;}



    QString getLocalPreviewPath() {return KONAIMAGE_PREVIEWPATH;}
    QString getLocalImagePath() {return KONAIMAGE_LOCALPATH;}
    QString getLocalJpegPath() {return KONAIMAGE_JPEGPATH;}
    QString getLocalSamplePath() {return KONAIMAGE_SAMPLEPATH;}

private:
    KonaClient *kc;
    int id;
    QStringList tags;
    int createdAt;
    int creatorId;
    QString author;
    int change;
    QString source;
    int score;
    QString md5;
    int fileSize;
    QString fileUrl;
    bool isShownInIndex;
    QString previewUrl;
    int previewWidth;
    int previewHeight;
    int actualPreviewWidth;
    int actualPreviewHeight;
    QString sampleUrl;
    int sampleWidth;
    int sampleHeight;
    int sampleFileSize;
    QString jpegUrl;
    int jpegWidth;
    int jpegHeight;
    int jpegFileSize;
    QString rating;
    bool hasChildren;
    int parentID;
    QString status;
    int width;
    int height;
    bool isHeld;
    QString framesPendingString;
    QString framesString;
    // Eventuell fehlen noch children

    // Preview image
    QHttp *previewHttp;
    QFile *previewFile;
    int previewStatusCode;

    // "real" image
    QHttp *imageHttp;
    QFile *imageFile;
    int imageStatusCode;

    // jpeg image
    QHttp *jpegHttp;
    QFile *jpegFile;
    int jpegStatusCode;

    // sample image
    QHttp *sampleHttp;
    QFile *sampleFile;
    int sampleStatusCode;

signals:
    void previewDownloaded(KonaImage *ki, QString localPath);
    void imageDownloaded(KonaImage *ki, QString localPath);
    void jpegDownloaded(KonaImage *ki, QString localPath);
    void sampleDownloaded(KonaImage *ki, QString localPath);

public slots:
    void previewRequestFinished();
    void previewResponseHeaderReceived(QHttpResponseHeader resp);

    void imageRequestFinished();
    void imageResponseHeaderReceived(QHttpResponseHeader resp);

    void jpegRequestFinished();
    void jpegResponseHeaderReceived(QHttpResponseHeader resp);

    void sampleRequestFinished();
    void sampleResponseHeaderReceived(QHttpResponseHeader resp);

    void DownloadPreview(bool overwrite = false);
    void DownloadImage(bool overwrite = false);
    void DownloadJpeg(bool overwrite = false);
    void DownloadSample(bool overwrite = false);
};

#endif // KONAIMAGE_H
