
#include "MapConfig.h"

#include <assert.h>

#include <fstream>

#include "FileSys.h"
#include "ajson.hpp"

using namespace jf;

std::string MapConfig::map_file_dir;
std::string MapConfig::map_use_type;
std::string MapConfig::tag;
double MapConfig::tile_lon_interval;
double MapConfig::tile_lat_interval;
double MapConfig::node_interpolate;
double MapConfig::gps_rect_lon_metres_half;
double MapConfig::gps_rect_lat_metres_half;
double MapConfig::route_rect_lon;
double MapConfig::route_rect_lat;
int MapConfig::qt_max_level;
int MapConfig::algorithm_path_len;
int MapConfig::algorithm_condidate_max;
float MapConfig::algorithm_change_road_punish_div;
double MapConfig::algorithm_change_direction_punish_range_max;
std::string MapConfig::gps_file_path;
int MapConfig::profiler_interval;
std::string MapConfig::logDir;
int MapConfig::serialize_type_json1_binary2 = 1;
bool MapConfig::tile_encrypt;
MapConfig::sdConfig MapConfig::sd_config;
MapConfig::hdConfig MapConfig::hd_config;
std::string MapConfig::sdConfig::save_dir_json;
std::string MapConfig::sdConfig::save_dir_binary;
std::string MapConfig::hdConfig::save_dir_json;
std::string MapConfig::hdConfig::save_dir_binary;
std::string MapConfig::road_plan_file_dir;
// double MapConfig::road_plan_min_waypoint_dis;
// int MapConfig::road_plan_max_links;
// int MapConfig::road_plan_max_failes;
std::string MapConfig::projectDir;
std::string MapConfig::sdid2hdid_path;
std::string MapConfig::map_road_filer_logic;

AJSON(MapConfig::sdConfig, save_dir_json, save_dir_binary);

AJSON(MapConfig::hdConfig, save_dir_json, save_dir_binary);

AJSON(MapConfig, map_file_dir, map_use_type, tag, tile_lon_interval, tile_lat_interval, node_interpolate, gps_rect_lon_metres_half, gps_rect_lat_metres_half, route_rect_lon, route_rect_lat, qt_max_level, algorithm_path_len, algorithm_condidate_max,
      algorithm_change_road_punish_div, algorithm_change_direction_punish_range_max, sd_config, hd_config, gps_file_path, profiler_interval, logDir, serialize_type_json1_binary2, tile_encrypt, road_plan_file_dir, /*road_plan_min_waypoint_dis,
                                                                                                                                                                                   road_plan_max_links, road_plan_max_failes,*/
      sdid2hdid_path, map_road_filer_logic);

void MapConfig::Init(const std::string &inProjectDir, const std::string &configJsonPath) {
    std::ifstream in = {};
    in.open((inProjectDir + configJsonPath).c_str());
    std::string s((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
    assert(in.good());

    MapConfig c;
    std::cout << s;
    ajson::load_from_buff(c, s.c_str());

    map_file_dir = inProjectDir + c.map_file_dir;
    sdConfig::save_dir_json = inProjectDir + c.sd_config.save_dir_json;
    sdConfig::save_dir_binary = inProjectDir + c.sd_config.save_dir_binary;
    hdConfig::save_dir_json = inProjectDir + c.hd_config.save_dir_json;
    hdConfig::save_dir_binary = inProjectDir + c.hd_config.save_dir_binary;
    gps_file_path = inProjectDir + c.gps_file_path;
    // road_plan_file_dir = inProjectDir + c.road_plan_file_dir;
    sdid2hdid_path = inProjectDir + c.sdid2hdid_path;

    projectDir = inProjectDir;

    tag = "MapConfig";

    //   jf_log_info("--------");
    //   jf_log_info("{}", MapConfig::sdConfig::save_dir_json);
    //   jf_log_info("{}", MapConfig::sdConfig::save_dir_binary);
    //   jf_log_info("{}", MapConfig::hdConfig::save_dir_json);
    //   jf_log_info("{}", MapConfig::hdConfig::save_dir_binary);
    //   jf_log_info("--------");

    return;
}