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

import org.bson.Document;
import org.texttechnologylab.project.gruppe_05_1.database.MongoDBHandler;

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

public class Sentiment {
    int     begin;
    int     end;
    double  sentiment;      // overall sentiment
    double  negative;
    double  neutral;
    double  positive;

    public Sentiment() {
    }

    public Sentiment(int begin, int end, double sentiment, double negative, double neutral, double positive) {
        this.begin = begin;
        this.end = end;
        this.sentiment = sentiment;
        this.negative = negative;
        this.neutral = neutral;
        this.positive = positive;
    }

    public int getBegin() {
        return begin;
    }

    public void setBegin(int begin) {
        this.begin = begin;
    }

    public int getEnd() {
        return end;
    }

    public void setEnd(int end) {
        this.end = end;
    }

    public double getSentiment() {
        return sentiment;
    }

    public void setSentiment(double sentiment) {
        this.sentiment = sentiment;
    }

    public double getNegative() {
        return negative;
    }

    public void setNegative(double negative) {
        this.negative = negative;
    }

    public double getNeutral() {
        return neutral;
    }

    public void setNeutral(double neutral) {
        this.neutral = neutral;
    }

    public double getPositive() {
        return positive;
    }

    public void setPositive(double positive) {
        this.positive = positive;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Sentiment sentiment1)) return false;
        return begin == sentiment1.begin && end == sentiment1.end && Double.compare(sentiment, sentiment1.sentiment) == 0 && Double.compare(negative, sentiment1.negative) == 0 && Double.compare(neutral, sentiment1.neutral) == 0 && Double.compare(positive, sentiment1.positive) == 0;
    }

    @Override
    public int hashCode() {
        return Objects.hash(begin, end, sentiment, negative, neutral, positive);
    }

    @Override
    public String toString() {
        return new StringJoiner(", ", Sentiment.class.getSimpleName() + "[", "]")
                .add("begin=" + begin)
                .add("end=" + end)
                .add("sentiment=" + sentiment)
                .add("negative=" + negative)
                .add("neutral=" + neutral)
                .add("positive=" + positive)
                .toString();
    }

    /**
     *
     * @param sentimentDocs Die Sentiment-Dokumente (Speech --> analysisResults --> sentiment) aus der MongoDB lesen.
     *                      Das erste Dokument ist für die gesamte Rede, Sentiments 1..n entsprechen Sentences 0..n-1
     * @return
     */
    public static List<Sentiment> readSentimentsFromMongo(List<Document> sentimentDocs) {
        List<Sentiment> sentiments = new ArrayList<>();
        for (Document doc : sentimentDocs) {
            sentiments.add(new Sentiment(
                    doc.getInteger("begin"),
                    doc.getInteger("end"),
                    MongoDBHandler.getFieldAsDouble(doc, "score"),
                    MongoDBHandler.getFieldAsDouble(doc, "pos"),
                    MongoDBHandler.getFieldAsDouble(doc, "neu"),
                    MongoDBHandler.getFieldAsDouble(doc, "neg")
            ));
        }
        return sentiments;
    }
}
