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;
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 {
            // 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();
                String message = String.format(Locale.CHINESE, "[ %s ] 验证字典 [ %s ] 值出现异常-> %s！", fieldName, name, dictLoadResponse.getErrors().get(0).getMessage());
                constraintValidatorContext.buildConstraintViolationWithTemplate(message).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 {
                String message = String.format(Locale.CHINESE, "%s 指定的字典 [ %s ] 中，未找到字典值(%s)！", fieldName, name, o);
                constraintValidatorContext.buildConstraintViolationWithTemplate(message).addConstraintViolation();
                return false;
            }
        }
    }
}
