#ifndef _AREA_HPP_
#define _AREA_HPP_

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

#include <write-copy.h>

struct area_hover;
struct point;
struct area
{
	area()
	{
	}

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

	int id()const
	{
		return m_id;
	}

	std::atomic<int> m_card_count;

	int    m_id;
	double m_limit_speed;

//	std::string m_name;
//	int m_limit_time_second;
//	int m_limit_person_count;
//	int m_area_type;

	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;

	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(int64_t card_id,const point&pt,double speed);

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

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

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

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

#endif

