/*---------------------------------------------------------------------------*/
/* appclient_config.cpp                                                      */
/* copyright (c) innovaphone 2018                                            */
/*                                                                           */
/*---------------------------------------------------------------------------*/

#include "platform/platform.h"
#include "common/ilib/json.h"
#include "client/config/client_config.h"
#include "appclient_config.h"
#ifdef __ANDROID__
#include <jni.h>
#include "client/android/android_jni/android_jni.h"

JNIEnv *get_jni_env(void);

extern jobject phone_android_context;
#endif

/*---------------------------------------------------------------------------*/

IClientConfigLocalUlong64::IClientConfigLocalUlong64(IClientConfigItems * items, UClientConfigLocalUlong64 * user, const char * name, ulong64 init) :
    IClientConfigAdminItem(items, name),
    user(user),
    value(init)
{
    items->ItemAdded(this);
}

void IClientConfigLocalUlong64::Set(ulong64 newValue)
{
    if (!force) {
        value = newValue;
        set = true;
        items->ItemSet(this);
        user->Apply(this);
    }
}

void IClientConfigLocalUlong64::ToJson(class json_io & json, word baseObject, char * & tmp)
{
    int l = _sprintf(tmp, "%016llx", (unsigned long long)value);
    json.add_string(baseObject, "value", tmp);
    tmp += l + 1;
}

void IClientConfigLocalUlong64::FromJson(class json_io & json, word baseObject)
{
    const char * s;
    ulong64 u;
    unsigned i;

#ifdef __ANDROID__
    JNIEnv *phone_android_jni_env;
    phone_android_jni_env = get_jni_env();
    if (!set && Context_checkSelfPermission_ID && (Context_checkSelfPermission(phone_android_context, Manifest_permission_READ_LOGS) == PackageManager_PERMISSION_GRANTED)) {
        force = false;
    }
    else
#endif
    {
        u = 0;
        s = json.get_string(baseObject, "value");
        if (s) {
            i = ((s[0] == '0') && ((s[1] == 'x') || (s[1] == 'X'))) ? 2 : 0;
            while (isxdigit(s[i])) {
                u = (u << 4) | (isdigit(s[i]) ? s[i] - '0' : tolower(s[i]) - 'a' + 10);
                i++;
            }
        }
        value = u;
    }
}

/*---------------------------------------------------------------------------*/

IClientConfigAdminHotkey::IClientConfigAdminHotkey(IClientConfigItems * items, UClientConfigAdminHotkey * user, const char * name, const char * init) :
    IClientConfigAdminItem(items, name),
    user(user),
    value(_strdup(init)),
    appLink(NULL)
{
    items->ItemAdded(this);
}

IClientConfigAdminHotkey::~IClientConfigAdminHotkey()
{
    if (value) free(value);
    if (appLink) free(appLink);
}

void IClientConfigAdminHotkey::Set(const char * newValue, const char * newAppLink)
{
    if (!force) {
        if (value) free(value);
        value = _strdup(newValue);
        if (appLink) free(appLink);
        appLink = _strdup(newAppLink);
        set = true;
        items->ItemSet(this);
        user->Apply(this);
    }
}

void IClientConfigAdminHotkey::ToJson(class json_io & json, word baseObject, char * & tmp)
{
    json.add_string(baseObject, "value", value);
    json.add_string(baseObject, "appLink", appLink);
}

void IClientConfigAdminHotkey::FromJson(class json_io & json, word baseObject)
{
    if (value) free(value);
    value = _strdup(json.get_string(baseObject, "value"));
    if (appLink) free(appLink);
    appLink = _strdup(json.get_string(baseObject, "appLink"));
}

/*---------------------------------------------------------------------------*/

static ClientConfigIdNameItem IdNameDisplayOnIfCharging[] =
{
    { DISPLAY_ON_IF_CHARGING_NO,     "No" },
    { DISPLAY_ON_IF_CHARGING_DIMMED, "Dimmed" },
    { DISPLAY_ON_IF_CHARGING_BRIGHT, "Bright" },
    { DISPLAY_ON_IF_CHARGING_NO,     NULL },
};

#ifdef __ANDROID__

static ClientConfigIdNameItem IdNameAudioApi[] =
{
    { AUDIO_API_DEFAULT,            "Default" },
    { AUDIO_API_JAVA,               "Java" },
    { AUDIO_API_OPENSLES,           "OpenSLES" },
    { AUDIO_API_RTP,                "RTP" },
    { AUDIO_API_SLES_RTP_NOT_COMM,  "NotComm" },
    { AUDIO_API_JAVA_NOT_COMM,      "JavaNotComm" },
    { AUDIO_API_OPENSLES_NOT_COMM,  "OpenSLESNotComm" },
    { AUDIO_API_JAVA_VOICE_CALL,    "JavaVoiceCall" },
    { AUDIO_API_JAVA_MIC_BOOST,     "JavaMicBoost" },
    { AUDIO_API_OPENSLES_MIC_BOOST, "OpenSLESMicBoost" },
    { AUDIO_API_OPENSLES_EC,        "OpenSLESEC" },
    { AUDIO_API_JAVA_EC,            "JavaEC" },
    { AUDIO_API_DEFAULT,            NULL },
};

static ClientConfigIdNameItem IdNameBluetoothHeadsetMode[] =
{
    { BLUETOOTH_HEADSET_MODE_DEFAULT,  "Default" },
    { BLUETOOTH_HEADSET_MODE_TOGGLING, "Toggling" },
    { BLUETOOTH_HEADSET_MODE_DEFAULT,  NULL },
};

#endif

ClientConfig::ClientConfig(UClientConfigItems * itemsUser, ClientConfigUser * user) :
    items(itemsUser),
    logFlags(&items, user, "logFlags"),
    noHexdump(&items, user, "noHexdump"),
    orientationAdaptationOff(&items, user, "orientationAdaptationOff"),
    displayOnIfCharging(&items, user, "displayOnIfCharging", IdNameDisplayOnIfCharging),
    noPushAutostart(&items, user, "noPushAutostart"),
#ifdef __ANDROID__
    audioApi(&items, user, "audioApi", IdNameAudioApi),
    bluetoothHeadsetMode(&items, user, "bluetoothHeadsetMode", IdNameBluetoothHeadsetMode),
    phoneAccountEnabled(&items, user, "phoneAccountEnabled"),
    avoidNativeCallScreen(&items, user, "avoidNativeCallScreen"),
#endif
#ifdef _IOS_
    ringInHeadset(&items, user, "ringInHeadset"),
    activeAfterClose(&items, user, "activeAfterClose"),
    activeAfterCloseAsked(&items, user, "activeAfterCloseAsked"),
    addressBookInaccessibleWarned(&items, user, "addressBookInaccessibleWarned"),
#endif
    pushNotifications(&items, user, "pushNotifications"),
    pushPassword(&items, user, "pushPassword"),
    downloadedBuild(&items, user, "downloadedBuild"),
    noVpnAddresses(&items, user, "noVpnAddresses"),
    showDesktopNotifications(&items, user, "showDesktopNotifications"),
    autostart(&items, user, "autostart"),
    docking(&items, user, "docking"),
    autoOffline(&items, user, "offline", 300000),
    hotkeyDial(&items, user, "hotkeyDial", "--none--"),
    hotkeyAccept(&items, user, "hotkeyAccept", "--none--"),
    hotkeyReject(&items, user, "hotkeyReject", "--none--"),
    hotkeyAppLink0(&items, user, "hotkeyAppLink0", "--none--"),
    hotkeyAppLink1(&items, user, "hotkeyAppLink1", "--none--"),
    hotkeyAppLink2(&items, user, "hotkeyAppLink2", "--none--"),
    recordingUrl(&items, user, "recordingUrl"),
    isRecordingByDefaultOn(&items, user, "isRecordingByDefaultOn"),
    recordExternalOnly(&items, user, "recordExternalOnly"),
    hideFromNativeCallList(&items, user, "hideFromNativeCallList"),
    currentUserSip(&items, user, "currentUserSip"),
    localStorage(&items, user, "localStorage")
{
}

/*---------------------------------------------------------------------------*/
