package data;

import static gui.ValidationUtil.*;

import java.util.ArrayList;
import java.util.regex.Pattern;

import gui.I18N;
import org.apache.commons.lang3.StringUtils;

import gui.Messages;
import gui.ValidationUtil;

public class Validation {

	public static String validateForStringLevel(Filter filter) {
		ArrayList<String> errors = new ArrayList<>();

		// should not be null, error if null, because init failed
		if (filter.getNgramValue() == null) {
			errors.add(I18N.get("message.MISSING_NGRAM_LEVEL"));
		}

		// should not be null, error if null, because init failed
		if (filter.getCalculateFor() == null) {
			errors.add(I18N.get("message.MISSING_CALCULATE_FOR"));
		}

		if (filter.getSkipValue() == null) {
			filter.setSkipValue(0);
		}

		if (filter.getNgramValue() != null && ValidationUtil.isEmpty(filter.getMsd()) &&
				(filter.getMsd().size() != filter.getNgramValue())) {
			if (!(filter.getMsd().size() == 1 && filter.getNgramValue() == 0) && !ValidationUtil.isEmpty(filter.getMsd()))
				errors.add(I18N.get("message.WARNING_MISMATCHED_NGRAM_AND_TOKENS_VALUES"));
		}

		Integer ngramValue = filter.getNgramValue();
		ArrayList<Pattern> msd = filter.getMsd();

		if (ngramValue > 0 && !ValidationUtil.isEmpty(msd) && ngramValue != msd.size()) {
			errors.add(String.format(I18N.get("message.WARNING_MISMATCHED_NGRAM_AND_TOKENS_VALUES"), ngramValue, msd.size()));
		}

		if (filter.getNgramValue() != null && filter.getNgramValue() == 0 && isEmpty(filter.getStringLength())) {
			// if count letters, make sure that the length is given
			// TODO: check that words we're adding in xml reader are longer than this value
			errors.add(I18N.get("message.MISSING_STRING_LENGTH"));
		}

		return isEmpty(errors) ? null : StringUtils.join(errors, ", \n");
	}
}
