#include "config.h"

#include <cassert>

#ifdef USE_BOOST
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
#else
#include <filesystem>
namespace fs = std::filesystem;
#endif

ClientConfig::ClientConfig()
{
}

ClientConfig::~ClientConfig() = default;

bool ClientConfig::baseInit()
{
    fs::path tpath(COfPath::get_config_dir("transm", true));
    config_dir_ = tpath.string();
    config_path_ = tpath.append("transm.ini").string();
    if (!fs::exists(config_path_)) {
        gen_default_ini(config_path_);
    }
    SI_Error ret = ini_handle_.LoadFile(config_path_.c_str());
    if (ret != SI_OK) {
        TLOGE("Load Ini [{}] Failed.", config_path_);
        return false;
    }
    init_ = true;
    return true;
}

bool ClientConfig::read_ini(std::vector<TransmSet>& set)
{
    assert(init_ == true);
    long groups = ini_handle_.GetLongValue("BASE", "GROUPS");
    if (groups < 1) {
        TLOGE("GROUPS num < 1.");
        return false;
    }
    set.clear();
    for (long i = 0; i < groups; ++i) {
        std::string key = "GROUP" + std::to_string(i);
        TransmSet ts;
        ts.group = key;
        ts.grp_id = i;
        ts.ip = ini_handle_.GetValue(key.c_str(), "IP");
        ts.port = ini_handle_.GetLongValue(key.c_str(), "PORT");
        if (!ini_handle_.KeyExists(key.c_str(), "COMMENT")) {
            ts.comment = "default";
        } else {
            ts.comment = ini_handle_.GetValue(key.c_str(), "COMMENT");
        }
        set.push_back(ts);
    }
    return true;
}

long ClientConfig::have_ini(const std::vector<TransmSet>& set, const std::string& ip, long port)
{
    long id = -1;
    for (const auto& item : set) {
        if (item.ip == ip && item.port == port) {
            id = item.grp_id;
            break;
        }
    }
    return id;
}

bool ClientConfig::write_ini(const std::vector<TransmSet>& set)
{
    assert(init_ == true);
    for (size_t start = 0; start < set.size(); ++start) {
        std::string key = "GROUP" + std::to_string(start);
        ini_handle_.SetValue(key.c_str(), "IP", set[start].ip.c_str());
        ini_handle_.SetLongValue(key.c_str(), "PORT", set[start].port);
        ini_handle_.SetValue(key.c_str(), "COMMENT", set[start].comment.c_str());
    }
    ini_handle_.SaveFile(config_path_.c_str());
    return true;
}

long ClientConfig::append_ini(const std::string& ip, long port, const std::string& comment)
{
    assert(init_ == true);
    long id = -1;
    std::vector<TransmSet> set;
    if (!read_ini(set)) {
        return false;
    }
    id = have_ini(set, ip, port);
    if (id >= 0) {
        std::string node_name = "GROUP" + std::to_string(id);
        ini_handle_.SetValue(node_name.c_str(), "COMMENT", comment.c_str());
        ini_handle_.SaveFile(config_path_.c_str());
        return true;
    }
    id = ini_handle_.GetLongValue("BASE", "GROUPS");
    std::string node_name = "GROUP" + std::to_string(id);
    ini_handle_.SetValue(node_name.c_str(), "IP", ip.c_str());
    ini_handle_.SetLongValue(node_name.c_str(), "PORT", port);
    ini_handle_.SetValue(node_name.c_str(), "COMMENT", comment.c_str());
    ini_handle_.SetLongValue("BASE", "GROUPS", id + 1);
    ini_handle_.SaveFile(config_path_.c_str());
    return id;
}

bool ClientConfig::remove_ini(long num)
{
    assert(init_ == true);
    std::vector<TransmSet> set;
    if (!read_ini(set)) {
        return false;
    }
    set.erase(
        std::remove_if(set.begin(), set.end(), [&num](const TransmSet& item) { return item.grp_id == num; }),
        set.end());
    ini_handle_.Reset();
    ini_handle_.SetLongValue("BASE", "GROUPS", static_cast<long>(set.size()));
    return write_ini(set);
}

bool ClientConfig::get_ini(const std::vector<TransmSet>& set, long num, TransmSet& use)
{
    bool find = false;
    for (const auto& item : set) {
        if (item.grp_id == num) {
            find = true;
            use = item;
            break;
        }
    }
    return find;
}

std::string ClientConfig::get_config_dir() const
{
    return config_dir_;
}

bool ClientConfig::save_last_use(const std::string& ip, long port)
{
    assert(init_ == true);
    ini_handle_.SetValue("Base", "LastUseIP", ip.c_str());
    ini_handle_.SetLongValue("Base", "LastUsePort", port);
    ini_handle_.SaveFile(config_path_.c_str());
    return true;
}

bool ClientConfig::get_last_use(std::string& ip, long& port)
{
    assert(init_ == true);
    if (!ini_handle_.KeyExists("Base", "LastUseIP") || !ini_handle_.KeyExists("Base", "LastUsePort")) {
        TLOGE("Not Found Last Use Record.");
        return false;
    }
    ip = ini_handle_.GetValue("Base", "LastUseIP");
    port = ini_handle_.GetLongValue("Base", "LastUsePort");
    return true;
}

void ClientConfig::gen_default_ini(const std::string& path)
{
    TLOGW("Gen Default Setting Ini in [{}].", path);
    ini_handle_.LoadFile(path.c_str());
    ini_handle_.SetLongValue("BASE", "GROUPS", 1);
    ini_handle_.SetValue("GROUP0", "IP", "127.0.0.1");
    ini_handle_.SetValue("GROUP0", "PORT", "9898");
    ini_handle_.SaveFile(path.c_str());
}