/***************************************************************************
 *   Copyright (C) 2004-2009 by Pere Constans
 *   constans@molspaces.com
 *   cb2Bib version 1.3.2. Licensed under the GNU GPL version 3.
 *   See the LICENSE file that comes with this distribution.
 ***************************************************************************/
#include "c2bBibSearcherCache.h"

#include "c2bSettings.h"
#include "c2bUtils.h"

#include <document.h>

#include <QDir>


c2bBibSearcherCache::c2bBibSearcherCache(QObject* parento) : QObject(parento)
{
    cache_size = fileContents.size();
    settings = c2bSettingsP;
    p_simplify_source = false;
}

c2bBibSearcherCache::~c2bBibSearcherCache()
{
    save();
}


void c2bBibSearcherCache::load(const QString& filename, bool simplify_source)
{
    p_simplify_source = simplify_source;
    p_cache_dir = settings->fileName("cb2Bib/CacheDirectory");
    if (filename == load_filename)
        return;
    load_filename = filename;
    save();
    fileContents.clear();
    if (QFileInfo(p_cache_dir).exists() && QFileInfo(p_cache_dir).isDir())
        cache_filename = QDir::cleanPath(p_cache_dir + '/' + QFileInfo(load_filename).fileName() + ".c2b");
    else
        cache_filename = load_filename.trimmed() + ".c2b";
    if (QFileInfo(cache_filename).exists())
    {
        QFile file(cache_filename);
        file.open(QIODevice::ReadOnly);
        QDataStream ds(&file);
        ds >> fileContents;
    }
    cache_size = fileContents.size();
}

void c2bBibSearcherCache::save()
{
    if (cache_size != fileContents.size())
    {
        QFile file(cache_filename);
        file.open(QIODevice::WriteOnly);
        QDataStream ds(&file);
        ds << fileContents;
    }
}

const QString c2bBibSearcherCache::pdfToText(const QString& filename, int* pdfCounter, QString* logString, int* errorCounter)
{
    const QString pdf_file(filename.trimmed());
    if (pdf_file.isEmpty())
        return QString();
    QFileInfo fi(pdf_file);
    if (!fi.exists() || !fi.isFile())
    {
        (*errorCounter)++;
        (*logString) += tr("% [cb2bib] File %1 does not exists\n").arg(pdf_file);
        return QString();
    }

    (*pdfCounter)++;
    if (p_simplify_source)
    {
        if (fileContents.contains("simp:" + pdf_file))
            return QString::fromUtf8(qUncompress(fileContents.value("simp:" + pdf_file)));
    }
    else
    {
        if (fileContents.contains(pdf_file))
            return QString::fromUtf8(qUncompress(fileContents.value(pdf_file)));
    }

    document doc(pdf_file, document::Raw);
    QString text(doc.toString());
    const QString error(doc.errorString());
    if (!error.isEmpty())
    {
        (*errorCounter)++;
        (*logString) += tr("% [cb2bib] %1\n").arg(error);
        return QString();
    }
    text = text.simplified();
    fileContents.insert(pdf_file, qCompress(QByteArray(text.toUtf8())));
    const QString simp_text(c2bUtils::toAscii(text, c2bUtils::KeepWords));
    fileContents.insert("simp:" + pdf_file, qCompress(QByteArray(simp_text.toUtf8())));
    if (p_simplify_source)
        return simp_text;
    else
        return text;
}
