package de.wwwu.awolf.presenter.evaluation;

import de.wwwu.awolf.model.Line;
import de.wwwu.awolf.model.evaluation.ComparisonResult;
import de.wwwu.awolf.presenter.algorithms.Algorithm;
import de.wwwu.awolf.presenter.algorithms.AlgorithmHandler;

import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class AlgorithmComparison {

    private final List<Algorithm.Type> types;
    private final ExecutorService executorService;
    private final CompletionService<Line> completionService;
    private final Set<Line> lines;

    public AlgorithmComparison(List<Algorithm.Type> types, Set<Line> lines) {
        this.types = types;
        this.lines = lines;
        this.executorService = Executors.newFixedThreadPool(3);
        completionService = new ExecutorCompletionService<>(this.executorService);
    }

    public ComparisonResult compare() {

        ComparisonResult comparisonResult = new ComparisonResult();
        AlgorithmHandler algorithmHandler = AlgorithmHandler.getInstance();

        for (Algorithm.Type value : getTypes()) {
            Algorithm.Type type;
            synchronized (types) {
                type = value;
            }

            Line line = algorithmHandler.runAlgorithmByType(type, lines);
            comparisonResult.put(type, line);
        }
        return comparisonResult;
    }

    private List<Algorithm.Type> getTypes() {
        return types;
    }
}
