#ifndef MODULE_GEOFAULT_H
#define MODULE_GEOFAULT_H

/**
 * @brief 简要说明
 * @author 戴月腾
 * @date 2019-01-15
 */

#include <unordered_map>
#include<vector>
#include "module_singleton_base.h"
#include "card.h"
#include"area.h"
#include"config_file.h"

/**
 * @brief 当采煤机和掘进机有数据点过来的时候，判断当前点与地址断层坐标点的距离d。如d<设置的阈值，则告警，d>阈值则取消。
 */
class module_geofault:public singleton_base<module_geofault>
{
private:
    friend class singleton_base<module_geofault>;
    module_geofault()
    {
    }

public:
    void on_enter(std::shared_ptr<card_location_base> card_ptr, int index);
    void on_hover(std::shared_ptr<card_location_base> card_ptr, int index);
    void on_leave(std::shared_ptr<card_location_base> card_ptr, int index);

    void init(config_file& config)
    {
        _geofault_count_limit = std::stoi(config.get("service.geofault_count_limit","10"));
    }

    void init_geofault_from_db();

private:
    bool _is_near(std::shared_ptr<area>& area_ptr, double x, double y, double& out_dist);

    static std::vector<std::string> split(const std::string& str, char tag)
    {
        std::vector<std::string>  arr;
        std::string subStr;

        for(size_t i = 0; i < str.length(); i++)
        {
            if(tag == str[i])
            {
                if(!subStr.empty())
                {
                    arr.push_back(subStr);
                    subStr.clear();
                }
            }
            else
            {
                subStr.push_back(str[i]);
            }
        }

        if(!subStr.empty())
        {
            arr.push_back(subStr);
        }

        return arr;
    }

    static std::string points2str(std::vector<point>& points)
    {
        std::string str;
        for(auto&p : points)
        {
            char ch[64] = {0};
            sprintf(ch, "x=%f,y=%f;", p.x, p.y);
            str.append(ch);
        }

        return str;
    }

private:
    //area_id: points
    std::unordered_map<int, std::vector<point>> _area_geofault_map;

    int _geofault_count_limit;
};

#endif // MODULE_GEOFAULT_H
