
#include "card_car.h"
#include "card_message_handle.h"
#include "his_location.h"
#include "area.h"
#include "mine.h"
#include "site_area.h"
#include "three_rates.h"
#include "select_tool.h"
#include "websocket/ws_common.h"
#include "special_area.h"
#include "websocket/wsClientMgr.h"
#include "common_tool.h"
#include "tool_time.h"
#include "mine_business.h"
car::car(std::string type,uint32_t cardid,uint16_t needdisplay,int16_t t,int32_t deptid,
		int32_t categoryid, int type_id,int32_t level_id,uint32_t cid)
	:card_location_base(type,cardid,needdisplay,t,deptid,level_id,cid)
	,m_vehicle_category_id(categoryid)
	 ,m_vehicle_type_id(type_id)
{
	m_message_handle.reset(new card_message_handle(this));
	m_his_location_card.reset(new location_vehicle(m_id,m_type));
}

car::~car(){}

void car::site_hover(int sid)
{
	if(m_time<=0)return;
	m_site_area->on_point(m_id,sid,0, m_type);
}

std::shared_ptr<area_hover> car::get_area_hover()
{
	return m_area_tool->get_area_first();
}

std::shared_ptr<mine_tool> car::get_mine_tool()
{
	return m_mine_tool;
}

std::shared_ptr<site_area_hover> car::get_site_area()
{
	return m_site_area;
}

void car::do_business(const std::shared_ptr<site>&site,const point &pt,double acc)
{
	m_acc=acc;
	m_area_tool->on_point(site,shared_from_this(),pt);
	m_timeval=m_time;
	m_site_area->on_point(m_id,0,this, m_type);
	m_mine_tool->on_point(m_id, m_type, m_vehicle_category_id);
	handle_three_rates(pt);
	uint64_t id=tool_other::type_id_to_u64(m_type,m_id);
	mine_business::inst()->make_arg(id,pt,m_time);
}

int car::get_vehicle_type_id()
{
	return  m_vehicle_type_id;
}

void car::handle_three_rates(const point &pt)
{
	card_pos cp;
	m_biz_stat=get_stat();
	cp.biz_stat = m_biz_stat;
	cp.x=pt.x;cp.y=pt.y;cp.z=pt.z;	
	const auto lm = m_area_tool->getLandmark();
	cp.enter_time = std::get<0>(lm)*1000;
	cp.area_id = std::get<3>(lm);
	cp.map_id = std::get<2>(lm);
	cp.vibration=m_acc;
	put_three_rates(cp);
}

void car::on_timer()
{
	make_package();
}
int car::get_area()
{
	int status = m_biz_stat;
	int special_id = -1;
	if(status == STATUS_LOST)
	{
		special_id = special_area_list::instance()->get_special_id(m_id,*this,m_vehicle_category_id);
		log_info("enter_special_area:%.2f,%2.f,id:%d,special_area_id:%d",x,y,m_id,special_id);
	}
	return special_id;
}

void car::make_package()
{
	YA::_CARD_POS_ cp;
	loc_point pt = getSmoothPoint();

	const auto lm = m_area_tool->getLandmark();
	cp.enter_area_time = std::get<0>(lm);
	cp.rec_time = std::get<1>(lm);
	uint32_t map_id = std::get<2>(lm);
	uint32_t area_id = std::get<3>(lm);
	cp.map_id =map_id;cp.area_id=area_id;
	cp.landmark_id = std::get<4>(lm);
	cp.lm_direction = std::get<5>(lm);
	cp.landmark_dis=std::get<6>(lm);
	int32_t biz_stat=m_biz_stat;
	
	cp.biz_stat=biz_stat;
	cp.down_time = m_mine_tool->get_down_time();
	cp.work_time = m_mine_tool->get_work_time();
	cp.is_on_duty=m_mine_tool->is_on_duty();
	upt_card_pos(cp,pt);
	m_his_location_card->push(pt.m_time,pt,area_id,map_id);
	uint64_t _now=tool_time::now_to_ms();
	uint64_t t=_now>m_timeval?_now-m_timeval:m_timeval-_now;
	if(t>10*1000)
	{
		auto site = m_site_area->get_site();
		m_area_tool->on_point(site,shared_from_this(),pt);
		m_biz_stat=get_stat();
	}

//	if(biz_stat==STATUS_LOST && special_id != -1 && m_display==1)
//	{
//		cp.area_id = special_id;
//		swsClientMgr.SendSpecialAreaProcess(cp);
//	}
//	uint64_t _now=tool_time::now_to_ms();
//	uint64_t t=_now>m_time?_now-m_time:m_time-_now;
//	if(t<45*1000 && !empty())
//	  f=true;
//	return f;
}
loc_point car::getSmoothPoint()
{
	loc_point lp = m_smo_tool->smooth_strategy();
	m_speed = lp.m_speed;
	m_stat  = lp.m_stat;
	lp.y = -lp.y;
	return lp;
}

std::tuple<time_t,time_t,int,int,int,int,double,double> car::getLandmark()
{
	return  m_area_tool->getLandmark();
}

