#include "common/printing.h"
#include "common/jsonconfig.h"
#include "detection/locale/locale.h"
#include "modules/locale/locale.h"

bool ffPrintLocale(FFLocaleOptions* options) {
    FF_STRBUF_AUTO_DESTROY locale = ffStrbufCreate();

    const char* error = ffDetectLocale(&locale);
    if (error) {
        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Locale), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
        return false;
    }

    if (options->moduleArgs.outputFormat.length == 0) {
        ffPrintLogoAndKey(FF_MODULE_GET_DISPLAY_NAME(Locale), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
        ffStrbufPutTo(&locale, stdout);
    } else {
        FF_PRINT_FORMAT_CHECKED(FF_MODULE_GET_DISPLAY_NAME(Locale), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) { FF_ARG(locale, "result") }));
    }

    return true;
}

void ffParseLocaleJsonObject(FFLocaleOptions* options, yyjson_val* module) {
    yyjson_val *key, *val;
    size_t idx, max;
    yyjson_obj_foreach (module, idx, max, key, val) {
        if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
            continue;
        }

        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Locale), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
    }
}

void ffGenerateLocaleJsonConfig(FFLocaleOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
    ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
}

bool ffGenerateLocaleJsonResult([[maybe_unused]] FFLocaleOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
    FF_STRBUF_AUTO_DESTROY locale = ffStrbufCreate();

    const char* error = ffDetectLocale(&locale);

    if (error) {
        yyjson_mut_obj_add_str(doc, module, "error", error);
        return false;
    }

    if (locale.length == 0) {
        yyjson_mut_obj_add_str(doc, module, "error", "No locale found");
        return false;
    }

    yyjson_mut_obj_add_strbuf(doc, module, "result", &locale);

    return true;
}

void ffInitLocaleOptions(FFLocaleOptions* options) {
    ffOptionInitModuleArg(&options->moduleArgs, "");
}

void ffDestroyLocaleOptions(FFLocaleOptions* options) {
    ffOptionDestroyModuleArg(&options->moduleArgs);
}

FFModuleBaseInfo ffLocaleModuleInfo = {
    .name = "Locale",
    .description = "Print system locale name",
    .displayName = {
        .en = "Locale",
        .ar = "اللغة",
        .cs = "Národní prostředí",
        .de = "Sprachraum",
        .es = "Configuración regional",
        .fr = "Paramètres régionaux",
        .gl = "Configuración rexional",
        .he = "לוקאל",
        .id = "Lokal",
        .it = "Impostazioni locali",
        .ja = "ロケール",
        .ko = "로케일",
        .pl = "Ustawienia regionalne",
        .pt = "Configuração regional",
        .ru = "Локаль",
        .tr = "Yerel Ayar",
        .uk = "Локаль",
        .vi = "Bản địa",
        .zh_CN = "语言环境",
        .zh_TW = "語言環境",
    },
    .initOptions = (void*) ffInitLocaleOptions,
    .destroyOptions = (void*) ffDestroyLocaleOptions,
    .parseJsonObject = (void*) ffParseLocaleJsonObject,
    .printModule = (void*) ffPrintLocale,
    .generateJsonResult = (void*) ffGenerateLocaleJsonResult,
    .generateJsonConfig = (void*) ffGenerateLocaleJsonConfig,
    .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
        { "Locale code", "result" },
    })),
    .defaultOrder = 53,
};
