#ifndef MODULE_CALL_HELP_H
#define MODULE_CALL_HELP_H

/**
  *@brief 呼救模块
  * 把呼救消息保存到数据库，同时推送给web
  * @author 戴月腾
  * @date 2018-08-09
*/

#include <mutex>
#include <map>
#include <chrono>
#include <boost/thread.hpp>
#include <boost/enable_shared_from_this.hpp>
#include "module_web.h"
#include "module_const.h"

/**
 * @brief 呼救类
 * 把呼救消息保存到数据库，同时推送给web, 单例
 */
class module_call_help : public i_thread, public singleton_base<module_call_help>
{
private:
    friend class singleton_base<module_call_help>;
    module_call_help()
    {
    }

public:
    /**
     * @brief 接收到呼救请求
     */
    void rev_help(std::shared_ptr<card> card_ptr)
    {
        if(ES_START != card_ptr->m_stat_call_help)
        {
            card_ptr->m_stat_call_help = ES_START;
            //保存到数据库
            auto ev_ptr = tool_other::create_event(OT_CARD, card_ptr->m_id, ET_CARD_HELP);

            tool_other::copy_event(card_ptr, ev_ptr);

//            ev_ptr->status = ES_START;
//            ev_ptr->cur_time = std::chrono::system_clock::now();

            //保存到数据库
            tool_db::save_event(ev_ptr);
            //转发给web
            swsClientMgr.send(JSON_CMD_VALUE_PUSH, tool_json::ev_to_json(ev_ptr));

            std::lock_guard<std::mutex> lock(_mutex);
            tool_other::insert_event(ev_ptr, _map);
        }
    }

    /**
     * @brief 接收到web端发来的处理呼救请求
     * @param data_map
     */
    void accept_deal_help(std::map<std::string, sio::message::ptr> data_map)
    {
        uint32_t cardid = data_map[JSON_KEY_ID]->get_int();
        //uint64_t ev_id = data_map[JSON_KEY_EVENT_EVENT_ID]->get_int();

        auto card_ptr = card_list::instance()->get(tool_other::to_uint64_cardid(CT_PERSON, cardid));
        if(card_ptr)
        {
            card_ptr->m_stat_call_help = ES_DEAL_HELP;

            std::lock_guard<std::mutex> lock(_mutex);
            auto ev_ptr = tool_other::find_event(cardid, ET_CARD_HELP, _map);
            if(ev_ptr)
            {
                ev_ptr->m_status = ES_DEAL_HELP;
                tool_db::save_event(ev_ptr);
            }
        }
//        else
//        {
//            std::lock_guard<std::mutex> lock(_mutex);
//            _map.erase(ev_id);
//        }
    }

    /**
     * @brief web前端有用户登录时，反馈给web所有信息
     */
    std::string response_login()
    {
        std::vector<std::shared_ptr<ya_event>> arr;
        get_all_events(arr);

        if(!arr.empty())//发送给web端
        {
            return tool_json::evs_to_json(arr);
        }

        return "";
    }

    /**
     * @brief 读取数据库中的告警，初始化告警列表
     */
    void init()
    {}

private:
    /**
     * @brief 线程函数
     */
    void run()
    {
        std::vector<std::shared_ptr<ya_event>> arr;
        get_all_events(arr);

        if(!arr.empty())//发送给web端
        {
            swsClientMgr.send(JSON_CMD_VALUE_PUSH, tool_json::evs_to_json(arr));
        }
    }

    /**
     * @brief 获取所有的告警事件
     * @param out_data
     */
    void get_all_events(std::vector<std::shared_ptr<ya_event>>& arr)
    {
        {
            std::lock_guard<std::mutex> lock(_mutex);

            auto it_map = _map.begin();
            for(;it_map!=_map.end();++it_map)
            {
                arr.push_back(it_map->second);
                if(ES_DEAL_HELP== it_map->second->m_status)//删除掉已经处理的
                {
                    _map.erase(it_map--);
                }
            }
        }
    }

    std::map<uint64_t, std::shared_ptr<ya_event>> _map;
};


#endif
