#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"

void area_business_person_dwell_checker::on_load_his(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data>&ptr)
{
    if(!c->is_person())
      return;
    EVENT_TYPE evType = a->m_area->is_mine() ? EVENT_TYPE::ET_CARD_OVER_TIME_PERSON : EVENT_TYPE::ET_CARD_AREA_OVER_TIME_PERSON;
    auto ev_ptr_temp = event_list::instance()->get_event_card(c->m_id,c->m_type,evType,DT_COMMON);
    if (ev_ptr_temp && !ev_ptr_temp->is_end())
        c->set_event_flag(evType);
}

//进入区域,记录进入时间
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)
{
    log_info("area_business_person_dwell_checker::on_enter : In Area=%d Card = %d  ",a->m_area->id(),c->m_id);

	if( a->m_area->m_limit_person_min == 0 )
	{
		log_warn("区域area_id=%d超时值设置为0,不会检查超时。",a->m_area->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)
{
    if(!c->is_person()||a->m_enter_time==0||a->m_area->m_limit_person_min==0)
      return;

	int limit_val = a->m_area->m_limit_person_min*60;
	int cur_val = ( tool_time::now_to_seconds() - a->m_enter_time / 1000);

	EVENT_TYPE evType = a->m_area->is_mine() ? EVENT_TYPE::ET_CARD_OVER_TIME_PERSON : EVENT_TYPE::ET_CARD_AREA_OVER_TIME_PERSON;
	if (limit_val < cur_val)
    {
        c->set_event_flag(evType);
        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)
{
    if(!c->is_person()||a->m_enter_time==0||a->m_area->m_limit_person_min==0)
      return;

    EVENT_TYPE evType = a->m_area->is_mine() ? EVENT_TYPE::ET_CARD_OVER_TIME_PERSON:EVENT_TYPE::ET_CARD_AREA_OVER_TIME_PERSON;
    if(c->get_event_flag(evType))
    {
        c->set_event_flag(evType,0);
        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);
    }
}