/**
 * 分站数据结构 site_list
 */
#ifndef _ANT_LIST_HPP_
#define _ANT_LIST_HPP_

#include <math.h>
#include <array>
#include <deque>
#include <tuple>
#include <time.h>
#include <sstream>
#include <memory>
#include <memory.h>
#include <algorithm>
#include <sstream>
#include "log.h"
#include "line.h"
#include "point.h"
#include "write-copy.h"
#include"net-service.h"
#include"common.h"
class client;
class area;
struct path
{
    std::array<line_v,2>	m_line;
    path()
    {
    }

    std::string to_str() const
    {
        std::stringstream ss;
        for(int i=0;i<2;i++)
        {
            ss<<"line:" <<m_line[i].to_string()<<"slope:"<<m_line[i][0].z<< " cos:"<<m_line[i].cos()<<" sin:"<<m_line[i].sin()<<" | ";
        }
        return ss.str();
    }
    bool valid() const
    {
        return !m_line[0].empty();
    }
    line_v & operator[](int i)
    {
        return m_line[i];
    }
    const line_v & operator[](int i) const
    {
        return m_line[i];
    }
};
//?
struct algo_config
{
    const char*desc;
    int min_msg_cnt;
    int best_msg_cnt;
    double min_wait_time;
    double max_wait_time;
};
//
struct ant :point
{
    std::array<path,2> m_path;
    int m_id;

    path & operator[](int i)
    {
        return m_path[i];
    }

    const path & operator[](int i) const
    {
        return m_path[i];
    }

    size_t size() const
    {
        return m_path.size();
    }

	void set_path(const std::vector<line_v>&v_line);
	void set_path(const std::vector<line_v>&v_line,std::vector<line_v>::const_iterator it);
    std::vector<point> getsol(const double &dist) const;
};

struct site:point,std::enable_shared_from_this<site>
{
    static algo_config g_config[];
    int      m_algo;			//TOF:0,TDOA:1
    int      m_num_dims;	    //1维:0,2维:1,3维:2
    double  m_scale = 2.0;  // 地图比例尺
    point    m_position;
    mutable double m_height=1.5;
    std::array<ant,2> m_ant;
    mutable double m_ant_dist=0;
    mutable double m_ant_dist_sum_new=0;
    mutable int m_ant_dist_cnt_new=0;
    ///分站位置 READER_TYPE_ID
    int m_reader_type_id = 0;
    int m_map_id = 0;
    int m_area_id = 0;
    /// 设备类型,分站、通信分站、交通灯等
    int m_device_type_id=0;
    /// 指定分站定位类型:一维定位,二维定位,三维定位
    int m_dimension=0;

    std::shared_ptr<client> m_clt=nullptr;
    std::shared_ptr<area> m_area=nullptr;

	time_t m_time;
//  time_t m_rec_time;              // 接收数据的时间
//  time_t m_lost_time;		        // 丢失时间
//  time_t m_last_send_time;	    // 最后向前端发送时间
	char m_timestamp[8]={0};

    int m_id{-1};
//  int  m_tick_count;			    // 分站发生消息的计数器

//  unsigned char m_reader_dir;		// 分站方向,对于大小分站适用
//  unsigned char m_relay_counts;		// 大小分站经过中继数
    bool m_power_check_enable=false;//该分站是否启用交流掉电检测功能(告警功能)
    bool m_power_ac_down=false;     //false=交流电供电状态
    bool m_path_empty;

	std::array<int,32>	m_timeoff_count;

    site(int id=-1);
	bool check_timestamp(const char*tm_buf)
	{
		//秒、分、时、天、周、月、年

		struct tm t={0};
		

		char buf[6];
		buf[0]=tm_buf[6];
		buf[1]=tm_buf[5];
		buf[2]=tm_buf[3];
		buf[3]=tm_buf[2];
		buf[4]=tm_buf[1];
		buf[5]=tm_buf[0];

		t.tm_year=buf[0]+100;
		t.tm_mon=buf[1]-1;
		t.tm_mday=buf[2];
		t.tm_hour=buf[3];
		t.tm_min=buf[4];
		t.tm_sec=buf[5];

		time_t now=time(0);
		time_t site_tm=mktime(&t);
		int off=abs(now-site_tm);

		if(off>0)
		{
			off=log(off)/log(2);
		}

		m_timeoff_count[off]++;

		if(now % 300 ==0 )
		{
			std::ostringstream stm;
			for(uint32_t i=0;i<m_timeoff_count.size();i++)
			{
				if(m_timeoff_count[i]==0)
					continue;

				stm<<"("<<i<<":"<<m_timeoff_count[i]<<"),";
			}

			log_info("site-time-stat:%s",stm.str().c_str());
		}

		if(memcmp(m_timestamp,buf,6)<=0)
		{
			memcpy(m_timestamp,buf,6);
			return true;
		}
		return false;
	}


    int index()const;
    const algo_config&config()const;
    int id()const
    {
        return m_id;
	}
    void set_client(const std::shared_ptr<client>& clt)
    {
		if(m_clt != clt)
			m_clt = clt;
		m_time=time(0);
    }

    std::shared_ptr<client> get_client()
    {
        return m_clt;
    }

    bool is_up_site()
    {
        return READER_TYPE_ID_UP == m_reader_type_id;
    }
    void create_area();
    const std::shared_ptr<area> &get_area()const{return m_area;}
    point get_dstp(const point pt) const
    {
        point tmp;
        for(const auto & p : m_ant[0].m_path)
        {
            for(int i=0;i<2;i++)
            {
                if(!p[i].empty())
                {
                    if(p[i].contain(pt,0.01))
                    {
                        //if(i==0)
                        // return *this;
                        //else
                        tmp = p[i][0];
                    }
                }
            }
        }
        return tmp;
    }

    void count_ant_dist(double dist_tof1, double dist_tof2)const
    {
        if(dist_tof1<10 || dist_tof2<10)
            return;
        double dist = fabs(dist_tof1 - dist_tof2);
        if(dist>5)
            return;
        m_ant_dist_sum_new += dist;
        m_ant_dist_cnt_new++;

        if(m_ant_dist_cnt_new >= 2500)
        {
            m_ant_dist = m_ant_dist_sum_new / m_ant_dist_cnt_new;
            m_ant_dist_sum_new = 0;
            m_ant_dist_cnt_new = 0;
        }
    }

    double ant_dist()const
    {
        return m_ant[0].dist(m_ant[1]);
    }

    bool is_path_empty()const
    {
        return m_path_empty;
    }

    bool have_valid_path()const
    {
        return m_id != -1 && ant_dist() > 0.1;
    }

    std::string to_string()const
    {
        std::stringstream ss;
        ss<<"site_id:"<<m_id<<"x:"<<x<<" y: "<<y<<" scale:"<<m_scale;
        for(const auto&a:m_ant)
        {
            ss<<"<";
            for(const auto&p:a.m_path)
            {
                ss<<"{"<<p.to_str()<<"}";
            }
            ss<<">";
        }
        return ss.str();
    }

    const point&path(int i)const
    {
        static point p;
        if(i>=(int)m_ant[0].m_path.size())
            return p ;
        return m_ant[0].m_path[i].m_line[0][1];
    }

	void set_path(const std::vector<line_v>&v_line);
    std::vector<point> solving(int ant_id, double dist)const
    {
        const ant &a = m_ant[ant_id];
        if(dist<50 && dist>0)
        {
            if(dist<m_height)
            {
                m_height=dist;
                dist=0;
            }
            else
            {
                dist=sqrt(dist*dist-m_height*m_height);
            }
        }
        return std::move(a.getsol(dist));
    }

    ant&operator[](int i)
    {
        return m_ant[i];
    }

    const ant&operator[](int i) const
    {
        return m_ant[i];
    }

    void set_ex()
    {
        if(-1 == m_id)
        {
            return;
        }

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

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

    void delete_antenna(int antidx)
    {
        m_ant[antidx].set(point(0,0));
    }

/*

	处理分站供电状态,交流供电时,ac_down=false,直流供电时,ac_down=true

	目前只有大分站实现了这个功能,并且井下安装时是否接入了该电信号也不确定
	,所以需要有张表定义某个ID是否需要告警

*/

	void on_power_status(bool ac_down);//电源状态
};
struct visit_site_status:visitor<std::shared_ptr<site>>
{
	bool visit(std::shared_ptr<site> s);
};
struct sit_list:single_base<sit_list,int,std::shared_ptr<site>>
{
    void load()
    {
        read_sit_list(-1);
        read_ant_path(-1);
    }
    ///id=-1为初始化所有
    void load_from_db(int id=-1)
    {
        init_site(id);
        load();
    }
    void read_sit_list(int id);
    void read_ant_path(int id);
    void init_site(int id);
};
#endif