//
// Created by #Suyghur, on 2021/06/21.
//

#include <toolkit.h>
#include <logger.h>
#include "include/params_kit.h"

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

std::string ParamsKit::GetGameCode(JNIEnv *env, jobject context) {
    return GetValue4Properties(env, context, YYXX_GAME_CODE);
}

std::string ParamsKit::GetCommGcpCode(JNIEnv *env, jobject context) {
    return GetValue4Properties(env, context, YYXX_COMM_GCP_CODE);
}

std::string ParamsKit::GetCommGameCode(JNIEnv *env, jobject context) {
    return GetValue4Properties(env, context, YYXX_COMM_GAME_CODE);
}

std::string ParamsKit::GetChannelId(JNIEnv *env, jobject context) {
    return GetValue4Properties(env, context, YYXX_CHANNEL_ID);
}

std::string ParamsKit::GetChannelName(JNIEnv *env, const std::string &id) {
    jclass clz = env->FindClass("cn/yyxx/eyuancomm/impl/SdkGenerator");
    if (clz == nullptr) {
        Logger::E(env, "GetChannelName clz is null");
        return "";
    }
    const char *method_name = "getSdkName";
    const char *sig = "(Ljava/lang/String;)Ljava/lang/String;";
    jmethodID mid = env->GetStaticMethodID(clz, method_name, sig);
    jstring channel_id = env->NewStringUTF(id.c_str());
    auto result = (jstring) env->CallStaticObjectMethod(clz, mid, channel_id);
    return ToolKit::JString2String(env, result);
}

bool ParamsKit::GetDebugModel(JNIEnv *env, jobject context) {
    jclass clz = env->FindClass("cn/yyxx/support/hawkeye/OwnDebugUtils");
    if (clz == nullptr) {
        Logger::E(env, "OwnDebugUtils clz is null");
        return false;
    }
    const char *method_name = "isOwnDebug";
    const char *sig = "(Landroid/content/Context;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Z";
    jmethodID mid = env->GetStaticMethodID(clz, method_name, sig);
    jstring file_name = env->NewStringUTF(CONFIG_FILE);
    jstring folder_name = env->NewStringUTF("yyxx_game");
    jstring key_name = env->NewStringUTF(YYXX_OWN_DEBUG);
    jboolean result = env->CallStaticBooleanMethod(clz, mid, context, file_name, folder_name, key_name);
    return result == JNI_TRUE;
}

std::string ParamsKit::GetValue4Properties(JNIEnv *env, jobject context, const std::string &key) {
    jclass clz = env->FindClass("cn/yyxx/support/PropertiesUtils");
    if (clz == nullptr) {
        Logger::E(env, "PropertiesUtils clz is null");
        return "";
    }
    const char *method_name = "getValue4Properties";
    const char *sig = "(Landroid/content/Context;Ljava/lang/String;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 folder_name = env->NewStringUTF("yyxx_game");
    jstring key_name = env->NewStringUTF(key.c_str());
    auto result = (jstring) env->CallStaticObjectMethod(clz, mid, context, file_name, folder_name, key_name);
    return ToolKit::JString2String(env, result);
}

