/****************************************************************************
 ** Makou Reactor Final Fantasy VII Field Script Editor
 ** Copyright (C) 2009-2022 Arzel Jérôme <myst6re@gmail.com>
 **
 ** This program is free software: you can redistribute it and/or modify
 ** it under the terms of the GNU General Public License as published by
 ** the Free Software Foundation, either version 3 of the License, or
 ** (at your option) any later version.
 **
 ** This program is distributed in the hope that it will be useful,
 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 ** GNU General Public License for more details.
 **
 ** You should have received a copy of the GNU General Public License
 ** along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************/
#include <LZS>
#include "FieldArchiveIO.h"
#include "FieldArchive.h"
#include "Field.h"

QByteArray FieldArchiveIO::fieldDataCache;
Field *FieldArchiveIO::fieldCache = nullptr;
QString FieldArchiveIO::fieldExtensionCache;

FieldArchiveIO::FieldArchiveIO(FieldArchive *fieldArchive) :
	_fieldArchive(fieldArchive)
{
}

FieldArchiveIO::~FieldArchiveIO()
{
	clearCachedData();
}

FieldArchive *FieldArchiveIO::fieldArchive()
{
	return _fieldArchive;
}

QString FieldArchiveIO::name() const
{
	if (!hasName())
		return QString();

	QString filePath = path();
	if (filePath.isEmpty()) {
		return filePath;
	}

	return filePath.mid(filePath.lastIndexOf('/') + 1);
}

QString FieldArchiveIO::directory() const
{
	if (!hasName())
		return path() + "/";

	QString filePath = path();
	if (filePath.isEmpty()) {
		return filePath;
	}

	return filePath.left(filePath.lastIndexOf('/') + 1);
}

QByteArray FieldArchiveIO::fieldData(Field *field, const QString &extension, bool unlzs)
{
	// use data from the cache
	if (unlzs && fieldDataIsCached(field, extension)) {
//		qDebug() << "FieldArchive use field data from cache" << field->name();
		return fieldDataCache;
	} /*else {
		qDebug() << "FieldArchive don't use field data from cache" << field->name() << unlzs;
	}*/

	QByteArray data = fieldData2(field, extension, unlzs);

	// put decompressed data in the cache
	if (unlzs && !data.isEmpty()) {
		fieldCache = field;
		fieldDataCache = data;
		fieldExtensionCache = extension;
	}
	return data;
}

QByteArray FieldArchiveIO::fileData(const QString &fileName, bool unlzs)
{
	QByteArray data = fileData2(fileName);

	if (unlzs) {
		if (data.size() < 4) {
			return QByteArray();
		}

		const char *lzsDataConst = data.constData();
		quint32 lzsSize;
		memcpy(&lzsSize, lzsDataConst, 4);

		if (quint32(data.size()) != lzsSize + 4 && lzsSize == 0x90000) { // Maybe it is not compressed
			unlzs = false;
		}

		return unlzs
			? LZS::decompressAll(lzsDataConst + 4, int(qMin(qsizetype(lzsSize), data.size() - 4)))
			: data;
	}
	return data;
}

int FieldArchiveIO::exportFieldData(Field *field, const QString &extension, const QString &path, bool unlzs)
{
	QByteArray newData = fieldData(field, extension, unlzs);

	if (!newData.isEmpty()) {
		QFile fic(path);
		if (!fic.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
			return 2;
		}
		fic.write(newData);
		fic.close();
	} else {
		return 1;
	}

	return 0;
}

bool FieldArchiveIO::fieldDataIsCached(Field *field, const QString &fileType)
{
	return fieldCache && fieldCache == field && fieldExtensionCache == fileType;
}

void FieldArchiveIO::clearCachedData()
{
//	qDebug() << "FieldArchive::clearCachedData()";
	fieldCache = nullptr;
	fieldDataCache.clear();
}

void FieldArchiveIO::close()
{
	clearCachedData();
}

FieldArchiveIO::ErrorCode FieldArchiveIO::open(ArchiveObserver *observer)
{
	clearCachedData();

	return open2(observer);
}

FieldArchiveIO::ErrorCode FieldArchiveIO::save(const QString &path, ArchiveObserver *observer)
{
	ErrorCode error = save2(path, observer);
	if (error == Ok) {
		clearCachedData(); // Important: the file data will change
	}
	return error;
}

FieldArchiveIO::ErrorCode FieldArchiveIO::addField(const QString &fileName,
                                                   const QString &name)
{
	Q_UNUSED(fileName)
	Q_UNUSED(name)
	return NotImplemented;
}
