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

#include "commands.hpp"
#include "changetilesheet.hpp"

#include <utility>
#include <keel/media.hpp>

namespace jasper::world {

ChangeTileSheet::ChangeTileSheet(
		keel::Context &kctx,
		WorldObjectSet &doc,
		ox::FileAddress newVal,
		ox::String &path) noexcept:
	m_kctx{kctx},
	m_doc(doc),
	m_valAlt(std::move(newVal)),
	m_selectedTilesheet(path) {
}

ox::Error ChangeTileSheet::redo() noexcept {
	return swap();
}

ox::Error ChangeTileSheet::undo() noexcept {
	return swap();
}

int ChangeTileSheet::commandId() const noexcept {
	return static_cast<int>(WorldObjCommand::ChangeTileSheet);
}

ox::Error ChangeTileSheet::swap() noexcept {
	std::swap(m_doc.tilesheet, m_valAlt);
	OX_REQUIRE_M(path, m_doc.tilesheet.getPath());
	m_selectedTilesheet = keel::uuidUrlToPath(m_kctx, path).or_value(path);
	return {};
}

}
