#ifndef MODULE_WEB_H
#define MODULE_WEB_H

/**
 * @brief 与web通信的接口类，以及json工具类：tool_json
 * @author 戴月腾
 * @date 2018-08-25
 */

#include <memory>
#include <string>
#include"module_const.h"

#include "rapidjson/prettywriter.h"
#include "rapidjson/document.h"

#include "websocket/wsClientMgr.h"

//#define JSON_ROOT_KEY_CMD "cmd"
//#define JSON_ROOT_KEY_VERSION "version"
//#define INTERFACE_VERSION "1.0.0.2"
//#define JSON_ROOT_KEY_DATA "data"

////呼叫命令
//#define JSON_CMD_VALUE_CALL_CARD_REQUEST "call_card_req"
//#define JSON_CMD_VALUE_CALL_CARD_RESPONSE "call_card_resp"
//#define JSON_CMD_VALUE_CALL_CARD_CANCEL_REQUEST "call_card_cancel_req"
//#define JSON_CMD_VALUE_CALL_CARD_CANCEL_RESPONSE "call_card_cancel_resp"
//#define JSON_CMD_VALUE_CALL_CARD_LIST "callcardlist"

//#define JSON_KEY_CALL_CARD_CALL_TYPE "call_type_id"
//#define JSON_KEY_CALL_CARD_CALL_TIME_OUT "call_time_out"
//#define JSON_KEY_CALL_CARD_CALL_TIME "call_time"
//#define JSON_KEY_CALL_CARD_CALL_LEVEL "call_level_id"
//#define JSON_KEY_CALL_CARD_USER_NAME "user_name"
//#define JSON_KEY_CALL_CARD_STATIONS "stations"
//#define JSON_KEY_CALL_CARD_STATION_ID "stationid"
//#define JSON_KEY_CALL_CARD_CARDS "cards"
//#define JSON_KEY_CALL_CARD_CARD_ID "cardid"
//#define JSON_KEY_CALL_CARD_CARD_TYPE_ID "cardtype"

//#define JSON_KEY_CALL_LIGHT_TASK_ID "task_id"
//#define JSON_KEY_CALL_LIGHT_OBJ_TYPE "obj_type"
//#define JSON_KEY_CALL_LIGHT_CTRL_TYPE "ctrl_type"
//#define JSON_KEY_CALL_LIGHT_LIGHT_STATE "light_state"
//#define JSON_KEY_CALL_LIGHT_USER_ID "user_id"
//#define JSON_KEY_CALL_LIGHT_LIGHTS "lights"
//#define JSON_KEY_CALL_LIGHT_ID "id"

//#define JSON_KEY_ID "id"
//#define JSON_KEY_OP_TYPE "op_type"
//#define JSON_KEY_OP_TYPE_INSERT "INSERT"
//#define JSON_KEY_OP_TYPE_UPDATE "UPDATE"
//#define JSON_KEY_OP_TYPE_DELETE "DELETE"

//// 采集端接收接口
//#define JSON_CMD_VALUE_CLEAR_CARD "clear_card"
//#define JSON_CMD_VALUE_DEAL_HELP "helpme_done"   //deal_help
//#define JSON_CMD_VALUE_DEAL_HELP_RESPONSE "helpme_done_rsp"   //deal_help
//#define JSON_CMD_VALUE_REQUEST_ALL_POSTION "request_all_postion"
//#define JSON_CMD_VALUE_REQUEST_ALL_DATA "req_all_data"
//#define JSON_CMD_VALUE_RESPONSE_ALL_DATA "resp_all_data"
//#define JSON_CMD_VALUE_CALL_CARD_START "call_card_start"
//#define JSON_CMD_VALUE_CALL_CARD_CANCEL "call_card_cancel"
//#define  JSON_CMD_VALUE_LEADER_ARRANGE "leader_arrange"
////呼叫命令
//#define JSON_CMD_VALUE_CALL_CARD_REQUEST "call_card_req"
//#define JSON_CMD_VALUE_CALL_CARD_RESPONSE "call_card_resp"
//#define JSON_CMD_VALUE_CALL_CARD_CANCEL_REQUEST "call_card_cancel_req"
//#define JSON_CMD_VALUE_CALL_CARD_CANCEL_RESPONSE "call_card_cancel_resp"
//#define JSON_CMD_VALUE_CALL_CARD_LIST "callcardlist"

//#define JSON_CMD_VALUE_META_DATA_CHANGED "meta_data_changed"
//#define JSON_CMD_VALUE_LIGHTS_CTRL_REQUEST "light_control"
//#define JSON_CMD_VALUE_LIGHTS_CTRL_RESPONSE "lights_ctrl_res"
//#define JSON_CMD_VALUE_HAND_UP_RESPONSE "deal_hand_up_res"

//// id, name, type, map_id
//#define JSON_KEY_AREA_ID "area_id" // "区域编号"
//#define JSON_KEY_AREA_IS_SPECIAL "is_special_area" // "区域名称"
//// down_mine,up_mine
//#define JSON_CMD_VALUE_DOWN_MINE "down_mine"
//#define JSON_CMD_VALUE_UP_MINE "up_mine"
//#define JSON_CMD_VALUE_SPECIAL_AREA_UP_MINE "special_area_up_mine"
//#define JSON_CMD_VALUE_DEV_STATE "device_state"
//#define JSON_CMD_VALUE_EVENT "event"
//#define JSON_CMD_VALUE_CALL "CALL"
//#define JSON_CMD_VALUE_PUSH "PUSH"
//#define JSON_CMD_VALUE_USER "USER"
//#define JSON_CMD_DRIVING_FACE_STATE "tunneller_stat"


//#define JSON_ROOT_KEY_TOTAL "total"
//#define JSON_ROOT_KEY_STATISTIC_VEHICLE_DATA "v"
//#define JSON_ROOT_KEY_STATISTIC_STAFF_DATA "s"
//#define JSON_ROOT_KEY_STATISTIC_STAT "stat"
//#define JSON_ROOT_KEY_STATISTIC_DETAIL "detail"
//#define JSON_ROOT_KEY_STATISTIC_GLOBAL "glbl"
//#define JSON_ROOT_KEY_STATISTIC_DEPT "dept"
//#define JSON_ROOT_KEY_STATISTIC_SUM "sum"
//#define JSON_ROOT_KEY_STATISTIC_AREA "area"
//#define JSON_ROOT_KEY_STATISTIC_LEVEL "occupation_level"

//// 告警事件
//#define JSON_KEY_EVENT_EVENT_ID "event_id"
//#define JSON_KEY_EVENT_STATUS "status"
//#define JSON_KEY_EVENT_TYPE_ID "type_id"
//#define JSON_KEY_EVENT_OBJ_TYPE_ID "obj_type_id"
//#define JSON_KEY_EVENT_OBJ_ID "obj_id"
//#define JSON_KEY_EVENT_MAP_ID "map_id"
//#define JSON_KEY_EVENT_AREA_ID "area_id"
//#define JSON_KEY_EVENT_X "x"
//#define JSON_KEY_EVENT_Y "y"
//#define JSON_KEY_EVENT_LIMIT_VALUE "limit_value"
//#define JSON_KEY_EVENT_CUR_VALUE "cur_value"
//#define JSON_KEY_EVENT_CUR_TIME "cur_time"
//#define JSON_KEY_EVENT_LANDMARK_ID "lmid"
//#define JSON_KEY_EVENT_LANDMARK_DIRECTION "lmdirect"
//#define JSON_KEY_EVENT_LANDMARK_DISTANCE "lmdistance"
//#define JSON_KEY_EVENT_READER_POS_CHANGE    "reader_pos_change"

//// alarm
//#define JSON_KEY_ALARM_OVER_COUNT_PERSON "over_count_person"
//#define JSON_KEY_ALARM_OVER_COUNT_VEHICLE "over_count_vehicle"
//#define JSON_KEY_ALARM_OVER_TIME_PERSON "over_time_person"
//#define JSON_KEY_ALARM_OVER_TIME_VEHICLE "over_time_vehicle"
//#define JSON_KEY_ALARM_OVER_SPEED_VEHICLE "over_speed_vehicle"


class module_web : public singleton_base<module_web>
{
private:
    friend class singleton_base<module_web>;
    module_web()
    {
    }

    /**
     * @brief 用来处理WEB发送的请求(web请求回调函数)
     * @param ID
     * @param name
     * @param data
     * @param need_ack
     * @param ack_resp
     */
    void accept( int ID, std::string const& name,
                 sio::message::ptr const& data, bool need_ack, sio::message::list &ack_resp );

    /**
     * @brief 注册web请求回调函数
     * @param MsgFuncList
     */
    void init(std::map<std::string, YA::MSG_HANDLE_FUNC_TYPE>& MsgFuncList);

    /// web前端有用户登录时，反馈给web所有信息
    void response_login();
};

class tool_json
{
public:
    static bool try_get_value(int& d, const char* k, const rapidjson::Value& data)
    {
        if(data.IsObject())
        {
            auto iter = data.FindMember(k);
            if(data.MemberEnd() == iter)
            {
                return false;
            }
            if(iter->value.IsInt())
            {
                d = iter->value.GetInt();
                return true;
            }
        }

        return false;
    }

    static bool try_get_value(uint64_t& d, const char* k, const rapidjson::Value& data)
    {
        if(data.IsObject())
        {
            auto iter = data.FindMember(k);
            if(data.MemberEnd() == iter)
            {
                return false;
            }
            if(iter->value.IsUint64())
            {
                d = iter->value.GetUint64();
                return true;
            }
        }
        return false;
    }

    static bool try_get_value(std::string& d, const char* k, const rapidjson::Value& data)
    {
        if(data.IsObject())
        {
            auto iter = data.FindMember(k);
            if(data.MemberEnd() == iter)
            {
                return false;
            }
            if(iter->value.IsString())
            {
                d = iter->value.GetString();
                return true;
            }
        }

        return false;
    }

//    static bool is_equal(const char* k, const char* v, const rapidjson::Value& data){
//        auto iter = data.FindMember(k);
//        if(data.MemberEnd() == iter){
//            return false;
//        }
//        return 0 == strcmp(v, iter->value.GetString());
//    }

//    static bool is_equal(const char* k, const std::string& v, const rapidjson::Value& data){
//        return is_equal(k, v.c_str(), data);
//    }

//    static int get_value(const char* k, const int& d, const rapidjson::Value& data){
//        auto iter = data.FindMember(k);
//        if(data.MemberEnd() == iter){
//            return d;
//        }
//        if(iter->value.IsInt()){
//            return iter->value.GetInt();
//        }
//        return d;
//    }

//    static std::string get_value(const char* k, const std::string& d, const rapidjson::Value& data){
//        auto iter = data.FindMember(k);
//        if(data.MemberEnd() == iter){
//            return d;
//        }
//        if(iter->value.IsArray() || iter->value.IsObject()){
//            return d;
//        }
//        return iter->value.GetString();
//    }




//    static void push_back(const std::string& v, rapidjson::Value& data, rapidjson::Document::AllocatorType& allocator){
//        rapidjson::Value value_tmp;
//        value_tmp.SetString(v.c_str(), static_cast<rapidjson::SizeType>(v.size()), allocator);
//        data.PushBack(value_tmp, allocator);
//    }

    static std::string ev_to_json(std::shared_ptr<ya_event> ev_ptr)
    {
        std::vector<std::shared_ptr<ya_event>> evs;
        evs.push_back(ev_ptr);
        return evs_to_json(evs);
    }

    static std::string doc_to_json(rapidjson::Document& doc)
    {
        rapidjson::StringBuffer sb;
        rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
        doc.Accept(writer);

        return sb.GetString();
    }

    static std::string evs_to_json(std::vector<std::shared_ptr<ya_event>> evs)
    {
        rapidjson::Document doc(rapidjson::kObjectType);
        rapidjson::Value data(rapidjson::kArrayType);
        rapidjson::Document::AllocatorType& allocator=doc.GetAllocator();

        auto it=evs.begin();
        for(;it!=evs.end();++it)
        {
            ev_to_node(*it, allocator, data);
        }

        doc.AddMember(JSON_ROOT_KEY_CMD,JSON_CMD_VALUE_EVENT, allocator);
        doc.AddMember(JSON_ROOT_KEY_VERSION,INTERFACE_VERSION, allocator);
        doc.AddMember(JSON_ROOT_KEY_DATA,data, allocator);

        rapidjson::StringBuffer sb;
        rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
        doc.Accept(writer);

        return sb.GetString();
    }

private:
    static void ev_to_node(std::shared_ptr<ya_event> ev_ptr,
                                        rapidjson::Document::AllocatorType& allocator,
                                        rapidjson::Value& out_data)
    {
        rapidjson::Value ev(rapidjson::kObjectType);

        ev.AddMember(JSON_KEY_EVENT_EVENT_ID,ev_ptr->get_id(), allocator);
        ev.AddMember(JSON_KEY_EVENT_STATUS,ev_ptr->m_status, allocator);

        ev.AddMember(JSON_KEY_EVENT_TYPE_ID,ev_ptr->m_ev_type, allocator);
        ev.AddMember(JSON_KEY_EVENT_OBJ_TYPE_ID,ev_ptr->m_obj_type, allocator);
        ev.AddMember(JSON_KEY_EVENT_OBJ_ID,ev_ptr->m_obj_id, allocator);
        ev.AddMember(JSON_KEY_EVENT_MAP_ID,ev_ptr->m_map_id, allocator);
        ev.AddMember(JSON_KEY_EVENT_AREA_ID,ev_ptr->m_area_id, allocator);
        ev.AddMember(JSON_KEY_EVENT_X,ev_ptr->x, allocator);
        ev.AddMember(JSON_KEY_EVENT_Y,ev_ptr->y, allocator);

        ev.AddMember(JSON_KEY_EVENT_LIMIT_VALUE,ev_ptr->m_limit_value, allocator);
        ev.AddMember(JSON_KEY_EVENT_CUR_VALUE,ev_ptr->m_cur_value, allocator);
        ev.AddMember(JSON_KEY_EVENT_CUR_TIME,tool_time::to_ms(ev_ptr->m_cur_time), allocator);

        ev.AddMember(JSON_KEY_EVENT_LANDMARK_ID,ev_ptr->m_landmarkid, allocator);
        ev.AddMember(JSON_KEY_EVENT_LANDMARK_DIRECTION,ev_ptr->m_landmarkdirect, allocator);
        ev.AddMember(JSON_KEY_EVENT_LANDMARK_DISTANCE,ev_ptr->m_landmarkdist, allocator);

        out_data.PushBack(ev, allocator);
    }

};

#endif // MODULE_WEB_H
