
#include "area.h"
#include "card_base.h"
#include "event.h"
#include "tool_time.h"
#include "common_tool.h"
#include "log.h"
#include "area_business_person_dwell_checker.h"
/*
	判断当前区域a中的人卡停留时间
	人员进入区域时间存储在area_hover对象中,在当前类on_enter/on_leave中进行更新
	人员&车辆的代码重用，请自行设计
*/

struct SPersonDwellChecker : business_data
{
    int m_enter_time;
    point m_enter_point;
    int m_level_time;
    point m_level_point;
    bool m_send_event;  // 是否发生告警
    SPersonDwellChecker()
    {
        m_enter_time = 0;
        m_level_time = 0;
        m_send_event = false;
    }
};

//进入区域，记录进入时间
void area_business_person_dwell_checker::on_enter(const std::shared_ptr<area_hover>&a,
							const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data>&ptr)
{
    if (nullptr == ptr)
    {
        ptr = std::make_shared<SPersonDwellChecker>();
    }
    auto ptr_temp = dynamic_cast<SPersonDwellChecker*>(ptr.get());
    ptr_temp->m_enter_point.set(c->x,c->y,c->z);
    ptr_temp->m_enter_time = tool_time::now_to_seconds();
    ptr_temp->m_level_time = 0;
    ptr_temp->m_level_point.set(0,0,0);
    a->m_data[this->area_business_type()] = ptr;
    log_info("area_business_person_dwell_checker::on_enter : In Area=%d Card = %d  ",a->m_area->is_mine(),c->m_id);
}


//判断是否超时
void area_business_person_dwell_checker::on_hover(const std::shared_ptr<area_hover>&a,
							const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr)
{
    auto ptr_temp = dynamic_cast<SPersonDwellChecker*>(ptr.get());
    if(nullptr == ptr_temp || ptr_temp->m_enter_time == 0)
    {
        return ;
    }
	double limit_val = 0;
	double cur_val = ( tool_time::now_to_seconds() - ptr_temp->m_enter_time);
	EVENT_TYPE evType = EVENT_TYPE::ET_CARD_AREA_OVER_TIME_PERSON;
	if(c->is_person())
    {
	    if (a->m_area->m_limit_person_second > cur_val)
        {
	        return;
        }
	    ptr_temp->m_send_event = true;
	    evType = a->m_area->is_mine() ? EVENT_TYPE::ET_CARD_OVER_TIME_PERSON : EVENT_TYPE::ET_CARD_AREA_OVER_TIME_PERSON;
	    limit_val = a->m_area->m_limit_person_second;
    }
	else if (c->is_vehicle())
    {
	    return ;
    }
	else
    {
        return;
    }
    uint64_t id = tool_other::type_id_to_u64(c->m_type, c->m_id);
    event_tool::instance()->handle_event(OT_CARD,evType,id,limit_val,cur_val,true);
}


//如果有超时告警，取消超时告警
void area_business_person_dwell_checker::on_leave(const std::shared_ptr<area_hover>&a,
							const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr)
{
    auto ptr_temp = dynamic_cast<SPersonDwellChecker*>(ptr.get());
    if(nullptr == ptr_temp)
    {
        return ;
    }
    ptr_temp->m_level_point.set(c->x,c->y,c->z);
    ptr_temp->m_level_time = tool_time::now_to_seconds();
    ptr_temp->m_enter_point.set(0,0,0);
    ptr_temp->m_enter_time = 0;
    ptr_temp->m_send_event = false;
    EVENT_TYPE evType = EVENT_TYPE::ET_CARD_AREA_OVER_TIME_PERSON;
    if(c->is_person())
    {
        evType = a->m_area->is_mine() ? EVENT_TYPE::ET_CARD_OVER_TIME_PERSON : EVENT_TYPE::ET_CARD_AREA_OVER_TIME_PERSON;
    }
    else
    {
        return ;
    }
    uint64_t id = tool_other::type_id_to_u64(c->m_type, c->m_id);
    event_tool::instance()->handle_event(OT_CARD,evType,id,0,0,false);
}

