package ecoparasite;

import ecoparasite.completion.Completion;
import ecoparasite.input.InputFactory;
import ecoparasite.input.InputFileException;
import ecoparasite.input.RawData;
import ecoparasite.input.RawDataOverflow;
import ecoparasite.nettoyage.Nettoyage;
import ecoparasite.poisson.MackerelSerra;
import ecoparasite.poisson.Poisson;
import ecoparasite.representation.ValeursXY;
import ecoparasite.svg.IncorrectAxesPointsException;
import ecoparasite.svg.SVGBuilder;
import ecoparasite.svg.SVGFactory;
import ecoparasite.svg.elements.Element;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.function.BiConsumer;
import java.util.function.Function;

public class Application {

    public static void main(String[] args) throws InputFileException, RawDataOverflow {

        RawData rawMackerel = InputFactory.readData("test2.csv", ",");

        HashSet<Poisson> mackerelSet = MackerelSerra.parse(rawMackerel);

        System.out.println( "Avant nettoyage et complétion" );
        System.out.println( mackerelSet );

        Function<Poisson,Double> getLength = Poisson::getLength;
        Function<Poisson,Double> getInfes = Poisson::getInfestation;
        BiConsumer<Poisson,Double> setInfes = Poisson::setInfestation;

        mackerelSet = Nettoyage.nettoieColumns( mackerelSet, Poisson::getLength, Poisson::setLength, false );
        mackerelSet = Completion.completeColumnsMoyenne( mackerelSet, Poisson::getLength, Poisson::setLength );

        System.out.println( "Après Nettoyage et complétion de la longueur du poisson." );
        System.out.println( mackerelSet );

        mackerelSet = Nettoyage.nettoieColumns( mackerelSet, getInfes, setInfes, false );
        mackerelSet = Completion.completeColumnsLinear( mackerelSet, getLength, getInfes, setInfes );

        System.out.println( "Après Nettoyage et complétion du taux d'infestation des poissons" );
        System.out.println( mackerelSet );

        // Conversion des valeurs.
        HashSet<ValeursXY> mackerelXY = ValeursXY.convertToXY( mackerelSet, getLength, getInfes );
        HashMap<String, ArrayList<Double>> axes = SVGBuilder.calcPointAxes( mackerelXY );
        System.out.println( axes );

        SVGBuilder axesInstance;
        try {
            axesInstance = new SVGBuilder(axes);
        } catch (IncorrectAxesPointsException e) {
            System.out.println( "Mauvais format communiqué" );
            return;
        }

        // Sauvegarde pour plus tard.
        double[] ABCoef = Completion.getLinearCoef(mackerelSet, getLength, getInfes);

        ArrayList<Element> SVGElements = axesInstance.buildAll( "Length (mm)", "Taux d'infestation", mackerelXY, ABCoef[0], ABCoef[1] );
        SVGFactory.createSVG( SVGElements );

    }
}