package ${basePackage}.frame.validation;

import org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorContextImpl;
import ${basePackage}.frame.auth.LocalData;
import ${basePackage}.frame.utils.StringUtil;
import ${basePackage}.module.system.ent.DictItem;
import ${basePackage}.module.system.mgr.DictManager;
import ${basePackage}.module.system.req.DictLoadRequest;
import ${basePackage}.module.system.rsp.DictLoadResponse;

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 字典名称
            DictLoadRequest dictLoadRequest = new DictLoadRequest();
            dictLoadRequest.setDictName(name);
            DictManager dictManager = LocalData.getBean(DictManager.class);
            DictLoadResponse dictLoadResponse = dictManager.load(dictLoadRequest, LocalData.getSysToken());

            if (dictLoadResponse.hasError()) {
                constraintValidatorContext.disableDefaultConstraintViolation();
                constraintValidatorContext.buildConstraintViolationWithTemplate(messagePrefix + "验证字典[" + name + "]值出现异常->" + dictLoadResponse.getErrors().get(0).getMessage()).addConstraintViolation();
                return false;
            }

            List<DictItem> dictItems = dictLoadResponse.getDictItems();
            int i = 0;
            for (; i < dictItems.size(); i++) {
                DictItem dictItem = dictItems.get(i);
                if (o.equals(dictItem.getKey())) {
                    break;
                }
            }
            if (i < dictItems.size()) {
                return true;
            } else {
                constraintValidatorContext.disableDefaultConstraintViolation();
                constraintValidatorContext.buildConstraintViolationWithTemplate(messagePrefix + "不存在于字典[" + name + "]中").addConstraintViolation();
                return false;
            }
        }
    }
}
