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

#include "commands.hpp"
#include "editsize.hpp"

namespace jasper::world {

EditWorldSizeCommand::EditWorldSizeCommand(
		WorldStaticLoader &loader,
		WorldDoc &doc,
		ox::Size const &size):
		m_loader{loader},
		m_doc{doc},
		m_oldSize{m_doc.columns, m_doc.rows},
		m_newSize{size} {}

ox::Error EditWorldSizeCommand::redo() noexcept {
	m_oldMap = m_doc.tiles;
	resize(m_doc, m_newSize);
	return m_loader.loadWorldStatic();
}

ox::Error EditWorldSizeCommand::undo() noexcept {
	resize(m_doc, m_oldSize);
	m_doc.tiles = std::move(m_oldMap);
	return m_loader.loadWorldStatic();
}

int EditWorldSizeCommand::commandId() const noexcept {
	return static_cast<int>(WorldEditorCommand::EditWorldSize);
}

EditWorldSizeCommand::Dimension EditWorldSizeCommand::dimensionChanged() const noexcept {
	auto const xChanged = m_oldSize.width == m_newSize.width;
	auto const yChanged = m_oldSize.height == m_newSize.height;
	if (xChanged && !yChanged) {
		return Dimension::X;
	} else if (!xChanged && yChanged) {
		return Dimension::Y;
	} else if (xChanged && yChanged) {
		return Dimension::Both;
	}
	return Dimension::Neither;
}

}
