package com.yunfeiyun.agmp.common.framework.validator;

import com.yunfeiyun.agmp.common.core.domain.BaseEntity;
import com.yunfeiyun.agmp.common.exception.IncorrectParameterException;

import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;

public class Validator {

    public static final Pattern PROPERTY_PATTERN = Pattern.compile("^[\\p{L}0-9_-]+$"); // Unicode letters, numbers, '_' and '-' allowed

    public static void validateString(String val, String errorMessage) {
        if (val == null || val.isEmpty()) {
            throw new IncorrectParameterException(errorMessage);
        }
    }

    public static void validatePositiveNumber(long val, String errorMessage) {
        if (val <= 0) {
            throw new IncorrectParameterException(errorMessage);
        }
    }

    public static void validateId(String id, String errorMessage) {
        if (id == null) {
            throw new IncorrectParameterException(errorMessage);
        }
        try {
            UUID.fromString(id).toString();
        } catch (Exception e) {
            throw new IncorrectParameterException(errorMessage);
        }
    }

    public static void validateIds(List<? extends BaseEntity> ids, String errorMessage) {
        if (ids == null || ids.isEmpty()) {
            throw new IncorrectParameterException(errorMessage);
        } else {
            for (BaseEntity id : ids) {
                validateId(id.getId(), errorMessage);
            }
        }
    }

}
