/**
  * @project shutter_verify
  * @brief   $BRIEF$
  * @author  lz
  * @data    2023/4/13
 **/
#include "lidarsJsonConfig.h"

#include <utility>

lidarsJsonConfig::lidarsJsonConfig(std::string path) {
    m_path = path;
}

bool lidarsJsonConfig::getLidarConfig(int ordinal, lidarsJsonConfig::LidarConfig &lidarconfig) {
    if (m_config.empty() && !update()) {
        return false;
    }

    if (m_config.size() <= ordinal) {
        return false;
    }
    JV_DOUBLE(m_config[ordinal], "angle_min", lidarconfig.angle_min, DEFAULT_DOUBLE)
    JV_DOUBLE(m_config[ordinal], "angle_max", lidarconfig.angle_max, DEFAULT_DOUBLE)
    JV_DOUBLE(m_config[ordinal], "angle_increment", lidarconfig.angle_increment, DEFAULT_DOUBLE)
    JV_DOUBLE(m_config[ordinal], "time_increment", lidarconfig.time_increment, DEFAULT_DOUBLE)
    JV_DOUBLE(m_config[ordinal]["scan_box_limit"], "minx", lidarconfig.scan_box_limit.minx, DEFAULT_DOUBLE)
    JV_DOUBLE(m_config[ordinal]["scan_box_limit"], "maxx", lidarconfig.scan_box_limit.maxx, DEFAULT_DOUBLE)
    JV_DOUBLE(m_config[ordinal]["scan_box_limit"], "miny", lidarconfig.scan_box_limit.miny, DEFAULT_DOUBLE)
    JV_DOUBLE(m_config[ordinal]["scan_box_limit"], "maxy", lidarconfig.scan_box_limit.maxy, DEFAULT_DOUBLE)
    JV_INT(m_config[ordinal], "range_min", lidarconfig.range_min, DEFAULT_INT)
    JV_INT(m_config[ordinal], "range_max", lidarconfig.range_max, DEFAULT_INT)
    JV_INT(m_config[ordinal]["net_config"], "port", lidarconfig.net_config.port, DEFAULT_INT)
    JV_INT(m_config[ordinal]["scan_box_limit"], "dist_limit", lidarconfig.scan_box_limit.dist_limit, DEFAULT_INT)
    JV_STRING(m_config[ordinal]["net_config"], "ip_address", lidarconfig.net_config.ip_address, DEFAULT_STRING)
    return true;
}

bool lidarsJsonConfig::getNetConfig(int ordinal, lidarsJsonConfig::NetConfig &net_config) {
    if (m_config.empty() && !update()) {
        return false;
    }

    if (m_config.size() <= ordinal) {
        return false;
    }

    JV_INT(m_config[ordinal]["net_config"], "port", net_config.port, DEFAULT_INT)
    JV_STRING(m_config[ordinal]["net_config"], "ip_address", net_config.ip_address, DEFAULT_STRING)

    return true;
}

bool lidarsJsonConfig::getScanBoxLimit(int ordinal, lidarsJsonConfig::ScanBoxLimit &scan_box_limit) {
    if (m_config.empty() && !update()) {
        return false;
    }

    if (m_config.size() <= ordinal) {
        return false;
    }

    JV_DOUBLE(m_config[ordinal]["scan_box_limit"], "minx", scan_box_limit.minx, DEFAULT_DOUBLE)
    JV_DOUBLE(m_config[ordinal]["scan_box_limit"], "maxx", scan_box_limit.maxx, DEFAULT_DOUBLE)
    JV_DOUBLE(m_config[ordinal]["scan_box_limit"], "miny", scan_box_limit.miny, DEFAULT_DOUBLE)
    JV_DOUBLE(m_config[ordinal]["scan_box_limit"], "maxy", scan_box_limit.maxy, DEFAULT_DOUBLE)
    JV_INT(m_config[ordinal]["scan_box_limit"], "dist_limit", scan_box_limit.dist_limit, DEFAULT_INT)

    return true;
}

bool lidarsJsonConfig::update() {
    Json::Value config;
    if (!ReadJsonFile(m_path, config)) {
        return false;
    }

    for (int i = 0; i < config.size(); i++) {
        JV_DOUBLE(config[i], "angle_min", m_config[i]["angle_min"], DEFAULT_DOUBLE)
        JV_DOUBLE(config[i], "angle_max", m_config[i]["angle_max"], DEFAULT_DOUBLE)
        JV_DOUBLE(config[i], "angle_increment", m_config[i]["angle_increment"], DEFAULT_DOUBLE)
        JV_DOUBLE(config[i], "time_increment", m_config[i]["time_increment"], DEFAULT_DOUBLE)
        JV_DOUBLE(config[i]["scan_box_limit"], "minx", m_config[i]["scan_box_limit"]["minx"], DEFAULT_DOUBLE)
        JV_DOUBLE(config[i]["scan_box_limit"], "maxx", m_config[i]["scan_box_limit"]["maxx"], DEFAULT_DOUBLE)
        JV_DOUBLE(config[i]["scan_box_limit"], "miny", m_config[i]["scan_box_limit"]["miny"], DEFAULT_DOUBLE)
        JV_DOUBLE(config[i]["scan_box_limit"], "maxy", m_config[i]["scan_box_limit"]["maxy"], DEFAULT_DOUBLE)
        JV_INT(config[i], "range_min", m_config[i]["range_min"], DEFAULT_INT)
        JV_INT(config[i], "range_max", m_config[i]["range_max"], DEFAULT_INT)
        JV_INT(config[i]["net_config"], "port", m_config[i]["net_config"]["port"], DEFAULT_INT)
        JV_INT(config[i]["scan_box_limit"], "dist_limit", m_config[i]["scan_box_limit"]["dist_limit"], DEFAULT_INT)
        JV_STRING(config[i]["net_config"], "ip_address", m_config[i]["net_config"]["ip_address"], DEFAULT_STRING)
    }

    return true;
}

double lidarsJsonConfig::angle_min(int ordinal) {
    if (m_config.empty() && !update()) {
        return DEFAULT_DOUBLE;
    }

    if (m_config.size() <= ordinal) {
        return DEFAULT_DOUBLE;
    }
    return m_config[ordinal]["angle_min"].asDouble();
}

double lidarsJsonConfig::angle_max(int ordinal) {
    if (m_config.empty() && !update()) {
        return DEFAULT_DOUBLE;
    }

    if (m_config.size() <= ordinal) {
        return DEFAULT_DOUBLE;
    }
    return m_config[ordinal]["angle_max"].asDouble();
}

double lidarsJsonConfig::angle_increment(int ordinal) {
    if (m_config.empty() && !update()) {
        return DEFAULT_DOUBLE;
    }

    if (m_config.size() <= ordinal) {
        return DEFAULT_DOUBLE;
    }
    return m_config[ordinal]["angle_increment"].asDouble();
}

double lidarsJsonConfig::time_increment(int ordinal) {
    if (m_config.empty() && !update()) {
        return DEFAULT_DOUBLE;
    }

    if (m_config.size() <= ordinal) {
        return DEFAULT_DOUBLE;
    }
    return m_config[ordinal]["time_increment"].asDouble();
}

int lidarsJsonConfig::range_min(int ordinal) {
    if (m_config.empty() && !update()) {
        return DEFAULT_INT;
    }

    if (m_config.size() <= ordinal) {
        return DEFAULT_INT;
    }
    return m_config[ordinal]["range_min"].asInt();
}

int lidarsJsonConfig::range_max(int ordinal) {
    if (m_config.empty() && !update()) {
        return DEFAULT_INT;
    }

    if (m_config.size() <= ordinal) {
        return DEFAULT_INT;
    }
    return m_config[ordinal]["range_max"].asInt();
}

std::string lidarsJsonConfig::ip_address(int ordinal) {
    if (m_config.empty() && !update()) {
        return DEFAULT_STRING;
    }

    if (m_config.size() <= ordinal) {
        return DEFAULT_STRING;
    }
    return m_config[ordinal]["net_config"]["ip_address"].asString();
}

int lidarsJsonConfig::port(int ordinal) {
    if (m_config.empty() && !update()) {
        return DEFAULT_INT;
    }

    if (m_config.size() <= ordinal) {
        return DEFAULT_INT;
    }
    return m_config[ordinal]["net_config"]["port"].asInt();
}

int lidarsJsonConfig::dist_limit(int ordinal) {
    if (m_config.empty() && !update()) {
        return DEFAULT_INT;
    }

    if (m_config.size() <= ordinal) {
        return DEFAULT_INT;
    }
    return m_config[ordinal]["scan_box_limit"]["dist_limit"].asInt();
}

double lidarsJsonConfig::minx(int ordinal) {
    if (m_config.empty() && !update()) {
        return DEFAULT_DOUBLE;
    }

    if (m_config.size() <= ordinal) {
        return DEFAULT_DOUBLE;
    }
    return m_config[ordinal]["scan_box_limit"]["minx"].asDouble();
}

double lidarsJsonConfig::maxx(int ordinal) {
    if (m_config.empty() && !update()) {
        return DEFAULT_DOUBLE;
    }

    if (m_config.size() <= ordinal) {
        return DEFAULT_DOUBLE;
    }
    return m_config[ordinal]["scan_box_limit"]["maxx"].asDouble();
}

double lidarsJsonConfig::miny(int ordinal) {
    if (m_config.empty() && !update()) {
        return DEFAULT_DOUBLE;
    }

    if (m_config.size() <= ordinal) {
        return DEFAULT_DOUBLE;
    }
    return m_config[ordinal]["scan_box_limit"]["miny"].asDouble();
}

double lidarsJsonConfig::maxy(int ordinal) {
    if (m_config.empty() && !update()) {
        return DEFAULT_DOUBLE;
    }

    if (m_config.size() <= ordinal) {
        return DEFAULT_DOUBLE;
    }
    return m_config[ordinal]["scan_box_limit"]["maxy"].asDouble();
}
