package org.texttechnologylab.project.gruppe_05_1.rest;

import freemarker.template.Configuration;
import freemarker.template.TemplateExceptionHandler;
import io.javalin.Javalin;
import io.javalin.http.staticfiles.Location;
import io.javalin.openapi.plugin.OpenApiPlugin;
import io.javalin.openapi.plugin.redoc.ReDocPlugin;
import io.javalin.rendering.template.JavalinFreemarker;
import org.texttechnologylab.project.gruppe_05_1.util.Logger;

import java.io.File;
import java.io.IOException;

import static org.texttechnologylab.project.gruppe_05_1.Main.JAVALIN_STATIC_FILES_DIR;
import static org.texttechnologylab.project.gruppe_05_1.Main.JAVALIN_TEMPLATE_DIR;

public class RESTHandlerOld {

    public void startJavalin() {

        // Javalin Konfiguration (z.B. port)
        JavalinConfig jlConfig = new JavalinConfig();
        int port = jlConfig.getPort();

        // FreeMarker Konfiguration
        Configuration fmConfig = new Configuration(Configuration.VERSION_2_3_33);
        fmConfig.setDefaultEncoding("UTF-8");
        try {
            fmConfig.setDirectoryForTemplateLoading(new File(JAVALIN_TEMPLATE_DIR));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        fmConfig.setLogTemplateExceptions(true);
        fmConfig.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

        // Erzeuge die Javalin app
        Javalin app = Javalin.create(config -> {
                    config.staticFiles.add(JAVALIN_STATIC_FILES_DIR, Location.EXTERNAL);     // momentan nicht benutzt

                    config.fileRenderer(new JavalinFreemarker(fmConfig));

                    config.registerPlugin(new OpenApiPlugin(pluginConfig -> {
                        // Define OpenAPI spec configuration
                        pluginConfig.withDefinitionConfiguration((version, definition) -> {
                            definition.withOpenApiInfo(info -> info.setTitle("Javalin OpenAPI Documentation"));
                        });
                    }));

                    config.registerPlugin(new ReDocPlugin());

                })
                .start(port);
        Logger.info("Javalin app started on http://localhost:" + port);

        // Routes
        // ======

        // Parlamentarier
        app.get("/", ParlamentarierController::getAllParlamentarier);
        app.get("/portfolio/{id}", ParlamentarierController::getParlamentarierDetails);

        // Reden
        app.get("/reden/{id}", SpeechController::listSpeeches);      // zeige Reden eines Parlamentariers an
        app.get("/reden/{id}/{redeId}", SpeechController::showSpeech);      // zeige eine bestimmte Rede des Parlamentariers an

    }
}
