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

#include "commands.hpp"

#include "duplicatepagecommand.hpp"

namespace nostalgia::gfx {

DuplicatePageCommand::DuplicatePageCommand(Palette &pal, size_t srcIdx, size_t dstIdx) noexcept:
		m_pal(pal),
		m_dstIdx(dstIdx) {
	auto const&src = m_pal.pages[srcIdx];
	m_page.reserve(src.colors.size());
	for (auto const&s : src.colors) {
		m_page.emplace_back(s);
	}
}

int DuplicatePageCommand::commandId() const noexcept {
	return static_cast<int>(PaletteEditorCommandId::DuplicatePage);
}

ox::Error DuplicatePageCommand::redo() noexcept {
	m_pal.pages.emplace(m_dstIdx, ox::sfmt("Page {}", m_pal.pages.size() + 1), std::move(m_page));
	return {};
}

ox::Error DuplicatePageCommand::undo() noexcept {
	m_page = std::move(m_pal.pages[m_dstIdx].colors);
	return m_pal.pages.erase(m_dstIdx).error;
}

size_t DuplicatePageCommand::insertIdx() const noexcept {
	return m_dstIdx;
}

}