#include"module_call_help.h"

#include"event.h"
#include"common_tool.h"
#include"websocket/constdef.h"
#include"log.h"
#include"db/db_tool.h"
#include"tool_time.h"

/**
 * @brief //1.如果已经有呼救告警
//2.web取消告警。status_help && status_help_cancel 置为status_help_dealed
//3.如果底层已经没有呼救则，取消后，不会再次进行呼救。因为status_help已经不会置为status_error。
//4.如果底层还在呼救。这时候不管status_help是何种状态，极大的可能是error.status_help_cancel的存在都会取消。
//并且还要保证等待线程把json传送到前端
 */
void module_call_help::rev_from_card_help(std::shared_ptr<card_location_base> card_ptr)
{
    uint64_t id = tool_other::type_id_to_u64(card_ptr->m_type, card_ptr->m_id);
    event_tool::instance()->handle_event(OT_CARD, ET_CARD_HELP, id, 0, 0, true);
}

/**
 * @brief 接收到web端发来的处理呼救请求
 * @param data_map
 */
void module_call_help::accept_web_deal_help(sio::message::ptr const& data)
{
    int64_t ev_id = 0;
    std::string s_card_id;
    if(!tool_map::try_get_value(s_card_id, JSON_KEY_ID, data)
            ||!tool_map::try_get_value(ev_id, JSON_KEY_EVENT_EVENT_ID, data))
    {
        log_error("处理呼救，web发来的数据 card_id 或 ev_id格式不对");
        return;
    }

    uint32_t id = tool_other::id64_to_id(s_card_id);
    int type = tool_other::id64_to_type(s_card_id);

    auto card_ptr = card_list::instance()->get(tool_other::type_id_to_u64(type, id));
    if(card_ptr)
    {
        log_info("处理呼救，card_id=%d， type=%d,  ev_id=%d", id, type, ev_id);

        auto ev_ptr = event_list::instance()->get_event_card(id, type, ET_CARD_HELP);
        if(ev_ptr && ES_DEAL_HELP != ev_ptr->m_status)
        {
            ev_ptr->m_status = ES_DEAL_HELP;

            event_list::save_event(ev_ptr);

            //删除
            //event_list::instance()->remove(ev_ptr->get_list_id(), ev_ptr);
        }
    }
    else
    {
        log_error("处理呼救，在全局列表中找不到卡，卡id=%d，卡type=%d", id, type);
    }
}

std::string module_call_help::get_json_help()
{
    rapidjson::Document doc(rapidjson::kObjectType);
    rapidjson::Value data(rapidjson::kArrayType);
    rapidjson::Document::AllocatorType& allocator=doc.GetAllocator();

    auto tmp = event_list::instance()->m_map;
    auto it_ev=tmp.begin();
    for(;it_ev!=tmp.end();++it_ev)
    {
        if(!it_ev->second->m_is_display || ET_CARD_HELP != it_ev->second->m_ev_type)
        {
            continue;
        }

        std::string obj_id = it_ev->second->m_obj_id;
        int type= tool_other::id64_to_type(obj_id);

        rapidjson::Value alarm(rapidjson::kObjectType);

        tool_json::add_member(alarm, JSON_KEY_CARD_ID,it_ev->second->m_obj_id, allocator);
        alarm.AddMember(JSON_KEY_CARD_TYPE_ID,type, allocator);

        std::string time = tool_time::to_str(it_ev->second->m_cur_time);
        tool_json::add_member(alarm, "start_time",time, allocator);

        data.PushBack(alarm, allocator);
    }

    if(!data.Size())
    {
        return "";
    }

    doc.AddMember(JSON_ROOT_KEY_CMD,"helpme_req", 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();
}

