/*
* Copyright (C) 2012-2019 UCloud. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

#include "json_parser.h"

#include "lite-utils.h"
#include "uiot_import.h"
#include "uiot_defs.h"


char *LITE_json_value_of(char *key, char *src)
{
    char       *value = NULL;
    int         value_len = -1;
    char       *ret = NULL;

    char       *delim = NULL;
    char       *key_iter;
    char       *key_next;
    int         key_len;
    char       *src_iter;

    src_iter = src;
    key_iter = key;

    do {
        if ((delim = strchr(key_iter, '.')) != NULL) {
            key_len = delim - key_iter;
            key_next = HAL_Malloc(key_len + 1);
            strncpy(key_next, key_iter, key_len);
            key_next[key_len] = '\0';
            value = json_get_value_by_name(src_iter, strlen(src_iter), key_next, &value_len, 0);

            if (value == NULL) {
                HAL_Free(key_next);
                return NULL;
            }

            src_iter = value;
            key_iter = delim + 1;
            HAL_Free(key_next);
        }
    } while (delim);

    value = json_get_value_by_name(src_iter, strlen(src_iter), key_iter, &value_len, 0);
    if (NULL == value) {
        return NULL;
    }
    ret = HAL_Malloc((value_len + 1) * sizeof(char));
    if (NULL == ret) {
        return NULL;
    }
    HAL_Snprintf(ret, value_len + 1, "%s", value);
    return ret;
}

list_head_t *LITE_json_keys_of(char *src, char *prefix)
{
    static              LIST_HEAD(keylist);

    char    *pos = 0, *key = 0, *val = 0;
    int     klen = 0, vlen = 0, vtype = 0;

    if (src == NULL || prefix == NULL) {
        return NULL;
    }

    if (!strcmp("", prefix)) {
        INIT_LIST_HEAD(&keylist);
    }

    json_object_for_each_kv(src, pos, key, klen, val, vlen, vtype) {
        if (key && klen && val && vlen) {

            json_key_t     *entry = NULL;

            entry = HAL_Malloc(sizeof(json_key_t));
            memset(entry, 0, sizeof(json_key_t));
            entry->key = LITE_format_string("%s%.*s", prefix, klen, key);
            list_add_tail(&entry->list, &keylist);

            if (JSOBJECT == vtype) {
                char *iter_val = LITE_format_string("%.*s", vlen, val);
                char *iter_pre = LITE_format_string("%s%.*s.", prefix, klen, key);
                LITE_json_keys_of(iter_val, iter_pre);
                HAL_Free(iter_val);
                HAL_Free(iter_pre);
            }
        }
    }

    if (!strcmp("", prefix)) {
        json_key_t     *entry = NULL;

        entry = HAL_Malloc(sizeof(json_key_t));
        memset(entry, 0, sizeof(json_key_t));
        list_add_tail(&entry->list, &keylist);

        return &keylist;
    }

    return NULL;
}

void LITE_json_keys_release(list_head_t *keylist)
{
    json_key_t         *pos, *tmp;

    list_for_each_entry_safe(pos, tmp, keylist, list, json_key_t) {
        if (pos->key) {
            HAL_Free(pos->key);
        }
        list_del(&pos->list);
        HAL_Free(pos);
    }
}

int LITE_get_int32(int32_t *value, char *src) {
    return (sscanf(src, "%" SCNi32, value) == 1) ? SUCCESS_RET : FAILURE_RET;
}

int LITE_get_int16(int16_t *value, char *src) {
    return (sscanf(src, "%" SCNi16, value) == 1) ? SUCCESS_RET : FAILURE_RET;
}

/* NOTICE: scanning 8-bit types requires use of the hh specifier
 * which is only supported on newlib platforms that
 * are built with C99 I/O format support enabled.  If the flag in
 * newlib.h hasn't been set during configuration to indicate this, the 8-bit
 * scanning format macros are disabled here as they result in undefined
 * behaviour which can include memory overwrite.  Overriding the flag after the
 * library has been built is not recommended as it will expose the underlying
 * undefined behaviour.so we use int16 and uint16 transfer to int8 and uint8
 */
int LITE_get_int8(int8_t *value, char *src) {
    int16_t temp = 0;
    if(1 != sscanf(src, "%" SCNi16, &temp))
    {
        return FAILURE_RET;
    }
    *value = (int8_t)temp;
    return SUCCESS_RET;
}

int LITE_get_uint32(uint32_t *value, char *src) {
    return (sscanf(src, "%" SCNu32, value) == 1) ? SUCCESS_RET : FAILURE_RET;
}

int LITE_get_uint16(uint16_t *value, char *src) {
    return (sscanf(src, "%" SCNu16, value) == 1) ? SUCCESS_RET : FAILURE_RET;
}

int LITE_get_uint8(uint8_t *value, char *src) {
    uint16_t temp = 0;
    if(1 != sscanf(src, "%" SCNu16, &temp))
    {
        return FAILURE_RET;
    }
    *value = (uint8_t)temp;
    return SUCCESS_RET;
}

int LITE_get_float(float *value, char *src) {
    return (sscanf(src, "%f", value) == 1) ? SUCCESS_RET : FAILURE_RET;
}

int LITE_get_double(double *value, char *src) {
    return (sscanf(src, "%lf", value) == 1) ? SUCCESS_RET : FAILURE_RET;
}

int LITE_get_boolean(bool *value, char *src) {
    if (!strcmp(src, "0")) {
        *value = false;
    }
    else {
        *value = true;
    }

    return SUCCESS_RET;
}

