package nogui;

import data.*;
import gui.GUIController;
import gui.I18N;
import javafx.concurrent.Task;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import util.Tasks;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.regex.Pattern;

import static nogui.Utils.*;

public class WordSets {
    public final static Logger logger = LogManager.getLogger(GUIController.class);

    public static void wordSets(JSONObject settings, Corpus corpus) {
        Filter filter = new Filter();
        // fixed values

        filter.setAl(AnalysisLevel.STRING_LEVEL);

        filter.setIsCvv(false);
        filter.setStringLength(1);

        // tab specific values
        filter.setNgramValue(Math.toIntExact((Long) settings.get("ngramValue")));
        filter.setSkipValue(Math.toIntExact((Long) settings.get("skipValue")));
        filter.setNotePunctuations((boolean) settings.get("notePunctuations"));
        filter.setCollocability(getCollocability((JSONArray) settings.get("collocability")));



        String calculateForString = (String) settings.get("calculateFor");
        CalculateFor calculateFor = CalculateFor.factory(I18N.get(calculateForString));
        filter.setCalculateFor(calculateFor);
        ArrayList<String> alsoVisualize = getAlsoVisualizeList((JSONArray) settings.get("alsoVisualize"));
        filter.setMultipleKeys(alsoVisualize);
        filter.setDisplayTaxonomy((boolean) settings.get("displayTaxonomy"));
        filter.setMinimalRelFre(Math.toIntExact((Long) settings.get("minimalRelFre")));


        // right part
        ArrayList<Pattern> msd = getMsd((String) settings.get("msd"));
        filter.setMsd(msd);
        filter.setTaxonomySetOperation(I18N.get((String) settings.get("taxonomySetOperation")));
        ArrayList<Taxonomy> taxonomy = getTaxonomy((JSONArray) settings.get("taxonomy"), corpus);
        filter.setTaxonomy(taxonomy);
        filter.setMinimalOccurrences(Math.toIntExact((Long) settings.get("minimalOccurrences")));
        filter.setMinimalTaxonomy(Math.toIntExact((Long) settings.get("minimalTaxonomy")));

        String message = Validation.validateForStringLevel(filter);
        if (message == null) {
            // no errors
            logger.info("Executing: ", filter.toString());
            StatisticsNew statistic = new StatisticsNew(corpus, filter, false);
            execute(statistic, corpus);
            try {
                boolean successullySaved = statistic.saveResultToDisk();
                if (successullySaved) {
                    logger.info(I18N.get("message.NOTIFICATION_ANALYSIS_COMPLETED"));
                } else {
                    logger.info(I18N.get("message.NOTIFICATION_ANALYSIS_COMPLETED_NO_RESULTS"));
                }
            } catch (UnsupportedEncodingException e1) {
                logger.error(I18N.get("message.ERROR_WHILE_SAVING_RESULTS_TO_CSV"));
            }
        } else {
            logger.error(message);
        }
    }

    private static void execute(StatisticsNew statistic, Corpus corpus) {
        Filter f = statistic.getFilter();
        logger.info("Started execution: ", f);

        if (f.getMinimalRelFre() > 1){
            prepareTaskForMinRelFre(statistic, corpus);
        } else {
            prepareMainTask(statistic, corpus);
        }
    }
}
