package org.texttechnologylab.project.gruppe_05_1.database.domainimp.speeches;

import de.tudarmstadt.ukp.dkpro.core.api.metadata.type.DocumentMetaData;
import org.apache.uima.UIMAException;
import org.apache.uima.fit.factory.JCasFactory;
import org.apache.uima.jcas.JCas;
import org.bson.Document;
import org.texttechnologylab.project.gruppe_05_1.xml.speeches.Impls.Speech_File_Impl;
import org.texttechnologylab.project.gruppe_05_1.xml.speeches.Interfaces.Content;
import org.texttechnologylab.project.gruppe_05_1.xml.speeches.Interfaces.Speech;


import java.util.List;

public class Speech_MongoDB_Impl  extends Speech_File_Impl implements Speech {
    public Speech_MongoDB_Impl(Document mongoDocument, boolean includeContent) {
        super(
                mongoDocument.getInteger("sessionId"),
                mongoDocument.getInteger("agendaItemId"),
                mongoDocument.getInteger("speechId"),
                mongoDocument.getInteger("speakerId"),
                mongoDocument.getString("speakerName"),
                mongoDocument.getString("fraction"),
                mongoDocument.getString("speechKey")
        );

        if (includeContent) {
            for (Document content : (List<Document>) mongoDocument.get("speechContents")) {
                switch (content.getString("type")) {
                    case "line":
                        this.addContent(new Line_MongoDB_Impl(content));
                        break;
                    case "comment":
                        this.addContent(new Comment_MongoDB_Impl(content));
                        break;
                    case "speaker":
                        this.addContent(new Speaker_MongoDB_Impl(content));
                        break;
                    default:
                        throw new IllegalArgumentException("Unknown content type: " + content.getString("type"));
                }
            }
        }
    }

    public String getFullText() {
        StringBuilder fullText = new StringBuilder();
        // Iteriere über alle Inhalte, die bereits in der Rede gespeichert wurden.
        for (Object content : this.getSpeechContents()) {
            if (content instanceof Line_MongoDB_Impl) {
                // Wir gehen davon aus, dass Line_MongoDB_Impl eine Methode getContent() hat, die den Text zurückgibt.
                String lineText = ((Line_MongoDB_Impl) content).getContent();
                if (lineText != null && !lineText.isEmpty()) {
                    fullText.append(lineText).append("\n");
                }
            } else if (content instanceof Comment_MongoDB_Impl) {
                // Wir gehen davon aus, dass Comment_MongoDB_Impl eine Methode getComment() hat, die den Kommentartext liefert.
                String commentText = ((Comment_MongoDB_Impl) content).getComment();
                if (commentText != null && !commentText.isEmpty()) {
                    fullText.append(commentText).append("\n");
                }
            }
            // Inhalte vom Typ "speaker" werden ignoriert.
        }
        return fullText.toString().trim();
    }

    //TODO not going fuckin insane
    public JCas toCas() throws UIMAException {
        JCas jCas = JCasFactory.createJCas();
        jCas.setDocumentText(this.getFullText());
        DocumentMetaData dmd = new DocumentMetaData(jCas);
        dmd.setDocumentId(this.getSpeechKey());;
        dmd.addToIndexes();
        return jCas;
    }

}
