package cz.senslog.analytics.utils.newvalidator;

import cz.senslog.analytics.domain.AttributeType;
import cz.senslog.analytics.domain.ComparisonOperator;

import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

import static cz.senslog.analytics.domain.ComparisonOperator.*;
import static cz.senslog.analytics.domain.ComparisonOperator.NE;
import static java.util.Collections.emptyList;

public class Validator<T> {

    private static final Map<ComparisonOperator, BiFunction<Double, Double, Boolean>> functions;

    static {
        functions = new HashMap<>(6);
        functions.put(GT, (val, ths) -> val > ths);
        functions.put(GE, (val, ths) -> val >= ths);
        functions.put(LT, (val, ths) -> val < ths);
        functions.put(LE, (val, ths) -> val <= ths);
        functions.put(EQ, Double::equals);
        functions.put(NE, (val, ths) -> !val.equals(ths));
    }

    public static boolean checkThresholdValue(ComparisonOperator mode, Double value, Double threshold) {
        if (mode == null || value == null || threshold == null) return false;
        return functions.getOrDefault(mode, (val, ths) -> false).apply(value, threshold);
    }

    public static boolean checkThresholdValue(Threshold.Dimension.Rule rule, Double value) {
        if (rule == null || value == null) return false;
        return checkThresholdValue(rule.comparisonOperator(), value, rule.value());
    }

    public interface AttributeMapping<T> {
        Validator<T> addMapping(AttributeType property, Function<T, Supplier<Double>> getter);
    }


    public static <T> Validator.AttributeMapping<T> create() {
        return new Validator.AttributeMapping<>() {
            private final Validator<T> validator = new Validator<>();
            @Override
            public Validator<T> addMapping(AttributeType property, Function<T, Supplier<Double>> getter) {
                validator.map.put(property, getter);
                return validator;
            }
        };
    }

    private final Map<AttributeType, Function<T, Supplier<Double>>> map;

    private Validator() {
        this.map = new HashMap<>();
    }

    public Set<AttributeType> attributes() {
        return map.keySet();
    }

    public Validator<T> addMapping(AttributeType property, Function<T, Supplier<Double>> getter) {
        map.put(property, getter);
        return this;
    }

    public ValidationResult[] validate(T object, Threshold threshold) {
        return validate(object, threshold, map);
    }

    private static <T> ValidationResult validate(T object, Threshold.Dimension dimension, Map<AttributeType, Function<T, Supplier<Double>>> attributeMapping) {
        final Double testingValue = attributeMapping.getOrDefault(dimension.attributeType(), (o) -> () -> null).apply(object).get();
        final List<Threshold.Dimension.Rule> violatedRules = new ArrayList<>(dimension.rules().size());
        boolean passed = true;
        for (Threshold.Dimension.Rule rule : dimension.rules()) {
            passed &= checkThresholdValue(rule, testingValue);
            violatedRules.add(rule);
        }
        return new ValidationResult(testingValue, dimension.attributeType(), passed ? violatedRules : emptyList());
    }

    private static <T> ValidationResult[] validate(T object, Threshold threshold, Map<AttributeType, Function<T, Supplier<Double>>> attributeMapping) {
        final ValidationResult[] results = new ValidationResult[threshold.dimensions().size()];
        int resultIndex = 0;
        for (Threshold.Dimension dimension : threshold.dimensions()) {
            results[resultIndex++] = validate(object, dimension, attributeMapping);
        }
        return results;
    }
}
