// locale.cpp - Implementació del sistema de locale
// © 2026 JailDesigner

#include "core/locale/locale.hpp"

#include <cstddef>
#include <cstdint>
#include <exception>
#include <iostream>
#include <sstream>
#include <string>
#include <string_view>
#include <vector>

#include "core/resources/resource_helper.hpp"
#include "external/fkyaml_node.hpp"

namespace {

    // Recorre el node YAML i aplana jerarquies en claus "a.b.c". Suporta
    // mappings (recursió) i seqüències de strings (desa "a.b.0", "a.b.1"...).
    // Altres tipus (nombres, booleans solts) s'ignoren silenciosament.
    void flatten(const fkyaml::node& node, const std::string& prefix, std::unordered_map<std::string, std::string>& out) {
        if (node.is_mapping()) {
            for (auto it = node.begin(); it != node.end(); ++it) {
                const std::string KEY = prefix.empty()
                    ? it.key().get_value<std::string>()
                    : prefix + "." + it.key().get_value<std::string>();
                flatten(it.value(), KEY, out);
            }
            return;
        }
        if (node.is_sequence()) {
            std::size_t index = 0;
            for (const auto& item : node) {
                const std::string KEY = prefix + "." + std::to_string(index);
                flatten(item, KEY, out);
                index++;
            }
            return;
        }
        if (node.is_string()) {
            out[prefix] = node.get_value<std::string>();
        }
    }

}  // namespace

auto Locale::get() -> Locale& {
    static Locale instance_;
    return instance_;
}

auto Locale::load(const std::string& file_path) -> bool {
    // Normalitza traient prefix "data/" com fa StageLoader: el pack de
    // recursos indexa rutes relatives a `data/`.
    std::string normalized = file_path;
    if (normalized.starts_with("data/")) {
        normalized = normalized.substr(5);
    }

    std::vector<uint8_t> bytes = Resource::Helper::loadFile(normalized);
    if (bytes.empty()) {
        std::cerr << "[Locale] no s'ha pogut load " << normalized << '\n';
        return false;
    }

    try {
        std::string yaml_content(bytes.begin(), bytes.end());
        std::stringstream stream(yaml_content);
        fkyaml::node yaml = fkyaml::node::deserialize(stream);
        strings_.clear();
        flatten(yaml, "", strings_);
        std::cout << "[Locale] " << strings_.size() << " traduccions des de " << normalized << '\n';
        return true;
    } catch (const std::exception& e) {
        std::cerr << "[Locale] error parsejant " << normalized << ": " << e.what() << '\n';
        return false;
    }
}

auto Locale::switchTo(const std::string& lang) -> bool {
    return load("locale/" + lang + ".yaml");
}

auto Locale::text(const std::string& key) const -> std::string {
    auto it = strings_.find(key);
    if (it != strings_.end()) {
        return it->second;
    }
    std::cerr << "[Locale] clau no trobada: " << key << '\n';
    return key;
}

auto Locale::count(const std::string& prefix) const -> std::size_t {
    std::size_t n = 0;
    while (strings_.contains(prefix + "." + std::to_string(n))) {
        n++;
    }
    return n;
}

auto localeSubstitute(std::string tpl, std::string_view placeholder, std::string_view value) -> std::string {
    auto pos = tpl.find(placeholder);
    if (pos != std::string::npos) {
        tpl.replace(pos, placeholder.size(), value);
    }
    return tpl;
}
