#ifndef _AREA_HPP_
#define _AREA_HPP_

#include <atomic>
#include <algorithm>
#include <cfloat>
#include <iterator>
#include <point.h>
#include "common.h"
#include <write-copy.h>

struct area_hover;
struct point;
struct area_business;
struct business_data;
struct card_location_base;
struct site;

/*
	每个区域对应一个area对象。
*/
struct area
{
    area(int id,int limit_count_person, int limit_time_person,double scale,int32_t mapid,int32_t type);
  	virtual void on_hover(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c);
	virtual	void on_enter(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c);
	virtual	void on_leave(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c);

    virtual bool in_area(const std::shared_ptr<site>&s,const std::shared_ptr<card_location_base>&c, const point & p,int &);
    int id()const
    {
        return m_id;
    }
    int mapid()const
    {
        return m_mapid;
    }
    double scale()const
    {
        return m_scale;
    }
    bool special()const
    {
        return m_area_type == AREA_TYPE_NO_COVER;
    }
    virtual ~area()
    {}

    void  update(int limit_count_person, int limit_time_person,double scale,int32_t mapid,
                     int32_t type,int limit_count_vehicle, int limit_time_vehicle)
    {
        m_area_type=type;
        m_limit_person_second=limit_time_person;
        m_limit_person_count=limit_count_person;
        m_scale=scale;
        m_mapid=mapid;
        m_limit_vehicle_count=limit_count_vehicle;
        m_limit_vehicle_second=limit_time_vehicle;
    }
public:
	std::vector<area_business*> m_area_business_list;
public:
    std::vector<point> m_bound;
	//数据库唯一ID
    int    m_id;

	//用户定义的业务类型,BIT集合
	/*
	    1：位置[优先级]
		2：超时[超时时间分钟数]
		3：超员[人员数量、车辆数量]
		4：超速[超速值、判断策略N/M]
		5：人员考勤
		6：车辆考勤[离开最小，离开最小距离]
		7：禁区[进入时长]
		8：猴车区域
	*/
    int m_area_type;

	//人卡超时及超员数量
    int m_limit_person_second;
    int m_limit_person_count;

	//人卡超时及超员数量
    int m_limit_vehicle_second;
    int m_limit_vehicle_count;
    double m_scale;
    int32_t m_mapid;

    ///区域人卡数
    std::atomic<int> m_person_count;
    ///区域车卡数
    std::atomic<int> m_vehicle_count;

    //区域速度门限
    double m_over_speed_vehicle;
};

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

	//根据分站、所在点找出所在区域列表
	std::vector<std::shared_ptr<area>> get_area(const std::shared_ptr<site> s,const std::shared_ptr<card_location_base> &c,const point&pt,int &);
    std::vector<point> init_path(std::string  &str);
    ///id=-1为初始化所有
    void init_from_db(int id=-1);
    void init_monkeycar_area(int id=-1);
private:
	std::shared_ptr<area>  create(int type,int id,int limit_count_person, int limit_time_person,double scale,int32_t mapid,int32_t b_type);
    //禁区功能-给禁区中的卡发送警告及呼叫
    void CheckAreaType(int area_id,int new_area_type,int old_area_type);
    void CheckAreaType( std::shared_ptr<area> pArea,int new_area_type,int old_area_type);
};

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 landmark_id;
    int landmark_dir;
    double landmark_dis;

	/*
		记录该业务所关心的需持续使用的数据，每个业务一个指针
		建议该数据项在on_enter时初始化,on_leave时清除
	*/
	std::vector<std::shared_ptr<business_data>> m_data;

    area_hover()=default;
    area_hover(std::shared_ptr<area>&area,const point&pt)
        :m_area(area)
    {
        m_enter_time=m_last_time=time(0);
        m_enter_point=m_last_point=pt;
        landmark_id=0;
        landmark_dir=0;
        landmark_dis=0;
    }

	std::shared_ptr<business_data> get_business_data(int type)
	{
		if(type>=(int)m_data.size())
		{
			m_data.resize(type+1);
		}

		return m_data[type];
	}

    int id()const
    {
        return m_area->id();
    }
    int mapid()const
    {
        return m_area->mapid();
    }
    double scale()const
    {
        return m_area->scale();
    }
    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();
    }
    std::tuple<time_t,time_t,int,int,int,int,double,double> getLandmark()
    {
        return std::make_tuple(m_enter_time,m_last_time,mapid(),id(),landmark_id,landmark_dir,landmark_dis,scale());
    }
    void setLandmark(const point &pt);
    void set(const point&pt)
    {
        m_last_time=time(0);
        m_last_point = pt;
    }
};

//每张卡包含一个对象
//在解析出数据点时，调用on_point
struct site;
struct area_tool
{
	//卡所在的所有area的列表，以id排序小->大
	std::vector<std::shared_ptr<area_hover>> m_hover_list;

	//推送卡位置时需要推送的所在区域id列表
	std::vector<int> m_push_area_id_list;

	std::shared_ptr<area_hover> get_area_first()const
	{
		if(m_hover_list.empty())
			return nullptr;

		return m_hover_list.front();
	}

	void init_area(std::shared_ptr<area>&a,const point&pt,const std::shared_ptr<card_location_base> &c)
	{
		m_hover_list.clear();
		m_hover_list.push_back(std::make_shared<area_hover>(a,pt));
		a->on_enter(m_hover_list.back(), c);
	}

	void on_point(const std::shared_ptr<site>&s,const std::shared_ptr<card_location_base>& c,const point&pt);

    void setLandmark(const point &pt)
    {
		for(const auto & ha:m_hover_list)
			ha->setLandmark(pt);
    }
    std::tuple<time_t,time_t,int,int,int,int,double,double> getLandmark()
    {
		double dis = DBL_MAX,d=0;
		std::shared_ptr<area_hover> tmp=nullptr;
		for(const auto & ha:m_hover_list)
		{
			d=ha->landmark_dis;
			if(d<dis){
				dis=d;tmp = ha;
			}
		}	
		if(tmp)
		  return tmp->getLandmark();
		else
          return std::make_tuple(0,0,0,0,0,0,0,0);

    }

	void change_area(uint32_t card_id,double speed,int16_t type,int32_t new_areaid)
	{
#if 0
		do_leave_biz(card_id,speed,type);
		auto area = area_list::instance()->get(new_areaid);
		point pt;
		m_area_hover.reset(new area_hover(area,pt,speed));
		do_enter_biz(card_id,speed,type);
#endif
	}
};

#endif

