#include "core/rendering/palette_manager.hpp"

#include <algorithm>
#include <cctype>
#include <cmath>
#include <string>
#include <vector>

#include "core/rendering/surface.hpp"
#include "core/resources/resource_cache.hpp"
#include "game/defaults.hpp"
#include "game/options.hpp"
#include "utils/utils.hpp"

// ── Conversión string ↔ PaletteSortMode ──────────────────────────────────────

auto sortModeFromString(const std::string& str) -> PaletteSortMode {
    const std::string LOWER = toLower(str);
    if (LOWER == "luminance") { return PaletteSortMode::LUMINANCE; }
    if (LOWER == "spectrum") { return PaletteSortMode::SPECTRUM; }
    return PaletteSortMode::ORIGINAL;
}

auto sortModeToString(PaletteSortMode mode) -> std::string {
    switch (mode) {
        case PaletteSortMode::LUMINANCE:
            return "luminance";
        case PaletteSortMode::SPECTRUM:
            return "spectrum";
        default:
            return "original";
    }
}

// ── Paleta de referencia ZX Spectrum (16 colores ARGB) ───────────────────────

namespace {
    // Helpers para extraer componentes RGB de un color ARGB (0xAARRGGBB)
    constexpr auto redOf(Uint32 c) -> int { return static_cast<int>((c >> 16) & 0xFF); }
    constexpr auto greenOf(Uint32 c) -> int { return static_cast<int>((c >> 8) & 0xFF); }
    constexpr auto blueOf(Uint32 c) -> int { return static_cast<int>(c & 0xFF); }

    constexpr auto makeARGB(int r, int g, int b) -> Uint32 {
        return (0xFFU << 24) | (static_cast<Uint32>(r) << 16) | (static_cast<Uint32>(g) << 8) | static_cast<Uint32>(b);
    }

    // Paleta ZX Spectrum de referencia (misma que en tools/sort_palette/sort_palette.py)
    constexpr std::array<Uint32, 16> SPECTRUM_REFERENCE = {
        makeARGB(0, 0, 0),
        makeARGB(0, 0, 0),
        makeARGB(0, 0, 216),
        makeARGB(0, 0, 255),
        makeARGB(216, 0, 0),
        makeARGB(255, 0, 0),
        makeARGB(216, 0, 216),
        makeARGB(255, 0, 255),
        makeARGB(0, 216, 0),
        makeARGB(0, 255, 0),
        makeARGB(0, 216, 216),
        makeARGB(0, 255, 255),
        makeARGB(216, 216, 0),
        makeARGB(255, 255, 0),
        makeARGB(216, 216, 216),
        makeARGB(255, 255, 255),
    };

    // Luminancia percibida (ITU-R BT.709)
    auto luminance(Uint32 color) -> double {
        return (0.2126 * redOf(color)) + (0.7152 * greenOf(color)) + (0.0722 * blueOf(color));
    }

    // Distancia euclídea al cuadrado en espacio RGB (no necesita sqrt para comparar)
    auto rgbDistanceSq(Uint32 a, Uint32 b) -> int {
        const int DR = redOf(a) - redOf(b);
        const int DG = greenOf(a) - greenOf(b);
        const int DB = blueOf(a) - blueOf(b);
        return (DR * DR) + (DG * DG) + (DB * DB);
    }

    // Cuenta los colores activos en la paleta (los que tienen alpha != 0)
    auto countActiveColors(const Palette& palette) -> size_t {
        size_t count = 0;
        for (const auto& c : palette) {
            if (c == 0) { break; }
            ++count;
        }
        return count;
    }

    // Ordenar por luminancia
    auto sortByLuminance(const Palette& palette) -> Palette {
        const size_t N = countActiveColors(palette);
        std::vector<Uint32> colors(palette.begin(), palette.begin() + static_cast<ptrdiff_t>(N));
        std::ranges::sort(colors, [](Uint32 a, Uint32 b) {
            return luminance(a) < luminance(b);
        });

        Palette result{};
        result.fill(0);
        std::ranges::copy(colors, result.begin());
        return result;
    }

    // Ordenar por similitud con la paleta ZX Spectrum (greedy matching)
    auto sortBySpectrum(const Palette& palette) -> Palette {
        const size_t N = countActiveColors(palette);
        std::vector<Uint32> available(palette.begin(), palette.begin() + static_cast<ptrdiff_t>(N));
        std::vector<Uint32> result;
        result.reserve(N);

        // Para cada color de referencia del Spectrum, buscar el más cercano disponible
        const size_t REFS = std::min(N, SPECTRUM_REFERENCE.size());
        for (size_t i = 0; i < REFS && !available.empty(); ++i) {
            const Uint32 REF = SPECTRUM_REFERENCE[i];
            auto best = std::ranges::min_element(available, [REF](Uint32 a, Uint32 b) {
                return rgbDistanceSq(a, REF) < rgbDistanceSq(b, REF);
            });
            result.push_back(*best);
            available.erase(best);
        }

        // Si quedan colores sin asignar, añadirlos al final
        std::ranges::copy(available, std::back_inserter(result));

        Palette out{};
        out.fill(0);
        std::ranges::copy(result, out.begin());
        return out;
    }
}  // namespace

// ── PaletteManager ───────────────────────────────────────────────────────────

PaletteManager::PaletteManager(
    std::vector<std::string> raw_paths,
    const std::string& initial_name,
    PaletteSortMode initial_sort_mode,
    std::shared_ptr<Surface> game_surface,
    std::shared_ptr<Surface> border_surface,
    OnChangeCallback on_change)
    : palettes_(std::move(raw_paths)),
      sort_mode_(initial_sort_mode),
      game_surface_(std::move(game_surface)),
      border_surface_(std::move(border_surface)),
      on_change_(std::move(on_change)) {
    current_ = findIndex(initial_name);

    // Leer y aplicar paleta inicial directamente desde el archivo
    // (Resource::Cache aún no está disponible en este punto del ciclo de vida)
    const auto INITIAL_PALETTE = sortPalette(readPalFile(palettes_.at(current_)), sort_mode_);
    game_surface_->setPalette(INITIAL_PALETTE);
    border_surface_->setPalette(INITIAL_PALETTE);

    // Procesar la lista: conservar solo los nombres de archivo (sin ruta)
    processPathList();
}

void PaletteManager::next() {
    if (++current_ == palettes_.size()) {
        current_ = 0;
    }
    apply();
}

void PaletteManager::previous() {
    current_ = (current_ > 0) ? current_ - 1 : palettes_.size() - 1;
    apply();
}

auto PaletteManager::setByName(const std::string& name) -> bool {
    const std::string LOWER_NAME = toLower(name + ".pal");
    for (size_t i = 0; i < palettes_.size(); ++i) {
        if (toLower(palettes_[i]) == LOWER_NAME) {
            current_ = i;
            apply();
            return true;
        }
    }
    return false;
}

auto PaletteManager::getNames() const -> std::vector<std::string> {
    std::vector<std::string> names;
    names.reserve(palettes_.size());
    for (const auto& p : palettes_) {
        std::string name = p;
        const size_t POS = name.find(".pal");
        if (POS != std::string::npos) { name.erase(POS, 4); }
        std::ranges::transform(name, name.begin(), ::tolower);
        names.push_back(std::move(name));
    }
    return names;
}

auto PaletteManager::getCurrentName() const -> std::string {
    std::string name = palettes_.at(current_);
    const size_t POS = name.find(".pal");
    if (POS != std::string::npos) { name.erase(POS, 4); }
    std::ranges::transform(name, name.begin(), ::tolower);
    return name;
}

auto PaletteManager::getPrettyName() const -> std::string {
    std::string name = getCurrentName();
    std::ranges::replace(name, '-', ' ');
    return name;
}

void PaletteManager::nextSortMode() {
    sort_mode_ = static_cast<PaletteSortMode>((static_cast<int>(sort_mode_) + 1) % static_cast<int>(PaletteSortMode::COUNT));
    Options::video.palette_sort = sortModeToString(sort_mode_);
    apply();
}

void PaletteManager::setSortMode(PaletteSortMode mode) {
    sort_mode_ = mode;
    Options::video.palette_sort = sortModeToString(sort_mode_);
    apply();
}

auto PaletteManager::getSortMode() const -> PaletteSortMode {
    return sort_mode_;
}

auto PaletteManager::getSortModeName() const -> std::string {
    return sortModeToString(sort_mode_);
}

void PaletteManager::apply() {
    Palette raw = Resource::Cache::get()->getPalette(palettes_.at(current_));
    Palette sorted = sortPalette(raw, sort_mode_);
    game_surface_->loadPalette(sorted);
    border_surface_->loadPalette(sorted);

    Options::video.palette = getCurrentName();

    if (on_change_) {
        on_change_();
    }
}

auto PaletteManager::findIndex(const std::string& name) const -> size_t {
    const std::string LOWER_NAME = toLower(name + ".pal");
    for (size_t i = 0; i < palettes_.size(); ++i) {
        if (toLower(getFileName(palettes_[i])) == LOWER_NAME) {
            return i;
        }
    }
    // Fallback: buscar la paleta por defecto
    const std::string DEFAULT_NAME = toLower(std::string(Defaults::Video::PALETTE_NAME) + ".pal");
    for (size_t i = 0; i < palettes_.size(); ++i) {
        if (toLower(getFileName(palettes_[i])) == DEFAULT_NAME) {
            return i;
        }
    }
    return 0;
}

void PaletteManager::processPathList() {
    for (auto& palette : palettes_) {
        palette = getFileName(palette);
    }
}

auto PaletteManager::sortPalette(const Palette& palette, PaletteSortMode mode) -> Palette {
    switch (mode) {
        case PaletteSortMode::LUMINANCE:
            return sortByLuminance(palette);
        case PaletteSortMode::SPECTRUM:
            return sortBySpectrum(palette);
        default:
            return palette;
    }
}
