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

#include "commands.hpp"

#include "movecolorcommand.hpp"

namespace nostalgia::gfx {

MoveColorCommand::MoveColorCommand(
		Palette &pal,
		size_t const srcIdx,
		size_t const dstIdx) noexcept:
	m_pal(pal),
	m_srcIdx(srcIdx),
	m_dstIdx(dstIdx) {}

int MoveColorCommand::commandId() const noexcept {
	return static_cast<int>(PaletteEditorCommandId::MoveColor);
}

ox::Error MoveColorCommand::redo() noexcept {
	moveColor(m_srcIdx, m_dstIdx);
	return {};
}

ox::Error MoveColorCommand::undo() noexcept {
	moveColor(m_dstIdx, m_srcIdx);
	return {};
}

void MoveColorCommand::moveColor(
		size_t const srcIdx, size_t const dstIdx) noexcept {
	for (size_t page{}; page < m_pal.pages.size(); ++page) {
		auto const c = color(m_pal, page, srcIdx);
		std::ignore = colors(m_pal, page).erase(srcIdx);
		colors(m_pal, page).emplace(dstIdx, c);
	}
	{
		auto name = std::move(m_pal.colorNames[srcIdx]);
		std::ignore = m_pal.colorNames.erase(srcIdx);
		m_pal.colorNames.emplace(dstIdx, std::move(name));
	}
}

}
