#include <memory>
#include <write-copy.h>

#include <area.h>

template<> std::shared_ptr<area_list> 
single_base<area_list, int, std::shared_ptr<area>>::m_instance=std::make_shared<area_list>();

struct area_impl
{
	area_impl()
	{
	}


    void on_hover(int64_t card_id,std::shared_ptr<area_hover>&c,double speed)
	{

	}

    void on_enter(int64_t card_id,std::shared_ptr<area_hover>&c,double speed)
	{
	
	}

    void on_leave(int64_t card_id,std::shared_ptr<area_hover>&c,double speed)
	{
	
	
	}
};

area_list::area_list()
{
}

std::vector<std::shared_ptr<area>> area_list::get_area(const point&pt)
{
	std::vector<std::shared_ptr<area>> ret;
	//需要添加根据点查找区域的算法



	return std::move(ret);
}


void area_tool::on_point(int64_t card_id,const point&pt,double speed)
{
	std::vector<std::shared_ptr<area>> areas=area_list::instance()->get_area(pt);//找出所有的区域
	std::sort(areas.begin(),areas.end(),[](std::shared_ptr<area>&l,std::shared_ptr<area>&r){
			return l->id()<r->id();
			});

	auto c1=m_clist.begin(),ce=m_clist.end();
	auto a1=areas.begin() ,ae=areas.end();

	std::vector<std::shared_ptr<area_hover>> nlist;

	while (c1!=ce && a1!=ae)
	{
		if ((*c1)->id()<(*a1)->id()) 
		{ 
			do_leave_biz(card_id,*c1,speed);
			++c1;
		}
		else if ((*a1)->id()<(*c1)->id()) 
		{
			nlist.push_back(std::make_shared<area_hover>(*a1,pt,speed));
			do_enter_biz(card_id,nlist.back(),speed);
			++a1;
		}
		else 
		{ 
			nlist.push_back(*c1);
			do_hover_biz(card_id,nlist.back(),speed);
			++c1,++a1;
		}
	}

	while(c1!=ce)
	{
		do_leave_biz(card_id,*c1,speed);
		++c1;
	}

	while(a1!=ae)
	{
		nlist.push_back(std::make_shared<area_hover>(*a1,pt,speed));
		do_enter_biz(card_id,nlist.back(),speed);
		++a1;
	}

	m_clist=std::move(nlist);
}

