import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServer;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Route;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.client.HttpResponse;
import io.vertx.ext.web.client.WebClient;

import java.util.concurrent.atomic.AtomicReference;

public class NotMigrator {
    final private static int PORT_TDG = 8181;
    final private static int PORT_MY = 8080;
    final private static String HOST = "localhost";
    final private static String TOKEN = "9f226286-e0d8-4125-ad43-b123b4e464da";
    final private static String TYPE = "application/json;charset=utf-8";
    final private static String PATH = "/api/migrate/graphql";

    public static void main(String[] args) throws Exception {
        Vertx vertx = Vertx.vertx();
        HttpServer server = vertx.createHttpServer();
        Router router = Router.router(vertx);
        WebClient client = WebClient.create(vertx);

        JsonObject req = new JsonObject()
                .put("query", "{Book { uuid, book_name, author }}");

        NotMigrator migrator = new NotMigrator();
        router
                .get(PATH)
                .respond(ctx -> Future.succeededFuture(migrator.graphql(req, client)));

        server.requestHandler(router).listen(8080);
    }

    public Future<HttpResponse<Buffer>> graphql(JsonObject request, WebClient client) {
        Promise<HttpResponse<Buffer>> promise = Promise.promise();
        client
                .post("/graphql")
                .sendJson(request, t -> {
                    if (t.succeeded()) {
                        promise.complete(t.result());
                    } else {
                        promise.fail(new Exception("Error while sending request to tarantool", t.cause()));
                    }
                });
        return promise.future();

    }
}
