#include <string>
#include <thread>
#include "card_person.h"
#include "loc_point.h"
#include "card_message_handle.h"
#include "area.h"
#include "mine.h"
#include "site_area.h"
#include "his_location.h"
#include "three_rates.h"
#include "select_tool.h"
#include "monkey_car/monkeycar_person.h"
#include "websocket/ws_common.h"
#include "ya_event.h"
#include "mine_business.h"

person::person(const std::string &type,uint32_t cardid,uint16_t needdisplay,int16_t t,int32_t deptid,int32_t level_id,uint32_t cid,int wl,const std::string &sname,const std::string &dname)
	:card_location_base(type,cardid,needdisplay,t,deptid,level_id,cid)
	,m_workLine(wl)
	,m_stafferName(sname)
	,m_deptName(dname)
{
	m_message_handle.reset(new card_message_handle(this));
	m_his_location_card.reset(new location_staff(m_id,m_type));
}

person::~person()
{
}
void person::clear()
{
	//m_site_area.reset(new site_area_hover);
	m_area_tool.reset(new area_tool);
	m_mine_tool.reset(new mine_tool);

	card_location_base::clear();
}

void person::site_hover(int sid)
{
	if(m_time<=0)
		return;
	m_site_area->on_point(m_id,sid,nullptr, m_type);
	IKSDK_DB(sid);
}
//虹膜识别入库功能	
void person::IKSDK_DB(int sid)
{
	auto sit_ptr = sit_list::instance()->get(sid);
	if(nullptr == sit_ptr)
	  return;
	if(sit_ptr->m_reader_type_id == READER_TYPE_ID_UP)
	{
		time_t now = time(0);
		if(now - m_iris_recognition_timeval > 5)
		{
			int f=0;
			if(event_list::instance()->get_event_card(m_id, m_type, ET_CARD_LOW_POWER_SERIOUS))
			  f=1;
			std::string card_id = tool_other::type_id_to_str(m_type,m_id);
			std::string st	    = tool_time::to_str(now);
			char nsql[256]={0};
			const char*sql="REPLACE INTO his_att_interface (staff_id,card_id,reader_id,staff_name,dept_name,upt_time,low_power_warn) VALUES (%d,%s,%d,'%s','%s','%s',%d);";
			snprintf(nsql,256,sql,m_cid,card_id.c_str(),sid,m_stafferName.c_str(),m_deptName.c_str(),st.c_str(),f);
			db_tool::PushAsync(nsql);
			logn_info(2,"%s",nsql);
			m_iris_recognition_timeval = now;
		}
	}
}
int person::get_area()
{
	return m_site_area->get_att_type();
}
std::shared_ptr<area_hover> person::get_area_hover()
{
	return m_area_tool->get_area_first();
}

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

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

void person::do_business(const std::shared_ptr<site>&site,const point &pt,double acc)
{
	m_area_tool->on_point(site,shared_from_this(),pt);
	m_site_area->on_point(m_id,0,this, m_type);
	handle_three_rates(pt);
}

void person::reset(std::shared_ptr<monkey_person> mp)
{
	m_monkeyPerson = mp;
}

std::tuple<time_t,time_t,int,int,int,int,double,double> person::getLandmark()
{
	return  m_area_tool->getLandmark();
}
void person::handle_three_rates(const point & pt)
{
	card_pos cp;
	cp.work_line=m_workLine;
	cp.biz_stat = get_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);
	put_three_rates(cp);
}

bool person::on_timer()
{
	//人员井下超时
//    m_mine_tool->on_check_overtime(this->m_id,this->m_type);

	if(!m_mine_tool->is_attendance())
		return false;
	YA::_CARD_POS_ cp;
	if(m_display)
	  mine_business::inst()->fetch_add();
	uint64_t _time=0;
	point pt = getSmoothPoint(_time);
	const auto lm = m_area_tool->getLandmark();

	cp.enter_area_time = std::get<0>(lm)*1000;
	cp.rec_time = std::get<1>(lm);

	int32_t map_id = std::get<2>(lm);
	int32_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);

	cp.biz_stat = get_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();
	log_info("on_timer here ...%d,%lld,%.2f,%.2f,%d,%d",m_id,_time,pt.x,pt.y,area_id,map_id);
	m_his_location_card->push(_time,pt,area_id,map_id);
	upt_card_pos(cp,pt);
	return false;
}

point person::getSmoothPoint(uint64_t& t)
{
	point pt;
	loc_point lp = m_smo_tool->smooth_strategy();
	m_speed = lp.m_speed;
	m_stat  = lp.m_stat;
	pt.x = lp.x;
	pt.y = -lp.y;
	t=lp.m_time;
	if(auto  p = m_monkeyPerson.lock() )
	{
		if(p->is_on_bus())
		{
			m_stat = 7;
			pt = p->getPoint(t);	
			log_info("getpoint_oncar:%d,%lld",m_id,t);
		}
	}
	return pt;
}