/* This file is autogenerated. DO NOT CHANGE. All changes will be lost */

#include "qmltestservice_client.grpc.qpb.h"

namespace qtgrpc::tests {
namespace TestService {
using namespace Qt::StringLiterals;

QmlClient::QmlClient(QObject *parent)
    : Client(parent)
{
}


void QmlClient::testMethod(const qtgrpc::tests::SimpleStringMessage &arg, const QJSValue &callback, const QJSValue &errorCallback,const QGrpcCallOptions &options)
{
    if (!callback.isCallable()) {
        qWarning() << "Unable to call QmlClient::testMethod, callback is not callable";
        return;
    }

    QJSEngine *jsEngine = qjsEngine(this);
    if (jsEngine == nullptr) {
        qWarning() << "Unable to call QmlClient::testMethod, it's only callable from JS engine context";
        return;
    }

    std::shared_ptr<QGrpcCallReply> reply = call("testMethod"_L1, arg, options);
    reply->subscribe(jsEngine, [reply, callback, jsEngine]() {
        if (const auto result = reply->read<qtgrpc::tests::SimpleStringMessage>()) {
            callback.call(QJSValueList{jsEngine->toScriptValue(*result)});
            return;
        }
        QGrpcStatus::StatusCode code = QGrpcStatus::StatusCode::InvalidArgument;
        if (reply->deserializationError() == QAbstractProtobufSerializer::UnexpectedEndOfStreamError)
            code = QGrpcStatus::StatusCode::OutOfRange;
        emit reply->errorOccurred(QGrpcStatus{ code, reply->deserializationErrorString() });
    }, [errorCallback, jsEngine](const QGrpcStatus &status) {
        errorCallback.call(QJSValueList{jsEngine->toScriptValue(status)});
    });
}

} // namespace TestService
} // namespace qtgrpc::tests

