#ifndef XYSDK_COMMON_H
#define XYSDK_COMMON_H

/*
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
*/
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
// #include "asm/cpu.h"
// #include "app_config.h"

#include "xysdk_log.h"
#include "xysdk_platform.h"
#include "xysdk_cJSON.h"
#include "xysdk_queue.h"
#include "xysdk_list.h"
#include "xysdk_jl_externfunction.h"
#include  "system/includes.h"
#include  "app_config.h"

#define PRI_U8        "%d"
#define PRI_S8       "%d"
#define PRI_U16       "%d"
#define PRI_S16       "%d"
#define PRI_U32       "%ld"
#define PRI_S32      "%ld"
#define PRI_U64       "%lld"
#define PRI_S64       "%lld"
#define PRI_STR       "%s"
#define PRI_nSTR       "%*.s"
#define PRI_ADDR       "0x%x"
#define PRI_FLOAT       "%.1f"
#define PRI_TIME_2_TS       "%d000"

#define XY_MAX_URL_LEN          (256)
#define XY_MAX_PLAY_URL_LEN     (512)
#define XY_MAX_ID_LEN           (16)
#define XY_MAX_TOKEN_LEN        (48)
#define XY_MAX_NAME_LEN      (128)
#define XY_MAX_USER_DATA_DB_LEN      (128*6)

#define XY_MAX_STR_LEN_8       (8)
#define XY_MAX_STR_LEN_16       (16)
#define XY_MAX_STR_LEN_32       (32)
#define XY_MAX_STR_LEN_64       (64)
#define XY_MAX_STR_LEN_128       (128)
#define XY_MAX_STR_LEN_256       (256)
#define XY_MAX_STR_LEN_512       (512)
#define XY_MAX_STR_LEN_1024       (1024)
#define XY_MAX_STR_LEN_2048       (2048)
#define XY_MAX_STR_LEN_4096       (4096)

#define XY_ALBUM_NUM_PER_PAGE   (10) //请求专辑列表时每页获取的专辑个数，最大设置为20
#define XY_ALBUMLIST_MAX_NUM   (100) //专辑列表页最大专辑个数限制
#define XY_ALBUMLIST_MAX_PAGE   (XY_ALBUMLIST_MAX_NUM/XY_ALBUM_NUM_PER_PAGE) //专辑列表最大页数
#define XY_PLAY_DATA_UPLOAD_NUM_MAX (1)//播放数据上传的最大个数
#define XY_REFRESH_TOKEN_TIMEOUT  (3600*24*30L)
#define XY_ACCESS_TOKEN_TIMEOUT_DEFAULT  (3600*2)
#define XY_HOMEPAGE_BANNER_NUM_MAX   (5) //首页banner最大个数
#define XY_HOMEPAGE_LISTEN_NUM_MAX   (10)//首页听单最大个数

#define XY_HTTP_SIG_ERR_CODE        (101)
#define XY_HTTP_ACCESS_TOKEN_ERR_CODE        (206)
#define XY_HTTP_REFRESH_TOKEN_ERR_CODE        (212)

//#define XYSDK_SUPPORT_BANNER          //是否支持banner
//#define XYSDK_SUPPORT_IMAGE_DOWNLOAD          //是否图片下载

typedef enum {
    XY_HTTP_LOG_PAY_INFO_ERR = -1016, // 登录或支付信息不完整
    XY_HTTP_SIG_ERR = -1015, // 签名错误
    XY_HTTP_PLEASE_PAY = -1014, // 未支付，请支付
    XY_HTTP_LOGIN_INVALID = -1013, // 未登录，请登录
    XY_HTTP_BOUGHT_IS_NULL = -1012, //已购列表为空
    XY_HTTP_HISTORY_IS_NULL = -1011, //历史记录为空
    XY_HTTP_SUBCRIBE_IS_NULL = -1010, //订阅列表为空
    XY_HTTP_TOKEN_INVALID = -1009, // token失效
    XY_HTTP_BREAK = -1008,    // HTTP被打断
    XY_HTTP_PARAM_ERROR = -1007,    // 接口参数错误
    XY_HTTP_ALBUM_NOT_EXIST = -1006, // 专辑不存在、下架
    XY_HTTP_SERVER_DATA_ENPTY = -1005, // 服务器返回数据为空，用于判断历史、已购、订阅列表是否为空
    XY_HTTP_NET_ERR = -1004,  // 网络异常
    XY_HTTP_DATA_PRASE_ERR = -1003,  // 返回数据解析出错
    XY_HTTP_PARAM_NULL = -1002, // 接口参数为空
    XY_HTTP_MALLOC_ERR = -1001,  // 内存申请出错
    XY_HTTP_UNKNOW_ERR = -1000,        //未知异常 

    XY_HTTP_OK = 0,
} http_ret_value;


//用户信息结构体
typedef struct { 
    uint32_t user_id;   //用户id
    uint32_t access_token_update_time;   //access_token更新时间戳
    uint32_t refresh_token_update_time; //refresh_token更新时间戳
    uint16_t expires_in;                //access_token有效时间
    char nick_name[XY_MAX_NAME_LEN];   //用户昵称
    #ifdef XYSDK_SUPPORT_IMAGE_DOWNLOAD
    char avatar_url[XY_MAX_URL_LEN];   //用户头像
    #endif
    char access_token[XY_MAX_TOKEN_LEN];
    char refresh_token[XY_MAX_TOKEN_LEN];
} xy_user_info_model;

//用户信息结构体
typedef struct { 
    bool is_vip;                            //是否是vip 用户
    uint32_t vip_expires_in;                //vip到期时间,单位s
    char vip_expires_in_str[XY_MAX_STR_LEN_16]; //vip到期时间 xxxx-xx-xx
} xy_user_vip_info;

//临时token结构体
typedef struct {
    uint32_t temp_token_update_time;   //temp_token更新时间戳
    uint16_t expires_in;  // token有效时间,单位s
    char temp_token[XY_MAX_TOKEN_LEN];//临时token
} xy_temp_token_model;

void *xy_common_malloc(uint32_t size);

void* xy_common_calloc(uint32_t nblock, uint32_t size);

void* xy_common_realloc(void* pt, uint32_t size);

void xy_common_free(void **src);

void xy_set_string_ptr(char **destination, const char *source);

//获取ms时间戳字符串
char *xy_api_get_timestamp_ms_str(void);

//获取用户uid
uint32_t xy_common_get_uid(void);

//设置临时token，内部使用
int xy_common_save_temp_token(xy_temp_token_model *temp_token);

//获取access-token，内部使用
char *xy_common_get_access_token(void);

//获取refresh_token, 内部使用
char *xy_common_get_refresh_token(void);

//获取用户是否登录
bool xy_common_user_is_login(void);

//获取用户是否是vip
bool xy_common_user_is_vip(void);

//设置用户vip信息
int xy_common_set_user_vip_info(xy_user_vip_info *vip_info);

//获取用户vip信息
xy_user_vip_info *xy_common_get_user_vip_info(bool need_update);

//设置用户信息
int xy_common_set_user_info(xy_user_info_model *user_info);

//清除用户信息
int xy_common_clear_user_info(void);

//获取用户信息
xy_user_info_model *xy_common_get_user_info(void);



#include "xysdk_platform_http.h"
#include "xysdk_http_api.h"
#include "xysdk_player_api.h"


#endif		
