#include "area.h"
#include "card_base.h"
#include "common_tool.h"
#include "log.h"
#include "event.h"
#include "area_business_count_checker.h"
/*
	判断当前区域a中的人数是否超过设定人数，超过后告警
	区域内实时人数存储在area对象中,在当前类on_enter/on_leave中进行更新
	整个井下的超员和某个区域的超员都使用这个代码
*/
//增加计数,并进行判断
void area_business_count_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 == a->m_area )//|| !a->m_area->is_mine())
    {
        return ;
    }
	EVENT_TYPE ev = EVENT_TYPE::ET_OVER_COUNT_PERSON;
	int limitVal = 0;
	int curVal = 0;
	//uint64_t key = tool_other::type_id_to_u64(c->m_id,c->m_type);
	if (c->is_person())
	{
		a->m_area->m_person_count ++ ;
		limitVal = a->m_area->m_limit_person_count;
		curVal = a->m_area->m_person_count;
		ev = a->m_area->is_mine()?EVENT_TYPE::ET_OVER_COUNT_PERSON : EVENT_TYPE::ET_AREA_OVER_COUNT_PERSON ;
	}
	else if (c->is_vehicle())
	{
		a->m_area->m_vehicle_count ++ ;
		limitVal = a->m_area->m_limit_vehicle_count;
		curVal = a->m_area->m_vehicle_count;
		ev = a->m_area->is_mine()?EVENT_TYPE::ET_OVER_COUNT_VEHICLE : EVENT_TYPE::ET_AREA_OVER_COUNT_VEHICLE ;
	}
	if(curVal > limitVal)
	{
		if (c->is_vehicle())
		{
			a->m_area->m_event_vehicle_count = true;
		}
		else
		{
			a->m_area->m_event_person_count = true;
		}
		event_tool::instance()->handle_event(a->m_area->is_mine()? OT_MINE : OT_AREA,ev,a->m_area->id(),limitVal,curVal,true);
		log_info("area_business_count_checker::on_enter : Event OVER_COUNT=%d AreaId=%d,Limit=%d,CurNum=%d"
		            ,ev,a->m_area->id(),limitVal,curVal);
	}
}

void area_business_count_checker::on_hover(const std::shared_ptr<area_hover>&a,
						const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr)
{


}

	//减少计数
void area_business_count_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 (nullptr == a->m_area )//|| !a->m_area->is_mine())
    {
        return ;
    }
	int limitVal = 0;
	int curVal = 0;
	bool tmp_event = false;
	EVENT_TYPE ev = EVENT_TYPE::ET_OVER_COUNT_PERSON;
	if (c->is_person())
	{
		a->m_area->m_person_count -- ;
		limitVal = a->m_area->m_limit_person_count;
		curVal = a->m_area->m_person_count;
		tmp_event = a->m_area->m_event_person_count;
		ev = a->m_area->is_mine()?EVENT_TYPE::ET_OVER_COUNT_PERSON : EVENT_TYPE::ET_AREA_OVER_COUNT_PERSON ;
	}
	else if (c->is_vehicle())
	{
		a->m_area->m_vehicle_count -- ;
		limitVal = a->m_area->m_limit_vehicle_count;
		curVal = a->m_area->m_vehicle_count;
		tmp_event = a->m_area->m_event_vehicle_count;
		ev = a->m_area->is_mine()?EVENT_TYPE::ET_OVER_COUNT_VEHICLE : EVENT_TYPE::ET_AREA_OVER_COUNT_VEHICLE ;
	}
	else{
		return ;
	}
	if(tmp_event && curVal < limitVal)
	{
		if (c->is_vehicle())
		{
			a->m_area->m_event_vehicle_count = false;
		}
		else
		{
			a->m_area->m_event_person_count = false;
		}
        log_info("area_business_count_checker::on_leave :Cancel Event OVER_COUNT=%d AreaId=%d,Limit=%d,CurNum=%d"
        ,ev,a->m_area->id(),limitVal,curVal);
		event_tool::instance()->handle_event(a->m_area->is_mine()? OT_MINE : OT_AREA,ev,a->m_area->id(),limitVal,curVal,false);
	}
}

