// DragonX Wallet - ImGui Edition
// Copyright 2024-2026 The Hush Developers
// Released under the GPLv3

#pragma once

#include <string>
#include <unordered_map>
#include <vector>
#include <nlohmann/json_fwd.hpp>

namespace dragonx {
namespace util {

/**
 * @brief Internationalization support
 * 
 * Simple string table implementation for translating UI text.
 * Strings are stored in JSON files in res/lang/ directory.
 */
class I18n {
public:
    /**
     * @brief Get the singleton instance
     */
    static I18n& instance();
    
    /**
     * @brief Load a language file
     * @param locale Language code (e.g., "en", "es", "zh")
     * @return true if loaded successfully
     */
    bool loadLanguage(const std::string& locale);
    
    /**
     * @brief Translate a string
     * @param key The string key (usually English text)
     * @return Translated string, or key if not found
     */
    const char* translate(const char* key) const;
    
    /**
     * @brief Get current locale
     */
    const std::string& getCurrentLocale() const { return current_locale_; }
    
    /**
     * @brief Get list of available languages
     */
    const std::vector<std::pair<std::string, std::string>>& getAvailableLanguages() const { return available_languages_; }
    
    /**
     * @brief Register an available language
     * @param code Language code (e.g., "en")
     * @param name Display name (e.g., "English")
     */
    void registerLanguage(const std::string& code, const std::string& name);

private:
    I18n();
    void loadBuiltinEnglish();
    // Overlay translated strings onto the English base, but reject any value whose printf
    // format-specifier signature differs from the English source (keep English instead).
    // Many strings are used directly as printf/ImGui format strings, so a mismatched
    // translation (dropped/changed specifier) is undefined behavior — this prevents it.
    void overlayTranslations(const nlohmann::json& translations);

    std::string current_locale_ = "en";
    std::unordered_map<std::string, std::string> strings_;
    std::vector<std::pair<std::string, std::string>> available_languages_;
};

/**
 * @brief Convenience function for translation
 * @param key The string key
 * @return Translated string
 */
inline const char* tr(const char* key) {
    return I18n::instance().translate(key);
}

} // namespace util
} // namespace dragonx

// Convenience macro for translations
#define TR(key) dragonx::util::tr(key)
