//
// Created by #Suyghur, on 2021/10/15.
//

#include "include/tool_kit.h"
#include "include/yylog.h"
#include "include/params_kit.h"

std::string ParamsKit::GetGcpCode(JNIEnv *env, jobject context) {
    return GetCnfValue(env, context, GCP_CODE);
}

std::string ParamsKit::GetChannelTag(JNIEnv *env, jobject context) {
    return GetCnfValue(env, context, CHANNEL_TAG);
}

std::string ParamsKit::GetCnfValue(JNIEnv *env, jobject context, const std::string &key) {
    jclass clz = env->FindClass("com/yyrh/utils/FileUtils");
    if (clz == nullptr) {
        LOGE("FileUtils clz is null");
        return "";
    }
    const char *method_name = "readAssetsFileByKey";
    const char *sig = "(Landroid/content/Context;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;";
    jmethodID mid = env->GetStaticMethodID(clz, method_name, sig);
    jstring file_name = env->NewStringUTF(CONFIG_FILE);
    jstring key_name = env->NewStringUTF(key.c_str());
    auto result = (jstring) env->CallStaticObjectMethod(clz, mid, context, file_name, key_name);
    env->DeleteLocalRef(clz);
    env->DeleteLocalRef(file_name);
    env->DeleteLocalRef(key_name);

    return ToolKit::JString2String(env, result);
}
