#ifndef _AREA_HPP_
#define _AREA_HPP_

#include <atomic>
#include <algorithm>
#include <iterator>
#include <point.h>

#include <write-copy.h>

struct area_hover;
struct point;

/**
 * @brief 区域类型
 */
enum AREA_TYPE
{
    ///非考勤区域
    AT_NOT_ATTENDANCE,

    ///与考勤，卡数统计，超员，超时无关的区域
    AT_OTHER,
};

struct area
{
	area()
	{
	}

    virtual void on_hover(uint64_t card_id,std::shared_ptr<area_hover>&c,double speed)=0;
    virtual void on_enter(uint64_t card_id,std::shared_ptr<area_hover>&c,double speed)=0;
    virtual void on_leave(uint64_t card_id,std::shared_ptr<area_hover>&c,double speed)=0;

	int id()const
	{
		return m_id;
	}

	int    m_id;
    ///区域类型  AREA_TYPE
    int m_area_type;

    ///区域卡数
    int card_count()
    {
        return m_person_count + m_vehicle_count;
    }
    ///区域人卡数
    std::atomic<int> m_person_count;
    ///区域车卡数
    std::atomic<int> m_vehicle_count;
    std::atomic<bool> m_is_over_person;

    ///区域超时门限
    int m_limit_time_second;
    ///人数门限
    int m_limit_person_count;
    ///速度门限
    double m_limit_speed;

//	std::string m_name;

	std::vector<point> m_bound;
};

struct area_list:single_base<area_list,int,std::shared_ptr<area>>
{
	area_list();

	std::vector<std::shared_ptr<area>> get_area(const point&pt);
	void init_from_db()
	{
	}
};

struct area_hover
{
	std::shared_ptr<area>  m_area;
    //time_t m_enter_time,m_last_time;
	point  m_enter_point,m_last_point;
    //int    m_num_speeding;

    ///车卡区域超速
    std::atomic<bool> m_is_area_over_speed_vehicle;
    ///车卡区域超速次数计数器
    int m_vehicle_area_over_speed_count;
    ///车卡速度正常次数计数器
    int m_vehicle_area_normal_speed_count;

    ///人卡是否区域超时
    std::atomic<bool> m_is_area_over_time_person;
    ///进入区域的时间
    std::chrono::system_clock::time_point m_enter_time;

	area_hover(std::shared_ptr<area>&area,const point&pt,double speed)
		:m_area(area)
	{
//		m_enter_time=m_last_time=time(0);
//		m_enter_point=m_last_point=pt;
//		m_num_speeding=0;
//		if(speed>m_area->m_limit_speed)
//			m_num_speeding++;
	}

	int id()const
	{
		return m_area->id();
	}

	bool operator == (const area_hover&o)const
	{
		return m_area->id()==o.m_area->id();
	}

	bool operator < (const area_hover&o)const
	{
		return m_area->id()<o.m_area->id();
	}
};

//每张卡包含一个对象
//在解析出数据点时，调用on_point
struct area_tool
{
	std::vector<std::shared_ptr<area_hover>> m_clist;
    void on_point(uint64_t card_id,const point&pt,double speed);

	//检测是否超时
    void on_timer(uint64_t card_id)
	{
	
	}

    void do_hover_biz(uint64_t card_id,std::shared_ptr<area_hover>&a,double speed)
	{
		a->m_area->on_hover(card_id,a,speed);
	}

    void do_enter_biz(uint64_t card_id,std::shared_ptr<area_hover>&a,double speed)
	{
		a->m_area->on_enter(card_id,a,speed);
	}

    void do_leave_biz(uint64_t card_id,std::shared_ptr<area_hover>&a,double speed)
	{
		a->m_area->on_leave(card_id,a,speed);
	}
};

#endif

