#include <assert.h>

#include "card_base.h"
#include "area_business.h"
#include "area.h"
#include "card.h"

#include "module_service/module_call.h"
/*
	确定推送人员信息时的区域,每一个明确需要推送的区域,都要推送
*/
struct area_business_post_area:area_business
{
	virtual int area_business_type()
	{
		return 1;
	}
	//将推送区域信息加入人员数据
	virtual void on_enter(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr){}
	virtual void on_hover(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr){}
	//从人员数据中清除区域信息
	virtual void on_leave(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr){}
};

/*
	判断车辆超速,确定超速后记录日志、数据库并告警
	每张卡在每个区域的超速相关的数据信息记录在 area_hover 对象
	人员&车辆的代码重用,请自行设计
*/
struct area_business_speed_checker:area_business
{
	struct speed_checker_data:business_data
	{
		std::vector<double> speed;
	};

	virtual int area_business_type()
	{
		return 4;
	
	}

	//在ptr对象中初始化超速检测所需的对象
	virtual void on_enter(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr){}
	//根据超速检测的策略,进行超速判断,超速时进行告警
	//建议使用最近M秒内N秒超时进行判断,M=20,N=15,策略数据记录在ptr中..如此:一辆车理想情况下15s才能检测出来。15*4m/s=60mbut.15s内有可能只上来一个超速点。
	virtual void on_hover(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr){}
	//清除超速检测所需的对象
	virtual void on_leave(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr){}
};


/*
	禁区
*/
struct area_business_restricted:area_business
{
	virtual int area_business_type()
	{

		return 7;
	}
	//记录进入时间等信息,生成告警
	virtual void on_enter(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr)
	{
#if 0
        a->m_enter_point = point(c->x,c->y,c->z);
        a->m_last_point.set(0,0,0);
        a->m_last_time = 0;
        a->m_enter_time = time(nullptr);

        event_ptr evPtr = event_list::instance()->get_event_card(c->m_id,c->m_type,ET_CARD_AREA_FORBIDDEN_PERSON);
        if (nullptr == evPtr)
        {
            event_ptr evPtr = event_list::create_event_card(c->m_id, c->m_type, ET_CARD_AREA_FORBIDDEN_PERSON);
            event_list::instance()->copy_event(c, evPtr);
            event_list::instance()->add(evPtr->get_list_id(), evPtr);
        }
        evPtr->m_limit_value = 0;
        evPtr->m_cur_value = 0;
        evPtr->m_is_display = false;
        evPtr->m_status = EVENT_STATUS::ES_START ;
        evPtr->m_cur_time = std::chrono::system_clock::now();

        event_list::save_event(evPtr);

        //呼叫
        module_call::instance()->system_call_apoint(c->m_id,c->m_type);
#endif

	}
	virtual void on_hover(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr){}
	//记录退出时间等信息
	virtual void on_leave(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr)
	{
#if 0
        a->m_last_point = point(c->x,c->y,c->z);
        a->m_last_time = time(nullptr);

        // 发送警告
        int oldStatus = -1;
        event_ptr evPtr = event_list::instance()->get_event_card(c->m_id,c->m_type,ET_CARD_AREA_FORBIDDEN_PERSON);
        if (nullptr == evPtr)
        {
            event_ptr evPtr = event_list::create_event_card(c->m_id, c->m_type, ET_CARD_AREA_FORBIDDEN_PERSON);
            event_list::instance()->copy_event(c, evPtr);
            event_list::instance()->add(evPtr->get_list_id(), evPtr);
            oldStatus = EVENT_STATUS::ES_END;
        } else{
            if (evPtr->m_status != EVENT_STATUS::ES_END)
            {
                oldStatus = EVENT_STATUS::ES_END;
            }
        }
        evPtr->m_limit_value = 0;
        evPtr->m_cur_value = 0;
        evPtr->m_is_display = false;
        evPtr->m_status = EVENT_STATUS::ES_END ;
        evPtr->m_cur_time = std::chrono::system_clock::now();
        if (oldStatus != -1)
        {
            event_list::save_event(evPtr);
        }
        //取消呼叫
        module_call::instance()->system_cancel_call_apoint(c->m_id,c->m_type);
#endif
    }
};

/*
	猴车区域
*/
struct area_business_monkey_area:area_business
{
	virtual int area_business_type()
	{
		return 8;
	}
};

/*
	车辆考勤
*/
struct area_business_car_attendance:area_business
{
	virtual int area_business_type()
	{
		return 6;
	
	}
	//记录进入时间等信息,结束考勤,根据离开的时间和距离,判断是否记录一条新的考勤记录
	virtual void on_enter(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr){}
	virtual void on_hover(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr){}
	//记录离开考勤区域信息,开始考勤
	virtual void on_leave(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr){}
};

/*
	人员考勤
*/
struct area_business_person_attendance:area_business
{
	virtual int area_business_type()
	{
		return 5;
	
	}
	//记录进入时间等信息,开始考勤
	virtual void on_enter(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr){}
	virtual void on_hover(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr){}
	//记录离开考勤区域信息,结束考勤
	virtual void on_leave(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr){}
};

/*
	判断当前区域a中的人数是否超过设定人数,超过后告警
	区域内实时人数存储在area对象中,在当前类on_enter/on_leave中进行更新
	整个井下的超员和某个区域的超员都使用这个代码
*/
struct area_business_count_checker:area_business
{

	virtual int area_business_type()
	{
		return 3;
	
	}
	//增加计数,并进行判断
	virtual void on_enter(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr){}
	virtual void on_hover(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr){}
	//减少计数
	virtual void on_leave(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr){}
};


/*
	判断当前区域a中的人卡停留时间
	人员进入区域时间存储在area_hover对象中,在当前类on_enter/on_leave中进行更新
	人员&车辆的代码重用,请自行设计
*/
struct area_business_person_dwell_checker:area_business
{
	virtual int area_business_type()
	{
		return 2;
	}
	//进入区域,记录进入时间
	virtual void on_enter(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr){}
	//判断是否超时
	virtual void on_hover(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr){}
	//如果有超时告警,取消超时告警
	virtual void on_leave(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr){}
};

struct area_business_card_enter_or_leave:area_business
{
	virtual int area_business_type()
	{
		return 9;
	}
	//进入区域则入库操作
	virtual void on_enter(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr){}
	virtual void on_hover(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr){}
	//出区域则入库
	virtual void on_leave(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr){}
};

struct area_business_factory
{
	void regist(int type,area_business* ab)
	{
		if(type>=(int)m_check_list.size())
		{
			m_check_list.resize(type+1);
		}

		assert(!m_check_list[type]);

		m_check_list[type].reset(ab);
	}

	std::vector<area_business*> get_check_list(int business_type)
	{
		std::vector<area_business*> check_list;
		for(int i=0;i<32;i++)
		{
			if(business_type==0)
				break;

			if((business_type&1)==0)
				continue;

			check_list.push_back(m_check_list[i].get());
		}

		return std::move(check_list);
	}

	static area_business_factory& instance()
	{
		static area_business_factory _instance;
		return _instance;
	}
private:
	std::vector<std::unique_ptr<area_business>> m_check_list;
	area_business_factory()
	{
		regist(1,new area_business_post_area);
		regist(2,new area_business_person_dwell_checker);
		regist(3,new area_business_count_checker);
		regist(4,new area_business_speed_checker);
		regist(5,new area_business_person_attendance);
		regist(6,new area_business_car_attendance);
		regist(7,new area_business_restricted);
		//regist(8,new area_business_monkey_area);
		regist(9,new area_business_card_enter_or_leave);
	}

};

std::vector<area_business*> area_business::get_instance_list(int business_type)
{
	return area_business_factory::instance().get_check_list(business_type);
}