#include "module_web.h"

#include"module_call_help.h"
#include"module_call.h"
#include"area_business_person_attendance.h"
#include"module_meta_date_changed.h"
#include"common_tool.h"
#include"event.h"

#include"log.h"

void module_web::accept( int ID, std::string const& name,
                         sio::message::ptr const& data, bool need_ack, sio::message::list &ack_resp )
{
    if(data->get_flag() != sio::message::flag_object)
    {
        log_error("web发来的数据不是对象");
        return;
    }

    std::string cmd = "";
    if(!tool_map::try_get_value(cmd, JSON_ROOT_KEY_CMD, data) || cmd.empty())
    {
        log_error("web发来的数据cmd字段为空");
        return;
    }

    log_info("web发来的数据 cmd=%s", cmd.c_str());

    if(JSON_CMD_VALUE_CLEAR_CARD == cmd)//手动升井
    {
        area_business_person_attendance::handle_up_mine(data);
    }
    else if (JSON_CMD_VALUE_REQUEST_ALL_DATA == cmd)//web登录请求所有信息
    {
        module_web::instance()->response_login();
    }
    else
    {
        sio::message::ptr data_value;
        if(!tool_map::try_get_value(data_value, JSON_ROOT_KEY_DATA, data))
        {
            log_error("web发来的数据data字段格式不对, 不是map");
            return;
        }

        if(JSON_CMD_VALUE_META_DATA_CHANGED == cmd)///基础数据
        {
            module_meta_date_changed::instance()->accept(data_value);
        }
        else if(JSON_CMD_VALUE_DEAL_HELP == cmd) // 处理呼救信息
        {
            module_call_help::instance()->accept_web_deal_help(data_value);
        }
        else if(JSON_CMD_VALUE_CALL_CARD_REQUEST == cmd)//呼叫
        {
            module_call::instance()->accept_call(data_value);
        }
        else if(JSON_CMD_VALUE_CALL_CARD_CANCEL_REQUEST == cmd)//取消呼叫
        {
            module_call::instance()->accept_cancel(data_value);
        }
    }
}

void module_web::response_login()
{
    rapidjson::Document doc(rapidjson::kObjectType);
    auto& allocator = doc.GetAllocator();
    rapidjson::Value nodes(rapidjson::kArrayType);

    //所有的呼叫信息
    std::string str=module_call::instance()->accept_login();
    if(!str.empty())
    {
        tool_json::push_back(nodes, str, allocator);
    }

    //所有告警
    std::vector<std::shared_ptr<ya_event>> arr;
    _get_all_events(arr);
    if(!arr.empty())
    {
        tool_json::push_back(nodes, event_list::evs_to_json(arr), allocator);
    }

    if(nodes.Size()>0)
    {
        doc.AddMember(JSON_ROOT_KEY_CMD,JSON_CMD_VALUE_RESPONSE_ALL_DATA, allocator);
        doc.AddMember(JSON_ROOT_KEY_VERSION, INTERFACE_VERSION, allocator);
        doc.AddMember(JSON_ROOT_KEY_DATA, nodes, allocator);

        swsClientMgr.send(JSON_CMD_VALUE_PUSH, tool_json::doc_to_json(doc));
    }
}

void module_web::run()
{
    std::vector<std::shared_ptr<ya_event>> arr;
    _get_all_events(arr);

    if(!arr.empty())//发送给web端
    {
        _delete_end(arr);

        swsClientMgr.send(JSON_CMD_VALUE_PUSH, event_list::evs_to_json(arr));
    }

    std::string help = module_call_help::get_json_help();
    if(!help.empty())
    {
        swsClientMgr.send(JSON_CMD_VALUE_PUSH, help);
    }
}

void module_web::_get_all_events(std::vector<std::shared_ptr<ya_event>>& arr)
{
    auto _map = event_list::instance()->m_map;
    auto it_map = _map.begin();
    for(;it_map!=_map.end();++it_map)
    {
        arr.push_back(it_map->second);
    }
}

///在全局列表中删除已经处理或结束了的告警
void module_web::_delete_end(std::vector<std::shared_ptr<ya_event>>& arr)
{
    std::vector<uint64_t> todelete;
    auto arr_iter = arr.begin();
    for(;arr_iter!=arr.end();++arr_iter)
    {
        if((*arr_iter)->is_end())//删除掉已经处理的
        {
            todelete.push_back((*arr_iter)->get_list_id());
        }
    }

    if(todelete.size())
    {
        event_list::instance()->remove(todelete);
    }
}