#include "nv_message_converter.hpp"

NvMessageConverter::NvMessageConverter() {
    const auto& config = ConfigManager::get_instance().get_config();

    msgconv_config_file = config["msgconv"]["msgconv_config_file"];
    frame_interval = config["msgconv"]["msgconv_frame_interval"];
    payload_generation_library =
        config["msgconv"]["payload_generation_library"];
}

bool NvMessageConverter::create_message_converter() {
    msgconv = gst_element_factory_make("nvmsgconv", "nvmsg-converter");
    g_object_set(G_OBJECT(msgconv), "msg2p-lib",
                 payload_generation_library.c_str(), NULL);
    g_object_set(G_OBJECT(msgconv), "config", msgconv_config_file.c_str(),
                 NULL);
    g_object_set(G_OBJECT(msgconv), "payload-type", 0,
                 NULL);  // 0 = DeepStream schema, 1 = minimal schema
    g_object_set(G_OBJECT(msgconv), "msg2p-newapi", 0,
                 NULL);  // use new API; If you want to send images, please set
                         // the "payload-type: 1" and "msg2p-newapi: 1"
    // msg2p-newapi: TRUE for DeepStream 6.x+ (recommended).
    g_object_set(G_OBJECT(msgconv), "frame-interval", frame_interval, NULL);

    // g_object_set(G_OBJECT(msgconv),
    //             "config", "dstest5_msgconv.cfg",  // message schema config
    //             file "payload-type", 0, "msg2p-newapi", TRUE,             //
    //             use new API NULL);

    // g_object_set (G_OBJECT (msgconv), "config", "dstest4_msgconv_config.yml",
    // NULL); RETURN_ON_PARSER_ERROR(nvds_parse_msgconv (msgconv, argv[1],
    // "msgconv")); msg2p_meta = ds_test4_parse_meta_type(argv[1], "msgconv");
    // g_print("msg2p_meta = %d\n", msg2p_meta);

    if (!msgconv) {
        g_printerr("Unable to create msgconv.Exiting.");
        return false;
    }
    return true;
}