/*
 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
 * SPDX-License-Identifier: BSD-3-Clause
 */

// Autogenerated by Thrift Compiler (0.14.1-yarped)
//
// This is an automatically generated file.
// It could get re-generated if the ALLOW_IDL_GENERATION flag is on.

#include <MobileBaseVelocityControlRPC.h>

#include <yarp/os/idl/WireTypes.h>

class MobileBaseVelocityControlRPC_applyVelocityCommandRPC_helper :
        public yarp::os::Portable
{
public:
    explicit MobileBaseVelocityControlRPC_applyVelocityCommandRPC_helper(const double x_vel, const double y_vel, const double theta_vel, const double timeout);
    bool write(yarp::os::ConnectionWriter& connection) const override;
    bool read(yarp::os::ConnectionReader& connection) override;

    double m_x_vel;
    double m_y_vel;
    double m_theta_vel;
    double m_timeout;

    thread_local static bool s_return_helper;
};

thread_local bool MobileBaseVelocityControlRPC_applyVelocityCommandRPC_helper::s_return_helper = {};

MobileBaseVelocityControlRPC_applyVelocityCommandRPC_helper::MobileBaseVelocityControlRPC_applyVelocityCommandRPC_helper(const double x_vel, const double y_vel, const double theta_vel, const double timeout) :
        m_x_vel{x_vel},
        m_y_vel{y_vel},
        m_theta_vel{theta_vel},
        m_timeout{timeout}
{
    s_return_helper = {};
}

bool MobileBaseVelocityControlRPC_applyVelocityCommandRPC_helper::write(yarp::os::ConnectionWriter& connection) const
{
    yarp::os::idl::WireWriter writer(connection);
    if (!writer.writeListHeader(5)) {
        return false;
    }
    if (!writer.writeTag("applyVelocityCommandRPC", 1, 1)) {
        return false;
    }
    if (!writer.writeFloat64(m_x_vel)) {
        return false;
    }
    if (!writer.writeFloat64(m_y_vel)) {
        return false;
    }
    if (!writer.writeFloat64(m_theta_vel)) {
        return false;
    }
    if (!writer.writeFloat64(m_timeout)) {
        return false;
    }
    return true;
}

bool MobileBaseVelocityControlRPC_applyVelocityCommandRPC_helper::read(yarp::os::ConnectionReader& connection)
{
    yarp::os::idl::WireReader reader(connection);
    if (!reader.readListReturn()) {
        return false;
    }
    if (!reader.readBool(s_return_helper)) {
        reader.fail();
        return false;
    }
    return true;
}

class MobileBaseVelocityControlRPC_getLastVelocityCommandRPC_helper :
        public yarp::os::Portable
{
public:
    explicit MobileBaseVelocityControlRPC_getLastVelocityCommandRPC_helper();
    bool write(yarp::os::ConnectionWriter& connection) const override;
    bool read(yarp::os::ConnectionReader& connection) override;

    thread_local static return_getLastVelocityCommand s_return_helper;
};

thread_local return_getLastVelocityCommand MobileBaseVelocityControlRPC_getLastVelocityCommandRPC_helper::s_return_helper = {};

MobileBaseVelocityControlRPC_getLastVelocityCommandRPC_helper::MobileBaseVelocityControlRPC_getLastVelocityCommandRPC_helper()
{
}

bool MobileBaseVelocityControlRPC_getLastVelocityCommandRPC_helper::write(yarp::os::ConnectionWriter& connection) const
{
    yarp::os::idl::WireWriter writer(connection);
    if (!writer.writeListHeader(1)) {
        return false;
    }
    if (!writer.writeTag("getLastVelocityCommandRPC", 1, 1)) {
        return false;
    }
    return true;
}

bool MobileBaseVelocityControlRPC_getLastVelocityCommandRPC_helper::read(yarp::os::ConnectionReader& connection)
{
    yarp::os::idl::WireReader reader(connection);
    if (!reader.readListReturn()) {
        return false;
    }
    if (!reader.read(s_return_helper)) {
        reader.fail();
        return false;
    }
    return true;
}

// Constructor
MobileBaseVelocityControlRPC::MobileBaseVelocityControlRPC()
{
    yarp().setOwner(*this);
}

bool MobileBaseVelocityControlRPC::applyVelocityCommandRPC(const double x_vel, const double y_vel, const double theta_vel, const double timeout)
{
    MobileBaseVelocityControlRPC_applyVelocityCommandRPC_helper helper{x_vel, y_vel, theta_vel, timeout};
    if (!yarp().canWrite()) {
        yError("Missing server method '%s'?", "bool MobileBaseVelocityControlRPC::applyVelocityCommandRPC(const double x_vel, const double y_vel, const double theta_vel, const double timeout)");
    }
    bool ok = yarp().write(helper, helper);
    return ok ? MobileBaseVelocityControlRPC_applyVelocityCommandRPC_helper::s_return_helper : bool{};
}

return_getLastVelocityCommand MobileBaseVelocityControlRPC::getLastVelocityCommandRPC()
{
    MobileBaseVelocityControlRPC_getLastVelocityCommandRPC_helper helper{};
    if (!yarp().canWrite()) {
        yError("Missing server method '%s'?", "return_getLastVelocityCommand MobileBaseVelocityControlRPC::getLastVelocityCommandRPC()");
    }
    bool ok = yarp().write(helper, helper);
    return ok ? MobileBaseVelocityControlRPC_getLastVelocityCommandRPC_helper::s_return_helper : return_getLastVelocityCommand{};
}

// help method
std::vector<std::string> MobileBaseVelocityControlRPC::help(const std::string& functionName)
{
    bool showAll = (functionName == "--all");
    std::vector<std::string> helpString;
    if (showAll) {
        helpString.emplace_back("*** Available commands:");
        helpString.emplace_back("applyVelocityCommandRPC");
        helpString.emplace_back("getLastVelocityCommandRPC");
        helpString.emplace_back("help");
    } else {
        if (functionName == "applyVelocityCommandRPC") {
            helpString.emplace_back("bool applyVelocityCommandRPC(const double x_vel, const double y_vel, const double theta_vel, const double timeout) ");
        }
        if (functionName == "getLastVelocityCommandRPC") {
            helpString.emplace_back("return_getLastVelocityCommand getLastVelocityCommandRPC() ");
        }
        if (functionName == "help") {
            helpString.emplace_back("std::vector<std::string> help(const std::string& functionName = \"--all\")");
            helpString.emplace_back("Return list of available commands, or help message for a specific function");
            helpString.emplace_back("@param functionName name of command for which to get a detailed description. If none or '--all' is provided, print list of available commands");
            helpString.emplace_back("@return list of strings (one string per line)");
        }
    }
    if (helpString.empty()) {
        helpString.emplace_back("Command not found");
    }
    return helpString;
}

// read from ConnectionReader
bool MobileBaseVelocityControlRPC::read(yarp::os::ConnectionReader& connection)
{
    yarp::os::idl::WireReader reader(connection);
    reader.expectAccept();
    if (!reader.readListHeader()) {
        reader.fail();
        return false;
    }

    std::string tag = reader.readTag();
    bool direct = (tag == "__direct__");
    if (direct) {
        tag = reader.readTag();
    }
    while (!reader.isError()) {
        if (tag == "applyVelocityCommandRPC") {
            double x_vel;
            double y_vel;
            double theta_vel;
            double timeout;
            if (!reader.readFloat64(x_vel)) {
                reader.fail();
                return false;
            }
            if (!reader.readFloat64(y_vel)) {
                reader.fail();
                return false;
            }
            if (!reader.readFloat64(theta_vel)) {
                reader.fail();
                return false;
            }
            if (!reader.readFloat64(timeout)) {
                reader.fail();
                return false;
            }
            MobileBaseVelocityControlRPC_applyVelocityCommandRPC_helper::s_return_helper = applyVelocityCommandRPC(x_vel, y_vel, theta_vel, timeout);
            yarp::os::idl::WireWriter writer(reader);
            if (!writer.isNull()) {
                if (!writer.writeListHeader(1)) {
                    return false;
                }
                if (!writer.writeBool(MobileBaseVelocityControlRPC_applyVelocityCommandRPC_helper::s_return_helper)) {
                    return false;
                }
            }
            reader.accept();
            return true;
        }
        if (tag == "getLastVelocityCommandRPC") {
            MobileBaseVelocityControlRPC_getLastVelocityCommandRPC_helper::s_return_helper = getLastVelocityCommandRPC();
            yarp::os::idl::WireWriter writer(reader);
            if (!writer.isNull()) {
                if (!writer.writeListHeader(4)) {
                    return false;
                }
                if (!writer.write(MobileBaseVelocityControlRPC_getLastVelocityCommandRPC_helper::s_return_helper)) {
                    return false;
                }
            }
            reader.accept();
            return true;
        }
        if (tag == "help") {
            std::string functionName;
            if (!reader.readString(functionName)) {
                functionName = "--all";
            }
            auto help_strings = help(functionName);
            yarp::os::idl::WireWriter writer(reader);
            if (!writer.isNull()) {
                if (!writer.writeListHeader(2)) {
                    return false;
                }
                if (!writer.writeTag("many", 1, 0)) {
                    return false;
                }
                if (!writer.writeListBegin(BOTTLE_TAG_INT32, static_cast<uint32_t>(help_strings.size()))) {
                    return false;
                }
                for (const auto& help_string : help_strings) {
                    if (!writer.writeString(help_string)) {
                        return false;
                    }
                }
                if (!writer.writeListEnd()) {
                    return false;
                }
            }
            reader.accept();
            return true;
        }
        if (reader.noMore()) {
            reader.fail();
            return false;
        }
        std::string next_tag = reader.readTag();
        if (next_tag == "") {
            break;
        }
        tag.append("_").append(next_tag);
    }
    return false;
}
