#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cyton_cmd_pub.h"
#include "cJSON.h"


#ifdef  ESP_8266
typedef struct stCytron_DevSeting {
    unsigned char aucRate;
    unsigned char aucStreamStat;
} CYTON_DEVSETING_T;

static CYTON_DEVSETING_T g_stCytron_DevSeting;
static int g_iOpenguiPort = -1;
static char g_szOpenguiIP[16] = {0};
static char g_szCmdBuf[64] = {0};


#define toNet32(A)   ((( (unsigned int)(A) & 0xff000000) >> 24) | \  
                            (( (unsigned int)(A) & 0x00ff0000) >> 8)  | \  
                            (( (unsigned int)(A) & 0x0000ff00) << 8)  | \  
                            (( (unsigned int)(A) & 0x000000ff) << 24))  

static int Ctron_atoi(char *szData, int iLen)
{
    //0,11:123456711
    unsigned char szTmp[4] = {0};

    if (iLen > 4) {
        return -1;
    }

    strncpy((char *)szTmp, (char *)szData, iLen);
    return atoi((char *)szTmp);
}

int Cyton_GetCurrentRate(int iRate)
{
    int iRet = 1000;

    switch(iRate) {
        case 0:
            iRet = 16000;
            break;
        case 1:
            iRet = 8000;
            break;
        case 2:
            iRet = 4000;
            break;
        case 3:
            iRet = 2000;
            break;
        case 4:
            iRet = 1000;
            break;
        case 5:
            iRet = 500;
        case 6:
            iRet = 250;
            break;
        default:
            break;
    }

    return iRet;
}

/*
A = 5MIN
S = 15MIN
F = 30MIN
G = 1HR
H = 2HR
J = 4HR
K = 12HR
L = 24HR
*/
static int ctron_GetRate(char *szData, int iLen)
{
    char *pszIndex = NULL;
    int iRate = 0;
    int iRateHz = 0;
    int iSDSec = -1;

    pszIndex = strstr((char *)szData, "~");
    if (NULL != pszIndex) {
        iRate = Ctron_atoi(pszIndex + 1, 1);
        iRateHz = Cyton_GetCurrentRate(iRate);
        return CytonCmd_Pkg(CYTON_CMD_HZ, iRateHz, 0);
    } else {
        switch (*szData) {
            case 'A':
                iSDSec = 60 * 5;
                break;
            case 'S':
                iSDSec = 60 * 15;
                break;
            case 'F':
                iSDSec = 60 * 30;
                break;
            case 'G':
                iSDSec = 60 * 60;
                break;
            case 'H':
                iSDSec = 60 * 60 *2;
                break;
            case 'J':
                iSDSec = 60 * 60 * 4;
                break;
            case 'K':
                iSDSec = 60 * 60 * 12;
                break;
            case 'L':
                iSDSec = 60 * 60 * 24;
                break;
        }

        if (iSDSec > 0) {
            return CytonCmd_Pkg(CYTON_CMD_SD, iSDSec, 0);
        }
    }

    return -1;    
}

int CytonCmd_PraseTcpJson(char *szData, int iLen)
{
    //{"connected":true,"delimiter":true,"ip":"192.168.4.2","output":"raw","port":6677,"latency":10000}
    cJSON *cjson_data;
    cJSON *cjson_str;
    int iRet = -1;
    char *pszIndex = 0;
    
    pszIndex = strstr((const char *)szData, "{");
    if (NULL == pszIndex) {
        printf("\r\ntcp json prase failed1111111111: %s\n", szData);
        return -1;
    }

    cjson_data = cJSON_Parse(pszIndex);
    if (cjson_data) {
	    cjson_str = cJSON_GetObjectItem(cjson_data,"ip");
	    if (cjson_str) {
            sprintf(g_szOpenguiIP, "%s", cjson_str->valuestring);
            printf("TCP Json g_szOpenguiIP %s\n", g_szOpenguiIP);
        }

        cjson_str = cJSON_GetObjectItem(cjson_data,"port");
	    if (cjson_str) {
            g_iOpenguiPort = cjson_str->valueint;
            printf("TCP Json g_iOpenguiPort %d\n", g_iOpenguiPort);
        }

        cjson_str = cJSON_GetObjectItem(cjson_data,"command");
	    if (cjson_str) {
            iRet = ctron_GetRate(cjson_str->valuestring, strlen(cjson_str->valuestring));
            if (iRet >= 0) {
                g_stCytron_DevSeting.aucRate = iRet;
                printf("TCP Json g_stCytron_DevSeting.aucRate %d\n", g_stCytron_DevSeting.aucRate);
            }
        }

        printf("Test111");

	    cJSON_Delete(cjson_data);
        printf("Test222");

        return 0;
    }

    return -1;
}

int CytonCmd_Pkg(unsigned int uiCmd, unsigned int uiParam1, unsigned int uiParam2)
{
    CYTON_CMD_INFO_T *pstInfo= (CYTON_CMD_INFO_T *)g_szCmdBuf;

    pstInfo->uiHead = toNet32(CYTON_CMD_HEAD);
    pstInfo->iType = toNet32(uiCmd);
    pstInfo->uiParam1 = toNet32(uiParam1);
    pstInfo->uiParam2 = toNet32(uiParam2);

    return sizeof(CYTON_CMD_INFO_T);
}

char *CytonCmd_GetServerIp()
{
    return g_szOpenguiIP;
}

int CytonCmd_GetServerPort()
{
    return g_iOpenguiPort;
}

char *CytonCmd_PkgData(int *iLen)
{
    *iLen = sizeof(CYTON_CMD_INFO_T);
    return g_szCmdBuf;
}
#endif