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

#include "rmsubsheetcommand.hpp"

namespace nostalgia::gfx {

RmSubSheetCommand::RmSubSheetCommand(TileSheet &img, TileSheet::SubSheetIdx idx) noexcept:
		m_img(img),
		m_idx(std::move(idx)),
		m_parentIdx(m_idx) {
	m_parentIdx.pop_back();
}

ox::Error RmSubSheetCommand::redo() noexcept {
	auto &parent = getSubSheet(m_img, m_parentIdx);
	m_sheet = std::move(parent.subsheets[*m_idx.back().value]);
	OX_RETURN_ERROR(parent.subsheets.erase(*m_idx.back().value).error);
	return {};
}

ox::Error RmSubSheetCommand::undo() noexcept {
	auto &parent = getSubSheet(m_img, m_parentIdx);
	auto const i = *m_idx.back().value;
	parent.subsheets.insert(i, std::move(m_sheet));
	return {};
}

int RmSubSheetCommand::commandId() const noexcept {
	return static_cast<int>(CommandId::RmSubSheet);
}

TileSheet::SubSheetIdx const &RmSubSheetCommand::subsheetIdx() const noexcept {
	return m_idx;
}

}
