package org.texttechnologylab.project.gruppe_05_1.domain.html;

import org.bson.Document;
import org.texttechnologylab.project.gruppe_05_1.database.MongoDBHandler;
import org.texttechnologylab.project.gruppe_05_1.domain.nlp.NlpInfo;
import org.texttechnologylab.project.gruppe_05_1.domain.nlp.Token;
import org.texttechnologylab.project.gruppe_05_1.domain.nlp.Topic;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.StringJoiner;

public class HtmlSpeech {
    String                  speechKey;
    String                  speakerName;
    String                  fraction;
    List<SpeechContent> content = new ArrayList<>();
    NlpInfo                 nlp = null;

    public HtmlSpeech() {
    }

    public HtmlSpeech(Document doc) {
        setSpeechKey(doc.getString("speechKey"));
        setSpeakerName(doc.getString("speakerName"));
        setFraction(doc.getString("fraction"));

        List<Document> contentDocList = doc.get("speechContents", MongoDBHandler.DOC_LIST_CLASS);
        if (contentDocList == null) {
            setContent(new ArrayList<>());
        } else {
            for (Document contentDoc : contentDocList) {
                addContent(new SpeechContent(contentDoc));
            }
        }

        Document nlpDoc = (Document) doc.get("analysisResults");
        nlp = readNlpInfo(nlpDoc);
    }

    private NlpInfo readNlpInfo(Document nlpDoc) {
        if (nlpDoc == null) return null;
        NlpInfo nlp = new NlpInfo();

        // TODO: HERE
        List<Document> tokensDocs = nlpDoc.get("tokens", MongoDBHandler.DOC_LIST_CLASS);
        nlp.setTokens(Token.readTokensFromMongo(tokensDocs));

        List<Document> sentencesDocs = nlpDoc.get("sentences", MongoDBHandler.DOC_LIST_CLASS);

        List<Document> dependenciesDocs = nlpDoc.get("dependencies", MongoDBHandler.DOC_LIST_CLASS);

        List<Document> namedEntitiesDocs = nlpDoc.get("namedEntities", MongoDBHandler.DOC_LIST_CLASS);

        List<Document> sentimentsDocs = nlpDoc.get("sentiments", MongoDBHandler.DOC_LIST_CLASS);

        List<Document> topicsDocs = nlpDoc.get("topics", MongoDBHandler.DOC_LIST_CLASS);
        nlp.setTopics(Topic.readTopicsFromMongo(topicsDocs));

        // TODO: Video

        return nlp;
    }

    public String getSpeechKey() {
        return speechKey;
    }

    public void setSpeechKey(String speechKey) {
        this.speechKey = speechKey;
    }

    public String getSpeakerName() {
        return speakerName;
    }

    public void setSpeakerName(String speakerName) {
        this.speakerName = speakerName;
    }

    public String getFraction() {
        return fraction;
    }

    public void setFraction(String fraction) {
        this.fraction = fraction;
    }

    public List<SpeechContent> getContent() {
        return content;
    }

    public void setContent(List<SpeechContent> content) {
        this.content = content;
    }

    public void addContent(SpeechContent contentLine) {
        content.add(contentLine);
    }

    public NlpInfo getNlp() {
        return nlp;
    }

    public void setNlp(NlpInfo nlp) {
        this.nlp = nlp;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof HtmlSpeech that)) return false;
        return Objects.equals(speechKey, that.speechKey) && Objects.equals(speakerName, that.speakerName)
                && Objects.equals(fraction, that.fraction) && Objects.equals(content, that.content)
                && Objects.equals(nlp, that.nlp);
    }

    @Override
    public int hashCode() {
        return Objects.hash(speechKey, speakerName, fraction, content, nlp);
    }

    @Override
    public String toString() {
        return new StringJoiner(", ", HtmlSpeech.class.getSimpleName() + "[", "]")
                .add("speechKey='" + speechKey + "'")
                .add("speakerName='" + speakerName + "'")
                .add("fraction='" + fraction + "'")
                .add("content=" + content)
                .add("nlp=" + nlp)
                .toString();
    }
}
