/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package FunctionLayer;

import com.google.common.collect.MapMaker;
import edu.stanford.nlp.pipeline.CoreDocument;
import edu.stanford.nlp.pipeline.CoreEntityMention;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author install1
 */
public class MessageResponseHandler {

    private static ConcurrentMap<Integer, String> str = new MapMaker().concurrencyLevel(6).makeMap();

    public static ConcurrentMap<Integer, String> getStr() {
        ArrayList<String> arrayList = new ArrayList(str.values());
        Collections.sort(arrayList, (o1, o2) -> o2.length() - o1.length());
        int iterator = 0;
        for (String str1 : arrayList) {
            str.put(iterator, str1);
            iterator++;
        }
        return str;
    }

    public static void setStr(ConcurrentMap<Integer, String> str) {
        MessageResponseHandler.str = str;
    }

    public static void getMessage(String message) {
        if (message != null && !message.isEmpty()) {
            message = message.replace("@", "");
            if (message.contains("<>")) {
                message = message.substring(message.indexOf(">"));
            }
            if (message.startsWith("[ *")) {
                message = message.substring(message.indexOf("]"));
            }
            str.put(str.size() + 1, message);
        }
    }

    public static String selectReponseMessage(String toString, String personName) {
        String getResponseMsg = Datahandler.instance.getResponseMsg(toString);
        getResponseMsg = checkPersonPresentInSentence(personName, getResponseMsg, toString);
        return getResponseMsg;
    }

    private static String checkPersonPresentInSentence(String personName, String responseMsg, String userLastMessage) {
        //check if userlastmsg contains person as refference
        //check if first person is author or their person of mention
        try {
            String strreturn = responseMsg;
            CoreDocument pipelineCoreDcoument = new CoreDocument(responseMsg);
            CoreDocument pipelineCoreDcoumentLastMsg = new CoreDocument(userLastMessage);
            Datahandler.getPipeline().annotate(pipelineCoreDcoument);
            Datahandler.getPipeline().annotate(pipelineCoreDcoumentLastMsg);
            //Datahandler.pipeline.annotate(pipelineCoreDcoument);
            //Datahandler.pipeline.annotate(pipelineCoreDcoumentLastMsg);
            String regex = "(.*?\\d){10,}";
            for (CoreEntityMention em : pipelineCoreDcoument.entityMentions()) {
                String entityType = em.entityType();
                if (entityType.equals("PERSON")) {
                    String str = strreturn;
                    String emText = em.text();
                    Pattern pattern = Pattern.compile(regex);
                    Matcher matcher = pattern.matcher(personName);
                    boolean isMatched = matcher.matches();
                    if (!emText.equals(personName) && !isMatched) {
                        for (CoreEntityMention emLastMsg : pipelineCoreDcoumentLastMsg.entityMentions()) {
                            if (!emText.equals(emLastMsg.text()) && !Character.isDigit(emLastMsg.text().trim().charAt(0))) {
                                //System.out.println("emLastMsg.text(): " + emLastMsg.text());
                                str = strreturn.substring(0, strreturn.indexOf(emText)) + " "
                                        + emLastMsg + " " + strreturn.substring(strreturn.indexOf(emText));
                            }
                        }
                        str += " " + personName;
                        return str;
                    }
                }
            }
        } catch (Exception e) {
            System.out.println("SCUFFED JAYZ: " + e.getLocalizedMessage() + "\n");
        }
        return responseMsg;
    }
}
