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

#include "commands.hpp"
#include "removecolorcommand.hpp"

namespace nostalgia::gfx {

RemoveColorCommand::RemoveColorCommand(Palette &pal, size_t const idx) noexcept:
	m_pal(pal),
	m_idx(idx),
	m_colors([this] {
		ox::Vector<Color16> colors;
		colors.reserve(m_pal.pages.size());
		for (auto const&p : m_pal.pages) {
			colors.emplace_back(p.colors[m_idx]);
		}
		return colors;
	}()) {}

int RemoveColorCommand::commandId() const noexcept {
	return static_cast<int>(PaletteEditorCommandId::RemoveColor);
}

ox::Error RemoveColorCommand::redo() noexcept {
	m_colorInfo = std::move(m_pal.colorNames[m_idx]);
	OX_RETURN_ERROR(m_pal.colorNames.erase(m_idx));
	for (auto &page : m_pal.pages) {
		OX_RETURN_ERROR(page.colors.erase(m_idx));
	}
	return {};
}

ox::Error RemoveColorCommand::undo() noexcept {
	m_pal.colorNames.emplace(m_idx, std::move(m_colorInfo));
	for (size_t p = 0; auto &page : m_pal.pages) {
		page.colors.emplace(m_idx, m_colors[p]);
		++p;
	}
	return {};
}

}
