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

#include "editobject.hpp"

namespace jasper::world {

EditObjectSubSheet::EditObjectSubSheet(
		WorldObjectSet &doc,
		size_t const objIdx,
		ngfx::SubSheetId const newSubSheet,
		uint8_t const newFrames):
	m_doc{doc},
	m_objIdx{objIdx},
	m_oldSubSheet{m_doc.objects[objIdx].subsheetId},
	m_newSubSheet{newSubSheet},
	m_oldFrames{m_doc.objects[objIdx].frames},
	m_newFrames{newFrames} {
	if (m_newSubSheet == m_oldSubSheet && m_newFrames == m_oldFrames) {
		throw ox::Exception{1, "EditObjectSubsheet: no change occurred"};
	}
}

ox::Error EditObjectSubSheet::redo() noexcept {
	auto &obj = m_doc.objects[m_objIdx];
	obj.subsheetId = m_newSubSheet;
	obj.frames = m_newFrames;
	return {};
}

ox::Error EditObjectSubSheet::undo() noexcept {
	auto &obj = m_doc.objects[m_objIdx];
	obj.subsheetId = m_oldSubSheet;
	obj.frames = m_oldFrames;
	return {};
}

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

}