package eu.dnetlib.validator2.engine;

import eu.dnetlib.validator2.validation.XMLApplicationProfile;
import eu.dnetlib.validator2.validation.guideline.Guideline;
import eu.dnetlib.validator2.validation.guideline.openaire.AbstractOpenAireProfile;
import eu.dnetlib.validator2.validation.guideline.openaire.DataArchiveGuidelinesV2Profile;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;

import javax.xml.parsers.DocumentBuilder;
import java.io.File;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.OptionalDouble;
import java.util.stream.Collectors;

public class Test_v2_data {

    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Test_v2_data.class);


    private static final String[] FILES = new String[] {
//            "src/test/resources/openaireguidelinesV4/v4_literature_all_invalid_guidelines_record.xml",
            "src/test/resources/openaireguidelinesV4/v4_literature_all_guidelines_record.xml",
//            "src/test/resources/openaireguidelinesV4/oai_mediarep_org_doc_2534.xml",
//            "src/test/resources/openaireguidelinesV4/01_gv4.xml"
    };

    public static void main(String[] args) {
        // String xmlFile = args[0];
        AbstractOpenAireProfile profile = new DataArchiveGuidelinesV2Profile();
        logger.info("Max score: " + profile.maxScore());
        Map<String, Double> scorePerDoc = new LinkedHashMap<>();
        DocumentBuilder builder = TestUtils.getDocumentBuilder();
        if ( builder == null )
            return;
        for ( String fileName : FILES ) {
            try {
                logger.info("Processing \"" + fileName + "\"");
                Document doc = builder.parse(new File(fileName));
                XMLApplicationProfile.ValidationResult result = profile.validate(fileName, doc);
                scorePerDoc.put(fileName, result.score());
                Map<String, Guideline.Result> results = result.results();
                for ( Map.Entry<String, Guideline.Result> entry : results.entrySet() ) {
                    String key = entry.getKey();
                    Guideline.Result value = entry.getValue();
                    logger.debug(key + " = " + value);
                    if ( key.contains("Date")) {
                        logger.info("Warnings: " + results.get(key).warnings().toString());
                        logger.info("Errors: " + results.get(key).errors().toString());
                        logger.info("Result: " + key + " = " + value + "\n");
                    }
                }
            } catch (Exception e) {
                logger.error("", e);
            }
        }

        // Individual scores
        String printout = scorePerDoc.entrySet().stream().
                map(entry -> entry.getValue() + ": " + entry.getKey()).collect(Collectors.joining("\n"));

        logger.info(printout);

        // Average Score
        OptionalDouble optionalFinalScore = scorePerDoc.values().stream().mapToDouble(aDouble -> aDouble).average();
        double finalScore = (optionalFinalScore.isPresent() ? optionalFinalScore.getAsDouble() : -1 );

        logger.info("Validation Score: " + finalScore);
    }

}
