/*
 * Copyright 2015 - 2025 gary@drinkingtea.net
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

// make sure asserts are enabled for the test file
#undef NDEBUG

#include <functional>
#include <map>
#include <string_view>
#include <ox/std/std.hpp>
#include <ox/fs/ptrarith/nodebuffer.hpp>
#include <ox/fs/filestore/filestoretemplate.hpp>
#include <ox/fs/filesystem/filesystem.hpp>

template<typename T>
struct OX_PACKED NodeType: public ox::ptrarith::Item<T> {
	public:
		[[nodiscard]]
		size_t fullSize() const {
			return this->size() + sizeof(*this);
		}
};

const std::map<ox::StringView, std::function<ox::Error(ox::StringView)>> tests = {
	{
		{
			"PtrArith::setSize",
			[](ox::StringView) {
				using BuffPtr_t = uint32_t;
				ox::Vector<char> buff(5 * ox::units::MB);
				auto buffer = new (buff.data()) ox::ptrarith::NodeBuffer<BuffPtr_t, NodeType<BuffPtr_t>>(buff.size());
				using String = ox::IString<6>;
				auto a1 = buffer->malloc(sizeof(String)).value;
				auto a2 = buffer->malloc(sizeof(String)).value;
				oxAssert(a1.valid(), "Allocation 1 failed.");
				oxAssert(buffer->free(a1), "Free failed.");
				oxAssert(buffer->free(a2), "Free failed.");
				oxAssert(buffer->setSize(buffer->size() - buffer->available()), "Resize failed.");
				return ox::Error(0);
			}
		},
		{
			"PathIterator::next1",
			[](ox::StringView) {
				auto constexpr path = ox::StringLiteral("/usr/share/charset.gbag");
				ox::PathIterator it(path.c_str(), path.size());
				ox::StringView buff;
				oxAssert(it.next(buff) == 0 && buff == "usr", "PathIterator shows wrong next");
				oxAssert(it.next(buff) == 0 && buff == "share", "PathIterator shows wrong next");
				oxAssert(it.next(buff) == 0 && buff == "charset.gbag", "PathIterator shows wrong next");
				return ox::Error(0);
			}
		},
		{
			"PathIterator::next2",
			[](ox::StringView) {
				auto constexpr path = ox::StringView("/usr/share/");
				ox::PathIterator it(path);
				ox::StringView buff;
				oxAssert(it.next(buff), "PathIterator::next returned error");
				ox::expect(buff, "usr");
				oxAssert(it.next(buff), "PathIterator::next returned error");
				ox::expect(buff, "share");
				return ox::Error(0);
			}
		},
		{
			"PathIterator::next3",
			[](ox::StringView) {
				auto const path = ox::String("/");
				ox::PathIterator it(path.c_str(), path.size());
				ox::StringView buff;
				oxAssert(it.next(buff) == 0 && buff == "\0", "PathIterator shows wrong next");
				return ox::Error(0);
			}
		},
		{
			"PathIterator::next4",
			[](ox::StringView) {
				auto constexpr path = ox::StringLiteral("usr/share/charset.gbag");
				ox::PathIterator it(path);
				ox::StringView buff;
				oxAssert(it.next(buff) == 0 && buff == "usr", "PathIterator shows wrong next");
				oxAssert(it.next(buff) == 0 && buff == "share", "PathIterator shows wrong next");
				oxAssert(it.next(buff) == 0 && buff == "charset.gbag", "PathIterator shows wrong next");
				return ox::Error(0);
			}
		},
		{
			"PathIterator::next5",
			[](ox::StringView) {
				auto const path = ox::String("usr/share/");
				ox::PathIterator it(path.c_str(), path.size());
				ox::StringView buff;
				oxAssert(it.next(buff) == 0 && buff == "usr", "PathIterator shows wrong next");
				oxAssert(it.next(buff) == 0 && buff == "share", "PathIterator shows wrong next");
				return ox::Error(0);
			}
		},
		{
			"PathIterator::dirPath",
			[] (ox::StringView) {
				auto constexpr path = ox::StringLiteral("/usr/share/charset.gbag");
				ox::PathIterator it(path.c_str(), path.size());
				auto buff = static_cast<char*>(ox_alloca(path.size() + 1));
				OX_ALLOW_UNSAFE_BUFFERS_BEGIN
				oxAssert(it.dirPath(buff, path.size()) == 0 && ox::strcmp(buff, "/usr/share/") == 0, "PathIterator shows incorrect dir path");
				OX_ALLOW_UNSAFE_BUFFERS_END
				return ox::Error(0);
			}
		},
		{
			"PathIterator::hasNext",
			[](ox::StringView) {
				const auto path = "/file1";
				OX_ALLOW_UNSAFE_BUFFERS_BEGIN
				ox::PathIterator it(path, ox::strlen(path));
				OX_ALLOW_UNSAFE_BUFFERS_END
				oxAssert(it.hasNext(), "PathIterator shows incorrect hasNext");
				oxAssert(!it.next().hasNext(), "PathIterator shows incorrect hasNext");
				return ox::Error(0);
			}
		},
		{
			"Ptr::subPtr",
			[](ox::StringView) {
				constexpr auto buffLen = 5000;
				ox::ptrarith::Ptr<uint8_t, uint32_t> p(ox_alloca(buffLen), buffLen, 500, 500);
				oxAssert(p.valid(), "Ptr::subPtr: Ptr p is invalid.");

				auto subPtr = p.subPtr<uint64_t>(50);
				oxAssert(subPtr.valid(), "Ptr::subPtr: Ptr subPtr is invalid.");
				return ox::Error(0);
			}
		},
		{
			"NodeBuffer::insert",
			[](ox::StringView) {
				constexpr auto buffLen = 5000;
				auto list = new (ox_alloca(buffLen)) ox::ptrarith::NodeBuffer<uint32_t, ox::FileStoreItem<uint32_t>>(buffLen);
				oxAssert(list->malloc(50).value.valid(), "NodeBuffer::insert: malloc 1 failed");
				oxAssert(list->malloc(50).value.valid(), "NodeBuffer::insert: malloc 2 failed");
				auto first = list->firstItem();
				oxAssert(first.valid(), "NodeBuffer::insert: Could not access first item");
				oxAssert(first->size() == 50, "NodeBuffer::insert: First item size invalid");
				return ox::Error(0);
			}
		},
		{
			"FileStore::readWrite",
			[](ox::StringView) {
				constexpr auto buffLen = 5000;
				constexpr auto str1 = "Hello, World!";
				OX_ALLOW_UNSAFE_BUFFERS_BEGIN
				constexpr auto str1Len = ox::strlen(str1) + 1;
				constexpr auto str2 = "Hello, Moon!";
				constexpr auto str2Len = ox::strlen(str2) + 1;
				OX_ALLOW_UNSAFE_BUFFERS_END
				auto list = new (ox_alloca(buffLen)) ox::ptrarith::NodeBuffer<uint32_t, ox::FileStoreItem<uint32_t>>(buffLen);
				oxAssert(ox::FileStore32::format(list, buffLen), "FileStore::format failed.");
				ox::FileStore32 fileStore(list, buffLen);
				oxAssert(fileStore.write(4, str1, str1Len, 1), "FileStore::write 1 failed.");
				oxAssert(fileStore.write(5, str2, str2Len, 1), "FileStore::write 2 failed.");

				char str1Read[str1Len];
				size_t str1ReadSize = 0;
				oxAssert(fileStore.read(4, reinterpret_cast<void*>(str1Read), str1Len, &str1ReadSize), "FileStore::read 1 failed.");

				return ox::Error(0);
			}
		},
		{
			"Directory",
			[](ox::StringView) {
				ox::Vector<uint8_t> fsBuff(5000);
				oxAssert(ox::FileStore32::format(fsBuff.data(), fsBuff.size()), "FS format failed");
				ox::FileStore32 fileStore(fsBuff.data(), fsBuff.size());
				ox::Directory32 dir(fileStore, 105);

				oxTrace("ox.fs.test.Directory") << "Init";
				oxAssert(dir.init(), "Init failed");

				oxTrace("ox.fs.test.Directory") << "write 1";
				oxAssert(dir.write("/file1", 1), "Directory write of file1 failed");

				oxTrace("ox.fs.test.Directory") << "find";
				oxAssert(dir.find("file1").error, "Could not find file1");
				oxAssert(dir.find("file1").value == 1, "Could not find file1");

				oxTrace("ox.fs.test.Directory") << "write 2";
				oxAssert(dir.write("/file3", 3), "Directory write of file3 failed");

				oxTrace("ox.fs.test.Directory") << "write 3";
				oxAssert(dir.write("/file2", 2), "Directory write of file2 failed");

				return ox::Error(0);
			}
		},
		{
			"FileSystem",
			[](ox::StringView) {
				ox::Vector<uint8_t> fsBuff(5000);
				oxTrace("ox.fs.test.FileSystem") << "format";
				oxAssert(ox::FileSystem32::format(fsBuff.data(), fsBuff.size()), "FileSystem format failed");
				ox::FileSystem32 fs(ox::FileStore32(fsBuff.data(), fsBuff.size()));
				oxTrace("ox.fs.test.FileSystem") << "mkdir";
				oxAssert(fs.mkdir("/dir", true), "mkdir failed");
				oxAssert(fs.stat("/dir").error, "mkdir failed");
				oxAssert(fs.mkdir("/l1d1/l2d1/l3d1", true), "mkdir failed");
				oxAssert(fs.stat("/l1d1/l2d1/l3d1").error, "mkdir failed");
				oxAssert(fs.mkdir("/l1d1/l2d2", true), "mkdir failed");
				oxAssert(fs.stat("/l1d1/l2d2").error, "mkdir failed");
				return ox::Error(0);
			}
		},
	},
};

int main(int argc, const char **argv) {
	if (argc < 2) {
		oxError("Must specify test to run");
		return -1;
	}
	auto const args = ox::Span{argv, static_cast<size_t>(argc)};
	ox::StringView const testName = args[1];
	ox::StringView const testArg = argc >= 3 ? args[2] : nullptr;
	auto const func = tests.find(testName);
	if (func != tests.end()) {
		oxAssert(func->second(testArg), "Test returned Error");
		return 0;
	}
	return -1;
}
