package org.texttechnologylab.project.gruppe_05_1.rest;

import io.javalin.http.Context;
import io.javalin.openapi.*;
import org.texttechnologylab.project.gruppe_05_1.database.MongoPprUtils;
import org.texttechnologylab.project.gruppe_05_1.domain.html.ParlamentarierDetails;
import org.texttechnologylab.project.gruppe_05_1.domain.speech.Protocol;
import org.texttechnologylab.project.gruppe_05_1.domain.speech.SpeechMetaData;
import org.texttechnologylab.project.gruppe_05_1.util.GeneralUtils;
import org.texttechnologylab.project.gruppe_05_1.xml.speeches.Interfaces.Comment;
import org.texttechnologylab.project.gruppe_05_1.xml.speeches.Interfaces.Speech;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SpeechController {
    /**
     * Liste alle Reden eines Parlamentariers an
     * @param ctx Javalin Context
     */
    @OpenApi(
            summary = "Liste alle Reden eines Parlamentariers an",
            description = "Liste alle Reden eines Parlamentariers an",
            operationId = "listSpeeches",
            path = "/reden/{id}",
            methods = HttpMethod.GET,
            tags = {"Rede"},
            pathParams = {
                    @OpenApiParam(name = "id", description = "id des Parlamentariers", required = true),
            },
            responses = {
                    @OpenApiResponse(status = "200", content = {@OpenApiContent(from = Speech[].class)})
            })
    public static void listSpeeches(Context ctx) {
        String parlamentarierId = ctx.pathParam("id");

        ParlamentarierDetails p = MongoPprUtils.getParlamentarierDetailsByID(parlamentarierId);
        List<SpeechMetaData> speechMetaDataList = MongoPprUtils.getSpeechesMetadataForSeaker(parlamentarierId);

        Map<String, Object> attributes = new HashMap<>();
        attributes.put("p", p);
        attributes.put("speechesMetaDataList", speechMetaDataList);
        ctx.render("showSpeechesList.ftl", attributes);
    }

    /**
     * Zeige eine bestimmte Rede des Parlamentariers an
     * @param ctx Javalin Context
     */
    @OpenApi(
            summary = "Zeige eine bestimmte Rede des Parlamentariers an",
            description = "Zeige eine bestimmte Rede des Parlamentariers an",
            operationId = "showSpeech",
            path = "/reden/{id}/{redeID}",
            methods = HttpMethod.GET,
            tags = {"Rede"},
            pathParams = {
                    @OpenApiParam(name = "id", description = "id des Parlamentariers", required = true),
                    @OpenApiParam(name = "redeId", description = "id der Rede", required = true),
            },
            responses = {
                    @OpenApiResponse(status = "200", content = {@OpenApiContent(from = Speech.class)})
            })
    public static void showSpeech(Context ctx) {
        String parlamentarierId = ctx.pathParam("id");
        String redeId = ctx.pathParam("redeId");

        Map<String, Object> attributes = new HashMap<>();

        /*
        Speech rede = MongoPprUtils.getSpeechesById(redeId);
        List<Comment> comments = MongoPprUtils.getCommentsForSpeech(redeId);
        ParlamentarierDetails p = MongoPprUtils.getParlamentarierDetailsByID(parlamentarierId);


        attributes.put("p", p);
        attributes.put("rede", rede);
        attributes.put("comments", comments.size() > 0 ? comments : null);
        Protocol protocol = rede.getProtocol();
        if (protocol == null) {
            attributes.put("date", "(keine Angaben)");
            attributes.put("time", "(keine Angaben)");
        } else {
            LocalDate date = protocol.getDate();
            LocalTime time = protocol.getStarttime();
            if (date == null) {
                attributes.put("date", "(keine Angaben)");
            } else {
                attributes.put("date", GeneralUtils.formatDate(date));
            }
            if (time == null) {
                attributes.put("time", "(keine Angaben)");
            } else {
                attributes.put("time", GeneralUtils.formatTime(time));
            }
        }

         */
        ctx.render("speechWithComments.ftl", attributes);
    }

}
