/*
 * Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved.
 */

#include <ox/claw/read.hpp>
#include <ox/fs/fs.hpp>
#include <ox/mc/write.hpp>
#include <ox/model/descwrite.hpp>
#include <ox/model/modelvalue.hpp>

#include <nostalgia/core/gfx.hpp>
#include <nostalgia/core/typeconv.hpp>
#include <nostalgia/core/typestore.hpp>

#include "pack.hpp"

namespace nostalgia {

static ox::Error pathToInode(ox::FileSystem *dest, ox::ModelObject *obj) noexcept {
	auto &o = *obj;
	auto type = static_cast<ox::FileAddressType>(o["type"].get<int8_t>());
	auto &data = o["data"].get<ox::ModelUnion>();
	ox::String path;
	switch (type) {
		case ox::FileAddressType::Path:
			path = data["path"].get<ox::String>();
			break;
		case ox::FileAddressType::ConstPath:
			path = data["constPath"].get<ox::String>();
			break;
		case ox::FileAddressType::Inode:
		case ox::FileAddressType::None:
			return OxError(0);
	}
	oxRequire(s, dest->stat(path.c_str()));
	oxReturnError(o["type"].set(static_cast<int8_t>(ox::FileAddressType::Inode)));
	return data.set(2, s.inode);
}

/**
 * Convert path references in Claw data to inodes to save space
 * @param buff buffer holding file
 * @return error
 * stub for now
 */
static ox::Error transformObj(ox::FileSystem *dest, ox::ModelObject *obj) noexcept {
	for (auto &f : *obj) {
		auto &v = f->value;
		if (v.type() != ox::ModelValue::Type::Object) {
			continue;
		}
		auto &o = v.get<ox::ModelObject>();
		if (o.typeName() == "net.drinkingtea.ox.FileAddress" && o.typeVersion() == 1) {
			oxReturnError(pathToInode(dest, &o));
		} else {
			oxReturnError(transformObj(dest, &o));
		}
	}
	return OxError(0);
}

static ox::Error doTransformations(core::TypeStore *ts, ox::FileSystem *dest, const ox::String &filePath) noexcept {
	if (filePath.endsWith(".ng") || filePath.endsWith(".npal")) {
		// load file
		oxRequire(s, dest->stat(filePath.c_str()));
		oxRequireM(buff, dest->read(s.inode));
		if (filePath.endsWith(".ng")) {
			oxReturnError(core::convertBuffToBuff<core::CompactTileSheet>(buff, ox::ClawFormat::Metal).moveTo(&buff));
		}
		oxRequireM(obj, ox::readClaw(ts, buff));
		// do transformations
		oxReturnError(transformObj(dest, &obj));
		oxReturnError(ox::writeMC(&obj).moveTo(&buff));
		// write file to dest
		oxReturnError(dest->write(s.inode, buff.data(), buff.size()));
	}
	return OxError(0);
}

// claw file transformations are broken out because path to inode
// transformations need to be done after the copy to the new FS is complete
static ox::Error transformClaw(core::TypeStore *ts, ox::FileSystem *dest, const ox::String &path) noexcept {
	// copy
	oxTracef("pack::transformClaw", "path: {}", path);
	oxRequire(fileList, dest->ls(path));
	for (const auto &name : fileList) {
		const auto filePath = path + name;
		oxRequire(stat, dest->stat(filePath.c_str()));
		if (stat.fileType == ox::FileType::Directory) {
			const auto dir = path + name + '/';
			oxReturnError(transformClaw(ts, dest, dir));
		} else {
			oxReturnError(doTransformations(ts, dest, filePath));
		}
	}
	return OxError(0);
}

static ox::Error verifyFile(ox::FileSystem *fs, const ox::String &path, const ox::Buffer &expected) noexcept {
	ox::Buffer buff(expected.size());
	oxReturnError(fs->read(path.c_str(), buff.data(), buff.size()));
	return OxError(buff == expected ? 0 : 1);
}

struct VerificationPair {
	ox::String path;
	ox::Buffer buff;
	VerificationPair(const ox::String &pPath, ox::Buffer pBuff) noexcept:
		path(pPath),
		buff(std::move(pBuff)) {
	}
};

static ox::Error copy(ox::FileSystem *src, ox::FileSystem *dest, const ox::String &path) noexcept {
	oxOutf("copying directory: {}\n", path);
	ox::Vector<VerificationPair> verificationPairs;
	// copy
	oxRequire(fileList, src->ls(path));
	for (const auto &name : fileList) {
		const auto currentFile = path + name;
		if (currentFile == "/.nostalgia") {
			continue;
		}
		oxOutf("reading {}\n", name);
		oxRequire(stat, src->stat(currentFile.c_str()));
		if (stat.fileType == ox::FileType::Directory) {
			oxReturnError(dest->mkdir(currentFile.c_str(), true));
			oxReturnError(copy(src, dest, currentFile + '/'));
		} else {
			// load file
			oxRequireM(buff, src->read(currentFile.c_str()));
			// write file to dest
			oxOutf("writing {}\n", currentFile);
			oxReturnError(dest->write(currentFile.c_str(), buff.data(), buff.size()));
			oxReturnError(verifyFile(dest, currentFile, buff));
			verificationPairs.emplace_back(currentFile, std::move(buff));
		}
	}
	// verify all at once in addition to right after the files are written
	for (const auto &v : verificationPairs) {
		oxReturnError(verifyFile(dest, v.path, v.buff));
	}
	return OxError(0);
}

ox::Error pack(ox::FileSystem *src, ox::FileSystem *dest) noexcept {
	oxReturnError(copy(src, dest, "/"));
	core::TypeStore ts(src);
	oxReturnError(ox::buildTypeDef<core::CompactTileSheet>(&ts));
	oxReturnError(transformClaw(&ts, dest, "/"));
	return OxError(0);
}

}
