package com.iqser.red.service.persistence.management.v1.processor.validation;

import java.awt.Color;
import java.util.Optional;

import org.apache.commons.lang3.StringUtils;

import lombok.experimental.UtilityClass;

@UtilityClass
public class DictionaryValidator {

    public final String EMPTY_MSG = "%s is empty.";
    public final String COLOR_VALUE_MSG = "Invalid HEX color";
    public final String BOOLEAN_MSG = "Boolean value for %s not set.";


    public Optional<String> validateDictionaryType(String value) {

        return validateValue(value, ValueType.TYPE);
    }


    private Optional<String> validateValue(String value, ValueType valueType) {

        if (StringUtils.isBlank(value)) {
            return Optional.of(String.format(EMPTY_MSG, valueType.name()));
        }
        // no need to test the length of the value (at least 1 character is acceptable for non-latin words

        return Optional.empty();
    }


    public Optional<String> validateDictionaryEntry(String value) {

        return validateValue(value, ValueType.ENTRY);
    }


    public Optional<String> validateColor(String hexColor) {

        if (hexColor == null) {
            return Optional.of(String.format(EMPTY_MSG, "color"));
        }

        try {
            Color.decode(hexColor);
        } catch (NumberFormatException nfe) {
            return Optional.of(COLOR_VALUE_MSG);
        }

        return Optional.empty();

    }


    public Optional<String> validateBoolean(Boolean bool, String name) {

        if (bool == null) {
            return Optional.of(String.format(BOOLEAN_MSG, name));
        }
        return Optional.empty();

    }


    enum ValueType {
        TYPE("Dictionary type"),
        ENTRY("Dictionary entry");

        public final String label;


        ValueType(String label) {

            this.label = label;
        }
    }

}
