#include "system/includes.h"
#include "os/os_api.h"
#include "http/http_cli.h"
#include "fs/fs.h"
#include <stdlib.h>
#include "storage_device.h"
#include "net_update.h"
#include "app_config.h"
#include "wifi/wifi_connect.h"
#include "http/http_cli.h"
#include "json_c/json_object.h"
#include "json_c/json_tokener.h"
#include "syscfg_id.h"
#include "server/ai_server.h"


#define OTA_VERSION_URL "https://api.map.baidu.com/location/ip?coor=bd09ll&ak=8fce84abf30ba57c380ba4ac8f620a45"

#define DEFAULT_RECV_BUF_SIZE   (4*1024)   //数据缓存BUF大小
#define MAX_RECONNECT_CNT 10               //最大重连次数

#define DEFAULT_RECV_BUF_SIZE   (4*1024)   //数据缓存BUF大小
#define MAX_RECONNECT_CNT 10               //最大重连次数

#define CITY_NAME_LEN 24
static char city_name[CITY_NAME_LEN] = {0};

static int get_location_info()
{
    int ret = 0;
    int err = -1;
    http_body_obj http_body_buf;
    json_object *parse = NULL, *data = NULL;

    httpcli_ctx *ctx = (httpcli_ctx *)calloc(1, sizeof(httpcli_ctx));
    if (!ctx) {
        printf("calloc faile\n");
        return err;
    }

    memset(&http_body_buf, 0x0, sizeof(http_body_obj));

    http_body_buf.recv_len = 0;
    http_body_buf.buf_len = HTTP_RECV_BUF_SIZE;
    http_body_buf.buf_count = 1;
    http_body_buf.p = (char *) malloc(http_body_buf.buf_len * http_body_buf.buf_count);
    if (!http_body_buf.p) {
        free(ctx);
        return err;
    }

    ctx->url = OTA_VERSION_URL;
    ctx->timeout_millsec = 5000;
    ctx->priv = &http_body_buf;
    ctx->connection = "close";

    ret = httpcli_get(ctx);
    if (ret != HERROR_OK) {
        printf("get_latest_version_info : httpcli_get fail\n");
    } else {
        if (http_body_buf.recv_len > 0) {
            /** 对从服务器收到的json格式的版本信息进行解析，获取其版本号，版本描述信息和升级固件的URL信息*/
            parse = json_tokener_parse(http_body_buf.p);

            if (0 == json_object_get_int(json_object_object_get(parse, "status"))) {
                data = json_object_object_get(parse, "content");
                if (data != NULL) {
                    /** 获取版本信息*/
                    char *address = json_object_get_string(json_object_object_get(data, "address"));
                    snprintf(city_name, sizeof(city_name), "%s", address);

                    puts("\n*************************************************************\n");
                    printf("city_name url:%s\n", city_name);
                    puts("\n*************************************************************\n");

                    /** 获取成功*/
                    err = 0;
                }
            }
        }
    }
    httpcli_close(ctx);

    if (http_body_buf.p) {
        free(http_body_buf.p);
        http_body_buf.p = NULL;
    }

    if (ctx) {
        free(ctx);
        ctx = NULL;
    }

    if (parse) {
        json_object_put(parse);
        parse = NULL;
    }

    return err;
}

// void start_get_location_info_task(void *priv)
// {
//     if (thread_fork("get_location_info_start", 10, 512, 0, NULL, http_upgrade_start, NULL) != OS_NO_ERR) {
//         printf("thread fork fail\n");
//         ws_interact_ota_event_notify(AI_SERVER_EVENT_UPGRADE_FAIL, 0);
//     }
// }

//应用程序入口,需要运行在STA模式下
void c_main(void *priv)
{
    if (thread_fork("get_location_info_start", 10, 4 * 1024, 0, NULL, get_location_info, NULL) != OS_NO_ERR) {
        printf("thread fork fail\n");
    }
}

late_initcall(c_main);

