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

#pragma once

#include <ox/claw/claw.hpp>

#include <nostalgia/core/context.hpp>

namespace nostalgia::studio {

class ItemMaker {
	public:
		const ox::String name;
		const ox::String parentDir;
		const ox::String fileExt;
		constexpr explicit ItemMaker(ox::String pName, ox::String pParentDir, ox::String pFileExt) noexcept:
			name(std::move(pName)),
			parentDir(std::move(pParentDir)),
			fileExt(std::move(pFileExt)) {
		}
		virtual ~ItemMaker() noexcept = default;
		virtual ox::Error write(core::Context *ctx, const char *pName) const noexcept = 0;
};

template<typename T>
class ItemMakerT: public ItemMaker {
	private:
		const T item;
		const ox::ClawFormat fmt;
	public:
		constexpr explicit ItemMakerT(ox::String pDisplayName, ox::String pParentDir, ox::String fileExt, ox::ClawFormat pFmt = ox::ClawFormat::Metal) noexcept:
			ItemMaker(std::move(pDisplayName), std::move(pParentDir), std::move(fileExt)),
			fmt(pFmt) {
		}
		constexpr ItemMakerT(ox::String pDisplayName, ox::String pParentDir, ox::String fileExt, const T &pItem, ox::ClawFormat pFmt) noexcept:
			ItemMaker(std::move(pDisplayName), std::move(pParentDir), std::move(fileExt)),
			item(pItem),
			fmt(pFmt) {
		}
		constexpr ItemMakerT(ox::String pDisplayName, ox::String pParentDir, T &&pItem, ox::ClawFormat pFmt) noexcept:
			ItemMaker(std::move(pDisplayName), std::move(pParentDir), std::move(fileExt)),
			item(std::forward(pItem)),
			fmt(pFmt) {
		}
		ox::Error write(core::Context *ctx, const char *pName) const noexcept override {
			const auto path = ox::sfmt("{}/{}.{}", parentDir, pName, fileExt);
			auto sctx = core::applicationData<studio::StudioContext>(ctx);
			return sctx->project->writeObj(path, &item, fmt);
		}
};

}
