/**
 * @file color.cpp
 * @brief Implementación de la gestión de colores de la paleta Amstrad CPC
 */

#include "color.hpp"

#include <unordered_map>

auto Color::fromString(const std::string& name) -> Uint8 {
    // Mapa de nombres de colores a índices de paleta
    // Incluye nombres oficiales del CPC y aliases para compatibilidad
    static const std::unordered_map<std::string, Uint8> COLOR_MAP = {
        // Transparente
        {"transparent", getIndex(Cpc::CLEAR)},

        // Colores oficiales Amstrad CPC
        {"black", getIndex(Cpc::BLACK)},
        {"blue", getIndex(Cpc::BLUE)},
        {"bright_blue", getIndex(Cpc::BRIGHT_BLUE)},
        {"red", getIndex(Cpc::RED)},
        {"magenta", getIndex(Cpc::MAGENTA)},
        {"mauve", getIndex(Cpc::MAUVE)},
        {"bright_red", getIndex(Cpc::BRIGHT_RED)},
        {"purple", getIndex(Cpc::PURPLE)},
        {"bright_magenta", getIndex(Cpc::BRIGHT_MAGENTA)},
        {"green", getIndex(Cpc::GREEN)},
        {"cyan", getIndex(Cpc::CYAN)},
        {"sky_blue", getIndex(Cpc::SKY_BLUE)},
        {"yellow", getIndex(Cpc::YELLOW)},
        {"white", getIndex(Cpc::WHITE)},
        {"pastel_blue", getIndex(Cpc::PASTEL_BLUE)},
        {"orange", getIndex(Cpc::ORANGE)},
        {"pink", getIndex(Cpc::PINK)},
        {"pastel_magenta", getIndex(Cpc::PASTEL_MAGENTA)},
        {"bright_green", getIndex(Cpc::BRIGHT_GREEN)},
        {"sea_green", getIndex(Cpc::SEA_GREEN)},
        {"bright_cyan", getIndex(Cpc::BRIGHT_CYAN)},
        {"lime", getIndex(Cpc::LIME)},
        {"pastel_green", getIndex(Cpc::PASTEL_GREEN)},
        {"pastel_cyan", getIndex(Cpc::PASTEL_CYAN)},
        {"bright_yellow", getIndex(Cpc::BRIGHT_YELLOW)},
        {"pastel_yellow", getIndex(Cpc::PASTEL_YELLOW)},
        {"bright_white", getIndex(Cpc::BRIGHT_WHITE)},

        // Aliases para compatibilidad con archivos YAML existentes (Spectrum)
        {"bright_black", getIndex(Cpc::BLACK)},  // No existe en CPC, mapea a negro
    };

    auto it = COLOR_MAP.find(name);
    if (it != COLOR_MAP.end()) {
        return it->second;
    }

    // Si no se encuentra, devuelve negro por defecto
    return getIndex(Cpc::BLACK);
}
