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

int site::index()const
{
	return m_algo+(m_num_dims<<1);
}

sit_list*sit_list::instance()
{
	static sit_list _impl;
	//_impl.load_from_db();
	return &_impl;
}
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()];
}

const site& sit_list::operator[](int id) const
{
    return m_list[id];
} 
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]);
		if(id>(int)m_list.size())
			continue;

		int antid=atoi(s[2])-1;
		if(antid>=2)
			continue;

		m_list[id].m_id=id;
		m_list[id].m_ant[antid].set(atof(s[4]),-atof(s[5]));
	}

	for(auto&sit:m_list)
	{
		if(sit.m_id==-1)
			continue;

		if(sit.m_ant[0]==sit.m_ant[1])
		{
			printf("%d分站天线坐标相等.\n",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]);
		if(id>(int)m_list.size())
			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]));
        
        if(pid == 0)
        {
            line_v l(p1,p2);
            {
                point px = l.line::projection(m_list[id]);
                m_list[id].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]);
                    m_list[id].m_ant[i].m_path.push_back(p);
                }
            }
        }
        else 
        {
            ant &a = pid<0?m_list[id].m_ant[0]:m_list[id].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)
				 m_list[id].set(p1);
        }
	}
	fclose(fp);

	for(auto&s:m_list)
	{
		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());
        //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 }
};

template<> std::shared_ptr<sit_list> 
single_base<sit_list, int, std::shared_ptr<site>>::m_instance=std::make_shared<sit_list>();
#ifdef _TEST
int main()
{
	log_init("./log.ini");
    sit_list *sl = sit_list::instance();
	sl->load("data_reader_antenna.txt","path_tof.txt");
    (*sl)[219].solving(0,100);
    (*sl)[219].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
