/* This file is part of the KDE project
   Copyright (C) 2013 Boudewijn Rempt <boud@valdyas.org>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the Lesser GNU General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This code is based on Thacher Ulrich PSD loading code released
   on public domain. See: http://tulrich.com/geekstuff/
*/

#include "ora.h"

#include <QImage>
#include <QScopedPointer>

#include <kzip.h>

#include <KoStore.h>

#include <kis_image.h>
#include <kis_doc2.h>
#include <kis_group_layer.h>
#include <kis_image.h>
#include <kis_open_raster_stack_load_visitor.h>
#include "ora_load_context.h"
#include <kis_paint_layer.h>

#include <kdebug.h>

OraHandler::OraHandler()
{
}

bool OraHandler::canRead() const
{
    if (canRead(device())) {
        setFormat("ora");
        return true;
    }
    return false;
}

bool OraHandler::read(QImage *image)
{
//    qDebug() << "orahandler::read" << kBacktrace();
//    if (QFile *f = qobject_cast<QFile*>(device())) {
//        qDebug() << "\t" << f->fileName();
//    }

    QScopedPointer<KoStore> store(KoStore::createStore(device(), KoStore::Read, "image/openraster", KoStore::Zip));
    if (!store || store->bad()) {
        return false;
    }
    store->disallowNameExpansion();

    OraLoadContext olc(store.data());
    KisOpenRasterStackLoadVisitor orslv(0, &olc);
    orslv.loadImage();
    KisImageWSP img = orslv.image();
    img->initialRefreshGraph();
    *image = img->projection()->convertToQImage(0);

    return true;
}

bool OraHandler::write(const QImage &)
{
    // TODO Stub!
    return false;
}

QByteArray OraHandler::name() const
{
    return "ora";
}

bool OraHandler::canRead(QIODevice *device)
{
    if (!device) {
        qWarning("KraHandler::canRead() called with no device");
        return false;
    }

    KZip zip(device);
    if (!zip.open(QIODevice::ReadOnly)) return false;

    const KArchiveEntry *entry = zip.directory()->entry("mimetype");
    if (!entry || !entry->isFile()) return false;

    const KZipFileEntry* fileZipEntry = static_cast<const KZipFileEntry*>(entry);

    return (qstrcmp(fileZipEntry->data().constData(), "image/openraster") == 0);
}


class OraPlugin : public QImageIOPlugin
{
public:
    QStringList keys() const;
    Capabilities capabilities(QIODevice *device, const QByteArray &format) const;
    QImageIOHandler *create(QIODevice *device, const QByteArray &format = QByteArray()) const;
};

QStringList OraPlugin::keys() const
{
    return QStringList() << "ora" << "ORA";
}

QImageIOPlugin::Capabilities OraPlugin::capabilities(QIODevice *device, const QByteArray &format) const
{
    Q_UNUSED(device);
    if (format == "ora" || format == "ORA")
        return Capabilities(CanRead);
    else
        return 0;

}

QImageIOHandler *OraPlugin::create(QIODevice *device, const QByteArray &format) const
{
    QImageIOHandler *handler = new OraHandler;
    handler->setDevice(device);
    handler->setFormat(format);
    return handler;
}

Q_EXPORT_STATIC_PLUGIN(OraPlugin)
Q_EXPORT_PLUGIN2(Ora, OraPlugin)
