#include "bmser_ipc.h"
#include "toolkit.h"
#include <stdlib.h>
#include <stdio.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
#include <string.h>
#include <pthread.h>
#include "event_process.h"
#include "dtc_export.h"
#include "project_userdef.h"
#include "bmser_project_para.h"
#include "bmser_dev.h"
#include "event_query.h"
struct ipc_client *client;

struct ipc_topic g_subscribe_topic[] =
{
    {"ipc/+/+/+/event/Set/dtc", 1},
    {"ipc/+/+/+/+/Get/VersionInfoList", 0},
    {"ipc/+/+/+/data/Get/ImmediateAlarm",0},
    {"ipc/+/+/+/data/Get/DtcHistory",0},
    {"ipc/+/+/+/data/Get/HistoryAlarm",0},
    {"ipc/+/+/+/data/Get/AlarmCount",0},
    {"ipc/+/+/+/data/Set/CleanAlarm",1},
    {"ipc/+/+/+/+/Set/language" ,1},
    {"ipc/+/bms/+/upgrade/SetResp/binaryFlow" ,1},
    {"ipc/+/drs/+/data/Set/ExportSegmentLog",1},
    {"ipc/+/emg/+/data/SetResp/NtpCfg", 0}
};

static int32_t communication_resp_version(struct ipc_client *c,struct ipc_message *m)
{
    #define GET_APP_VERSION_IDENTIFIER 	         "VersionInfoList"
    if (NULL == m) {
        return -1;
    }

    int ret = 0;
    char sn[64] = {0}, topic[256] = {0}, payload[512] = {0};
    struct ipc_topic topic_t = {0};
    struct ipc_payload payload_t = {0};
    ret = bmser_dev_get_board_sn(sn);
    if (0 != ret) {
        return -1;
    }
    snprintf(payload, sizeof(payload), 
    "{\"identifier\":\"VersionInfoList\",\"tags\":{\"frames\":[{\"regs\":[{\"key\":\"alarm_common\",\"value\":\"%d.%d.%d.%d\"}]}]},\"data_type\":1}", 
        ALARM_COMMON_PROJECT_NUM,ALARM_COMMON_MAIN_VERSION,ALARM_COMMON_SUB_VERSION,ALARM_COMMON_CORRECT_VERSION);
    snprintf(topic, sizeof(topic), "ipc/%s/bms/00000000/data/GetResp/%s", sn, GET_APP_VERSION_IDENTIFIER);
    topic_t.name = topic;
    topic_t.qos = 0;
    payload_t.buf = payload;
    payload_t.len = strlen(payload);
    bmser_ipc_publish(c, &topic_t, &payload_t, 0);
    return ret;
}

static void on_message(struct ipc_client *c, void *obj, struct ipc_message *m)
{
    //TODO:现在的消息处理都在MQTT消息接收回调里面了，后续需要优化
    if(strstr(m->t.name,"event/Set/dtc")){
        eventprocess_messaging(m);
    }
    else if(strstr(m->t.name,"Get/VersionInfoList")){
        communication_resp_version(c,m);
    }
    else if(strstr(m->t.name,"Get/ImmediateAlarm") || strstr(m->t.name,"Get/HistoryAlarm") || strstr(m->t.name,"Get/DtcHistory")){
        event_query_alarm(m);
    }
    else if(strstr(m->t.name,"Get/AlarmCount")){
        event_query_alarm_count(m);
    }
    else if(strstr(m->t.name,"Set/CleanAlarm")){
        event_query_clean_alarm(m);
    }
    else if(strstr(m->t.name,"Set/language")){
        eventprocess_set_language(m);
    }
    else if(strstr(m->t.name,"SetResp/binaryFlow")){
        DTC_Export_flow_data_process(m);
    }
    else if(strstr(m->t.name,"SetResp/NtpCfg")){
        // 更新时区偏移秒数
        bmser_jsonUpdateTimeZoneOffset();
    }
}

static void on_connect(struct ipc_client * c, void *obj, int rc)
{
    int i = 0;

    for (i = 0; i < SIZE_OF_ARRAY(g_subscribe_topic); i++){
        bmser_ipc_subscribe(c, &g_subscribe_topic[i],NULL);
    }
}

int communication_publish(struct ipc_topic *t, struct ipc_payload *p)
{
    return bmser_ipc_publish(client, t, p, false);  
}


int communication_init()
{
    int ret = 0;
    
    struct ipc_event_ops cb =
    {
        .on_connect = on_connect,
        .on_disconnect = NULL,
        .on_publish = NULL,
        .on_subscribe = NULL,
        .on_unsubscribe = NULL,
        .on_message = on_message,
    };

    IPC_CLIENT_INFO_FAST(client_info, NULL, "127.0.0.1", 1883, NULL, &cb);
    if(IPC_ERR_SUCCESS != bmser_ipc_create(&client_info, &client)){
        ret = -1;
    }
    return ret;
}

void communication_deinit()
{
    bmser_ipc_destroy(client);
}