
#include <memory>
#include <message.h>

#include "card_message_handle.h"
#include "card_person.h"
#include "card_car.h"
#include "config_file.h"
#include "select_tool.h"
#include "module_service/module_mgr.h"
#include "websocket/wsClientMgr.h"
#include "websocket/wsTimerThread.h"
#include "three_rates.h"
#include "his_location.h"

extern config_file config;

card_location_base::card_location_base(std::string type,uint32_t id,uint16_t dis,int16_t t,int32_t deptid,int32_t level_id,uint32_t cid)
	:card(id,dis,t,deptid,level_id,cid)
{
    select_tool_manage::instance()->create_tool(type,m_sel_tool,m_smo_tool);
}

void card_location_base::do_status(int st)
{
    module_mgr::do_status((STATUS_CARD)st, m_id, m_type);
}

void card_location_base::on_location(const std::vector<point>&vp,const std::vector<loc_message> &lm )
{
	loc_point pt = m_sel_tool->select_solution(vp,lm);
	pt.y=-pt.y;
	if(pt.m_useless)
	{
		x = pt.x;
		y = pt.y;

		//Msg m;
		//m.type=m_type;m.x=(int)x;m.y=(int)y;m.cmd=CMD_HANDLE;m.cardid=m_type<<32|m_id;
		//cardMgr::instance()->tryPut(m);
		double acc = lm[0].m_acc;
		log_info("useful:card_id:%d,ct:%d,timestamp:%llu, loc_point,x:%.2f,y:%.2f acc:%.2f",m_id,m_ct,m_time,pt.x,pt.y,acc);
		do_business(lm.front().m_sit, pt, acc);
	}
}

void card_location_base::on_message(zloop<task*> * loop,const message_locinfo&loc,bool is_history)
{
	m_ct = loc.m_card_ct;
	m_time = loc.m_time_stamp;
	m_message_handle->on_message(loop,loc,is_history);
}

//前端推送位置函数.
void card_location_base::upt_card_pos(YA::_CARD_POS_&cp, const point &pt)
{
//	YA::_CARD_POS_ cp;
	cp.x = pt.x;
	cp.y = pt.y;
	cp.z = pt.z;

	cp.Type=m_type;
	cp.ID = m_id;
	cp.speed = abs(ceil(m_speed));
	cp.running_stat = m_stat;
	cp.dept_id = m_deptid;
	cp.display=m_display;
	cp.rec_time=m_time;
	cp.level_id = m_level_id;
	
	swsTimerThrd.upt_card_pos(cp);
}
void card_location_base::del_card_pos()
{
	YA::_CARD_POS_ cp;
	cp.ID = m_id;
	cp.Type=m_type;
	swsTimerThrd.del_card_pos(cp);
}
int  card_location_base::get_stat()
{
	//盲区>呼救>呼叫>超时>超速>正常
    uint64_t now = time(0)*1000;
    if(now-m_time>CARD_LOST_TIME_OUT)
    {
        return STATUS_LOST;
    }
    else if(auto ev_ptr = event_list::instance()->get_event_card(m_id, m_type, ET_CARD_HELP))
    {
        return  (ES_DEAL_HELP == ev_ptr->m_status) ?  STATUS_HELP_DEALED : STATUS_HELP;
    }
    else if(CALL_NONE != get_mine_tool()->m_status_call)
    {
        return  STATUS_CALL;
    }
    else if(event_list::instance()->get_event_card(m_id, m_type,
                is_person()? ET_CARD_AREA_OVER_TIME_PERSON: ET_CARD_AREA_OVER_TIME_VEHICLE))
    {
        return  STATUS_AREA_OVER_TIME;
    }
    else if(event_list::instance()->get_event_card(m_id, m_type, ET_CARD_OVER_SPEED))
    {
        return  STATUS_OVER_SPEED;
    }

    return STATUS_NORMAL;
}

void  card_location_base::clear()
{
//	uint16_t m_display;			//1显示0不显示，往前端推送
    m_speed=0;			//速度
    m_is_attendance=0;	//井上井下状态  0初始状态 1 井上 2 井下
    m_stat=0;			//运动静止状态
    //m_ct;				//ct
    m_time=0;			//时间戳
}
void card_location_base::put_three_rates(card_pos & cp)
{
	cp.rec_time=m_time;cp.type=m_type;cp.id=m_id;
	cp.identifier_id=m_cid;cp.running_stat=m_stat;cp.final_v=m_speed;
	log_info("three_rates:type:%d,id:%d,cid:%d",cp.type,cp.id,cp.identifier_id);
	three_rates::get_instance()->put(cp);
}

bool card_location_base::is_person() const
{
    return tool_other::is_person(m_type);
}

bool card_location_base::is_vehicle() const
{
    return tool_other::is_vehicle(m_type);
}

card_location_base::~card_location_base()
{
}

std::shared_ptr<card_location_base> card_location_base::make_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)
{
	return std::make_shared<person>(type,cardid,needdisplay,t, deptid,level_id,cid,wl,sname,dname);
}

std::shared_ptr<card_location_base> card_location_base::make_car(const 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)
{
	return std::make_shared<car>(type,cardid,needdisplay,t, deptid, categoryid, type_id,level_id,cid);
}


