#include "KMJsonHelper.h"

#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"

/**
 * @brief 打包进度条信息成Json
 *
 * @param refId refId, 如 "org.kde.kclock"
 * @param progress 当前总进度， 如81
 * @param speed 当前速度，如 "2.4 MB/s 00:05"
 * @param timeout 超时时间，目前默认为5秒，但实际上不生效
 * @return std::string
 */
std::string KMJsonHelper::packProgressInfoToJson(const std::string &refId, int progress, const std::string &speed, int timeout)
{
    rapidjson::StringBuffer buffer;
    rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);

    writer.StartObject();
    writer.Key("refId");
    writer.String(refId.c_str());
    writer.Key("progress");
    writer.Int(progress);
    writer.Key("speed");
    writer.String(speed.c_str());
    writer.Key("timeout");
    writer.Int(timeout);
    writer.EndObject();

    return buffer.GetString();
}

/**
 * @brief 将包信息序列化成Json字符串
 *
 * @param name 包名，如 "org.kde.kclock"
 * @param version 包版本，如 "23.04.3"
 * @param size 包大小，如 12363264 字节，开明命令对应 "11.8 MB"
 * @param branch 分支名，如 "stable"
 * @return std::string
 */
std::string KMJsonHelper::packLocalInfoToJson(const std::string &name, const std::string &version, int size, const std::string &branch)
{
    rapidjson::StringBuffer buffer;
    rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);

    writer.StartObject();
    writer.Key("name");
    writer.String(name.c_str());
    writer.Key("version");
    writer.String(version.c_str());
    writer.Key("size");
    writer.Int(size);
    writer.Key("branch");
    writer.String(branch.c_str());
    writer.EndObject();

    return buffer.GetString();
}

/**
 * @brief 将包信息序列化成Json字符串
 *
 * @param name 包名，如 "org.kde.kclock"
 * @param size 包大小，如 12363264 字节，开明命令对应 "11.8 MB"
 * @param branch 分支名，如 "stable"
 * @return std::string
 */
std::string KMJsonHelper::packRemoteInfoToJson(const std::string &name, int size, const std::string &branch)
{
    rapidjson::StringBuffer buffer;
    rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);

    writer.StartObject();
    writer.Key("name");
    writer.String(name.c_str());
    writer.Key("size");
    writer.Int(size);
    writer.Key("branch");
    writer.String(branch.c_str());
    writer.EndObject();

    return buffer.GetString();
}

void KMJsonHelper::unpackFromJson(const std::string &jsonString, std::string &refId, int &progress, std::string &speed, int &timeout)
{
    rapidjson::Document doc;
    doc.Parse(jsonString.c_str());

    if (!doc.IsObject())
    {
        std::cerr << "Invalid JSON format!" << std::endl;
        return;
    }

    if (doc.HasMember("refId") && doc["refId"].IsString())
    {
        refId = doc["refId"].GetString();
    }

    if (doc.HasMember("progress") && doc["progress"].IsInt())
    {
        progress = doc["progress"].GetInt();
    }

    if (doc.HasMember("speed") && doc["speed"].IsString())
    {
        refId = doc["speed"].GetString();
    }

    if (doc.HasMember("timeout") && doc["timeout"].IsInt())
    {
        timeout = doc["timeout"].GetInt();
    }
}
