package eu.dnetlib.validator2.validation.guideline.openaire;

import eu.dnetlib.validator2.validation.guideline.Builders;
import eu.dnetlib.validator2.validation.guideline.ElementSpec;
import eu.dnetlib.validator2.validation.guideline.Guideline;
import eu.dnetlib.validator2.validation.guideline.SyntheticGuideline;
import org.w3c.dom.Document;

import java.util.*;
import java.util.stream.Collectors;

import static eu.dnetlib.validator2.validation.guideline.Cardinality.ONE;
import static eu.dnetlib.validator2.validation.guideline.Cardinality.ONE_TO_N;

public final class R1_01M extends AbstractOpenAireProfile {

    private static final String[] RELATED_RESOURCE_GENERAL_TYPES = {
            "Audiovisual", "Collection", "DataPaper", "Dataset", "Event", "Image", "InteractiveResource",
            "Model", "PhysicalObject", "Service", "Software", "Sound", "Text", "Workflow", "Other"
    };

    private static final ElementSpec R1_01M_SPEC_1 = Builders.
            forMandatoryElement("resourceType", ONE).
            withMandatoryAttribute("resourceTypeGeneral", RELATED_RESOURCE_GENERAL_TYPES).
            build();

    private static final ElementSpec R1_01M_SPEC_2 = Builders.
            forMandatoryElement("format", ONE).
            build();

    private static final ElementSpec R1_01M_SPEC_3 = Builders. // With very low weight
            forMandatoryElement("description", ONE_TO_N).
            withMandatoryAttribute("descriptionType", "Methods").
            build();

    private static final ElementSpec R1_01M_SPEC_4 = Builders. // With very low weight
            forMandatoryElement("size", ONE_TO_N).
            build();

    //TODO: weights for guidelines haven't been finalized. They've been given an arbitrary value of 1.
    public static SyntheticGuideline R1_01M_1 = SyntheticGuideline.of("ResourceType", 3, R1_01M_SPEC_1);
    public static SyntheticGuideline R1_01M_2 = SyntheticGuideline.of("Format", 3, R1_01M_SPEC_2);
    public static SyntheticGuideline R1_01M_3 = SyntheticGuideline.of("Description", 3, R1_01M_SPEC_3);
    public static SyntheticGuideline R1_01M_4 = SyntheticGuideline.of("Size", 3, R1_01M_SPEC_4);

    private static final List<Guideline<Document>> GUIDELINES = Collections.unmodifiableList(
            Arrays.asList(
                    R1_01M_1,
                    R1_01M_2,
                    R1_01M_3,
                    R1_01M_4
            )
    );

    private static final Map<String, Guideline> GUIDELINE_MAP = GUIDELINES.
            stream().
            collect(Collectors.toMap(Guideline::getName, (guideline) -> guideline));

    private static final int MAX_SCORE = GUIDELINES.stream().map(Guideline::getWeight).reduce(0, Integer::sum);

    public R1_01M() {
        super("Plurality of accurate and relevant attributes are provided to allow reuse");
    }

    @Override
    public Collection<? extends Guideline<Document>> guidelines() {
        return GUIDELINES;
    }

    /**
     *
     * @param guidelineName
     * @return
     */
    @Override
    public Guideline guideline(String guidelineName) {
        return GUIDELINE_MAP.get(guidelineName);
    }

    @Override
    public int maxScore() {
        return MAX_SCORE;
    }
}