package ${basePackage}.frame.validation;

import org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorContextImpl;
import ${basePackage}.frame.auth.LocalData;
import ${basePackage}.frame.utils.StringUtil;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.List;

public class DictValidator implements ConstraintValidator<Dict, String> {

    private String name;

    @Override
    public void initialize(Dict constraint) {
        name = constraint.name();
    }

    @Override
    public boolean isValid(String o, ConstraintValidatorContext constraintValidatorContext) {
        String messagePrefix = "";
        if (constraintValidatorContext instanceof ConstraintValidatorContextImpl) {
            ConstraintValidatorContextImpl validatorContext = (ConstraintValidatorContextImpl) constraintValidatorContext;
            if (validatorContext.getConstraintViolationCreationContexts() != null
                    && validatorContext.getConstraintViolationCreationContexts().size() > 0
                    && validatorContext.getConstraintViolationCreationContexts().get(0).getPath() != null) {
                messagePrefix = "[" + validatorContext.getConstraintViolationCreationContexts().get(0).getPath().asString() + "]";
            }
        }

        if (StringUtil.isEmpty(o)) {
            return true;
        } else if (name == null) {
            constraintValidatorContext.disableDefaultConstraintViolation();

            constraintValidatorContext.buildConstraintViolationWithTemplate(messagePrefix + "字典名称为空").addConstraintViolation();
            return false;
        } else {
            // name 字典名称
            constraintValidatorContext.disableDefaultConstraintViolation();
            constraintValidatorContext.buildConstraintViolationWithTemplate(messagePrefix + "字典验证尚未实现").addConstraintViolation();
            return false;
        }
    }
}
