#ifndef AREA_BUSINESS_GEOFAULT
#define AREA_BUSINESS_GEOFAULT

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

#include<area_business.h>
#include <unordered_map>
#include<vector>
#include<area.h>
#include<point.h>

#include"config_file.h"

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

    void on_enter(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data>&ptr);
    void on_hover(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr);
    void on_leave(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr);

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

    static void init_geofault_from_db();

private:
    static 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
    static std::unordered_map<int, std::vector<point>> _area_geofault_map;

    static int _geofault_count_limit;
};

#endif // AREA_BUSINESS_GEOFAULT