package cz.senslog.analytics.server.ws;

import cz.senslog.analytics.app.Application;
import cz.senslog.analytics.app.PropertyConfig;
import io.vertx.core.Future;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;

import java.time.ZoneId;
import java.util.List;

import static cz.senslog.analytics.server.ws.AuthorizationType.BEARER;
import static cz.senslog.analytics.server.ws.AuthorizationType.NONE;
import static cz.senslog.analytics.server.ws.ContentType.JSON;
import static io.vertx.core.http.HttpHeaders.ACCEPT;
import static io.vertx.core.http.HttpHeaders.CONTENT_TYPE;
import static java.util.stream.Collectors.toList;

public class OpenAPIHandler {

    public static OpenAPIHandler create() {
        return new OpenAPIHandler();
    }

    public void info(RoutingContext rc) {
        boolean authEnable = !PropertyConfig.getInstance().authConfig().getDisabled();
        ContentTypeHandler.createWithSupportedTypes(JSON)
                .accept(rc.request().getHeader(ACCEPT))
                .handle(JSON, type -> rc.response().putHeader(CONTENT_TYPE, type.contentType())
                        .end(JsonObject.of(
                                "name", Application.PROJECT_NAME,
                                "version", Application.COMPILED_VERSION,
                                "build", Application.BUILD_VERSION,
                                "uptime", Application.uptimeFormatted(),
                                "uptimeMillis", Application.uptime(),
                                "authType", authEnable ? BEARER : NONE
                        ).encode()));
    }
}
