package xyz.wbsite.frame.validation;

import xyz.wbsite.frame.provider.DictProvider;
import xyz.wbsite.frame.provider.FrameProvider;
import xyz.wbsite.frame.utils.StringUtil;
import org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorContextImpl;

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

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 fieldName = "";
        // 获取注解的字段名称，用于快速定位排查
        if (constraintValidatorContext instanceof ConstraintValidatorContextImpl) {
            ConstraintValidatorContextImpl validatorContext = (ConstraintValidatorContextImpl) constraintValidatorContext;
            if (validatorContext.getConstraintViolationCreationContexts() != null
                    && validatorContext.getConstraintViolationCreationContexts().size() > 0
                    && validatorContext.getConstraintViolationCreationContexts().get(0).getPath() != null) {
                fieldName = validatorContext.getConstraintViolationCreationContexts().get(0).getPath().asString();
            }
        }

        if (StringUtil.isEmpty(o)) {// 当值为空字符时，认为值有效
            return true;
        } else if (name == null) {// 当字典名称为空时，说明未指定字典，提示错误
            // 禁用默认错误信息，防止错误信息与自定义信息乱串
            constraintValidatorContext.disableDefaultConstraintViolation();
            String message = String.format(Locale.CHINESE, "[ %s ] 未指定字典名称！", fieldName);
            constraintValidatorContext.buildConstraintViolationWithTemplate(message).addConstraintViolation();
            return false;
        } else {// 加载字典，并验证提交值是否包含在内
            DictProvider dictProvider = FrameProvider.getInstance().getDictProvider();

            // 如果加载字典出现错误，返回验证错误
            if (dictProvider == null) {
                // 禁用默认错误信息，防止错误信息与自定义信息乱串
                constraintValidatorContext.disableDefaultConstraintViolation();
                String message = String.format(Locale.CHINESE, "[ %s ] 验证字典 [ %s ] 未实现字典验证接口", fieldName, name);
                constraintValidatorContext.buildConstraintViolationWithTemplate(message).addConstraintViolation();
                return false;
            }

            // 确认字典是否存在
            boolean isExist = dictProvider.isExist(name);

            if (!isExist) {
                // 禁用默认错误信息，防止错误信息与自定义信息乱串
                constraintValidatorContext.disableDefaultConstraintViolation();
                String message = String.format(Locale.CHINESE, "[ %s ] 指定的字典 [ %s ] 未找到！", fieldName, name, o);
                constraintValidatorContext.buildConstraintViolationWithTemplate(message).addConstraintViolation();
                return false;
            }

            // 确认字典项是否存在
            boolean isExistValue = dictProvider.isExistValue(name, o);

            // 当未找到字典项值时，认为值无效，返回验证错误
            if (!isExistValue) {
                // 禁用默认错误信息，防止错误信息与自定义信息乱串
                constraintValidatorContext.disableDefaultConstraintViolation();
                String message = String.format(Locale.CHINESE, "[ %s ] 指定的字典 [ %s ] 中，未找到字典属值(%s)！", fieldName, name, o);
                constraintValidatorContext.buildConstraintViolationWithTemplate(message).addConstraintViolation();
                return false;
            } else {
                return true;
            }
        }
    }
}
