#pragma once
#include "frame_completion.hpp"
#include "pose_decode.hpp"
#include <sstream>
#include <iomanip>
#include <locale>

namespace contextra {
inline bool publishPose(NvDsBatchMeta* batch, NvDsFrameMeta* frame, guint component,
                        const std::vector<PoseDetection>& poses) {
    if (poses.size()>100) return false;
    std::ostringstream json;
    json.imbue(std::locale::classic());
    json << std::setprecision(9) << "{\"schema\":1,\"component\":" << component
         << ",\"frame\":" << frame->frame_num << ",\"pts\":" << frame->buf_pts << ",\"poses\":[";
    bool first=true;
    for (const auto& p:poses) {
        if (!first) json << ',';
        first=false;
        json << "{\"score\":" << p.score << ",\"box\":[";
        for(int j=0;j<4;++j) { if(j) json << ','; json << p.box[j]; }
        json << "],\"joints\":[";
        for(int j=0;j<17;++j) {
            if(j) json << ',';
            json << '[' << p.joints[j][0] << ',' << p.joints[j][1] << ',' << p.joints[j][2] << ']';
        }
        json << "]}";
    }
    json << "]}";
    auto* meta=nvds_acquire_user_meta_from_pool(batch);
    if (!meta) return false;
    meta->user_meta_data=g_strdup(json.str().c_str());
    meta->base_meta.meta_type=nvds_get_user_meta_type(const_cast<char*>("CONTEXTRA.RFDETR.POSE.V1"));
    meta->base_meta.copy_func=copyCompletion;
    meta->base_meta.release_func=releaseCompletion;
    nvds_add_user_meta_to_frame(frame,meta);
    return true;
}
}
