#ifndef MODULE_AREA_TIMEOUT_H
#define MODULE_AREA_TIMEOUT_H
/**
 * @brief 区域超时
 * @author 戴月腾
 * @date 2018-10-10
 */


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


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

public:
    void on_enter(std::shared_ptr<card_location_base> card_ptr,std::shared_ptr<area_hover>&c)
    {
//        EVENT_TYPE ev_type = card_ptr->is_person() ? ET_CARD_AREA_OVER_TIME_PERSON : ET_AREA_OVER_COUNT_VEHICLE;
//        //中途服务器宕机处理
//        if(auto ev_ptr = event_list::instance()->get(card_ptr->m_id, ev_type))
//        {
//            c->m_enter_time = std::chrono::system_clock::to_time_t(ev_ptr->m_cur_time);
//        }
//        else
//        {
//            std::time(&c->m_enter_time);
//        }
    }

    void on_hover(std::shared_ptr<card_location_base> card_ptr,std::shared_ptr<area_hover>&c)
    {
        auto area_ptr = c->m_area;

        int limit = area_ptr->m_limit_person_second;
        EVENT_TYPE ev_type = ET_CARD_AREA_OVER_TIME_PERSON;
        if(card_ptr->is_vehicle())
        {
            limit = area_ptr->m_limit_vehicle_second;
            ev_type = ET_CARD_AREA_OVER_TIME_VEHICLE;
        }

        int seconds = tool_time::elapse_seconds(c->m_enter_time);

        if(seconds > limit)//发生告警
        {
            auto ev_ptr = event_list::instance()->get_event_card(card_ptr->m_id, card_ptr->m_type, ev_type);
            if(ev_ptr)
            {
                event_list::copy_event(card_ptr, ev_ptr);

                ev_ptr->m_limit_value = limit;
                ev_ptr->m_cur_value = seconds;
            }
            else//从没有告警状态转化为告警状态
            {
                ev_ptr = event_list::create_event_card(card_ptr->m_id, card_ptr->m_type, ev_type);

                event_list::copy_event(card_ptr, ev_ptr);

                ev_ptr->m_limit_value = limit;
                ev_ptr->m_cur_value = seconds;

                //保存到数据库
                event_list::save_event(ev_ptr);

                event_list::instance()->add(ev_ptr->get_list_id(),ev_ptr);

                log_info("卡区域超时开始：卡id=%d，卡type=%d，区域id=%d，时间门限=%d，呆的时间长度=%d",
                         card_ptr->m_id, card_ptr->m_type,
                         area_ptr->id(), limit, seconds);
            }
        }
    }

    void on_leave(std::shared_ptr<card_location_base> card_ptr,std::shared_ptr<area_hover>&c)
    {
        auto area_ptr = c->m_area;

        int limit = area_ptr->m_limit_person_second;
        EVENT_TYPE ev_type = ET_CARD_AREA_OVER_TIME_PERSON;
        if(card_ptr->is_vehicle())
        {
            limit = area_ptr->m_limit_vehicle_second;
            ev_type = ET_CARD_AREA_OVER_TIME_VEHICLE;
        }

        auto ev_ptr = event_list::instance()->get_event_card(card_ptr->m_id, card_ptr->m_type, ev_type);
        if(ev_ptr && !ev_ptr->is_end())//有超时告警就删除
        {
            event_list::copy_event(card_ptr, ev_ptr);

            ev_ptr->m_limit_value = limit;
            ev_ptr->m_cur_value = tool_time::elapse_seconds(c->m_enter_time);

            ev_ptr->m_status = ES_END;

            //保存到数据库
            event_list::save_event(ev_ptr);

            log_info("卡区域超时结束：卡id=%d，卡type=%d，区域id=%d，时间门限=%d",
                     card_ptr->m_id, card_ptr->m_type,
                     area_ptr->id(), limit);
        }
    }
};

#endif // MODULE_AREA_TIMEOUT_H
