
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "ant.h"

template<> std::shared_ptr<sit_list> 
single_base<sit_list, int, std::shared_ptr<site>>::m_instance=std::make_shared<sit_list>();
int site::index()const
{
	return m_algo+(m_num_dims<<1);
}

site::site(int id)
	:m_algo(0)
	,m_num_dims(0)
    ,m_id(id)
    ,m_path_empty(true)
{

}
const algo_config&site::config()const
{
	return g_config[index()];
}

 
static int str_split(char*s,char**rc)
{
	char**o=rc;

	for(;*s;)
	{
		*o=strtok_r(s,",",&s);
		o++;
	}

	return o-rc;
}

//1, 101, 1, '101-1', 4727, 75, 0, 0, '2017-08-29 10:21:14'
void sit_list::read_sit_list(const char*fname)
{
	FILE*fp=fopen(fname,"r");
	char buf[512];
	int t,id;
	char* s[20];
	while(fgets(buf,sizeof(buf),fp))
	{
		t=str_split(buf,&s[0]);
		if(t<8)
			continue;

		id=atoi(s[1]);

		int antid=atoi(s[2])-1;
		if(antid>=2)
			continue;
		auto tmp = m_instance->get(id);
		if(tmp==nullptr)
		{
			tmp = std::make_shared<site>();
			tmp->m_id=id;
		}
		tmp->m_ant[antid].set(atof(s[4]),-atof(s[5]));
		instance()->add(id,tmp);
		//std_info("211111 id=%dsize = %d",id,instance()->m_map.size());
		//m_list[id].m_id=id;
		//m_list[id].m_ant[antid].set(atof(s[4]),-atof(s[5]));
	}
	
	for(auto&sit_:m_instance->m_map)
	{
		auto & sit = *(sit_.second);
		if(sit.m_id==-1)
			continue;

		if(sit.m_ant[0]==sit.m_ant[1])
		{
			log_warn("%d分站天线坐标相等.",sit.m_id);
		}

		sit.set( (sit.m_ant[0].x+sit.m_ant[1].x)/2,(sit.m_ant[0].y+sit.m_ant[1].y)/2);
	}

	fclose(fp);
}
void sit_list::read_ant_path(const char*fname)
{
	FILE*fp=fopen(fname,"r");
	char buf[512],*p;
	int t,id,pid;
	char* s[20];
	while((p=fgets(buf,sizeof(buf),fp)))
	{
		t=str_split(buf,&s[0]);
		if(t<9)
			continue;

		id=atoi(s[0]);
		auto s_ = m_instance->get(id);
		if(s_==nullptr)
			continue;

		pid=atoi(s[1]);
		if(pid>2)
          continue;
        point p1(atof(s[2]),-atof(s[3]));
        point p2(atof(s[5]),-atof(s[6]));
		auto &sit_ = *s_; 
        if(pid == 0)
        {
            line_v l(p1,p2);
            {
                point px = l.line::projection(sit_);
                sit_.set(px);
                for(int i=0;i<2;i++)
                {
                    path p;
                    p.m_slope[0] = atof(s[8]);
                    p.m_line[0] = line_v(px,l[i]);
                    sit_.m_ant[i].m_path.push_back(p);
                }
            }
        }
        else 
        {
            ant &a = pid<0?sit_.m_ant[0]:sit_.m_ant[1];
            if(a.m_path.size()!=0)
            {
                path &p = a.m_path[0];
                p.m_line[abs(pid)-1] = line_v(p1,p2);
                p.m_slope[abs(pid)-1] = atof(s[8]);
            }
            else
            {
                path p;
                p.m_line[abs(pid)-1] = line_v(p1,p2);
                p.m_slope[abs(pid)-1] = atof(s[8]);
                a.m_path.push_back(p);
            }
            if(abs(pid)==1)
				 sit_.set(p1);
        }
	}
	fclose(fp);

	for(auto&_s:m_instance->m_map)
	{
		auto & s = *(_s.second);
		if(s.m_id==-1)
			continue;
        s.swap();
		if((s.path(0).empty() && s.path(1).empty()))
			continue;
        s.m_path_empty=false; 

        for(auto &a:s.m_ant)
        for(auto &p:a.m_path)
        {
            if(!p.m_line[0].empty())
            {
                point px = p.m_line[0].line::projection(a);
                p.m_line[0]=line_v(px,p.m_line[0][1]);
            }
        }
        //std_info("%s",s.to_string().c_str());
        log_info("%s",s.to_string().c_str());
        //std_info("%f----%f",s.x,s.y);
	}
}



loc_message::loc_message()
	 :m_num_ticks(0)
{
}
int loc_message::tool_index()const
{
	return m_sit.index();
}

algo_config site::g_config[]=
{
	{ "tof-1",  1, 2, 0.1, 1 },
	{ "tdoa-1", 2, 2, 0.1, 1 },
	{ "tof-2",  2, 3, 0.1, 1 },
	{ "tdoa-2", 3, 3, 0.1, 1 },
	{ "tof-3",  3, 4, 0.1, 1 },
	{ "tdoa-3", 4, 4, 0.1, 1 }
};

#ifdef _TEST
int main()
{
	log_init("./log.ini");
    //sit_list *sl = sit_list::instance();
	sit_list::instance()->load("data_reader_antenna.txt","path_tof.txt");
	sit_list::instance()->get(209)->solving(0,100);
	sit_list::instance()->get(209)->solving(1,100.5);
    //std_info("---%d",(*sl)[209].m_ant[0].m_path.size());
    //std_info("---%d",(*sl)[209][0].size());
    //std_info("---%s",(*sl)[209][0][0][0].to_string().c_str());
}
#endif
