package cn.superdesk.uniorder.common.excel.validate.constraint;

import cn.superdesk.uniorder.common.excel.validate.RelationCheckFromCacheRedisUtils;
import cn.superdesk.uniorder.common.excel.validate.annotion.RelationIsEntry;
import cn.superdesk.uniorder.common.mq.redis.service.RedisService;
import cn.superdesk.uniorder.common.spring.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;


/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2021/8/26 下午5:38
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/8/26 下午5:38
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
//@Component
public class RelationIsEntryValidator implements ConstraintValidator<RelationIsEntry, String> {

	private boolean isSuggest;

	/**
	 * 是否强制校验(必须)
	 */
//	private boolean required;

	/**
	 * 前缀
	 */
	private String cachePrefix;
	/**
	 * cacheKey
	 */
	private String cacheKey;

	private String message;

//	@Autowired
//	private CacheService cacheService;


	@Override
	public void initialize(RelationIsEntry constraintAnnotation) {

		this.isSuggest = constraintAnnotation.isSuggest();
//		this.required = constraintAnnotation.required();

		this.cachePrefix = constraintAnnotation.cachePrefix();
		this.cacheKey = constraintAnnotation.cacheKey();

		this.message = constraintAnnotation.message();
	}


	/**
	 * Implements the validation logic.
	 * The state of {@code value} must not be altered.
	 * <p>
	 * This method can be accessed concurrently, thread-safety must be ensured
	 * by the implementation.
	 *
	 * @param value   object to validate
	 * @param context context in which the constraint is evaluated
	 * @return {@code false} if {@code value} does not pass the constraint
	 */
	@Override
	public boolean isValid(String value, ConstraintValidatorContext context) {

		log.info("{}", value);

		// TODO

//		if (required) {
//			return !StrUtil.isEmpty(fieldValue);
//		}


		return RelationCheckFromCacheRedisUtils.builder()
				.fieldValue(value)
				.redisService(SpringContextHolder.getBean(RedisService.class))
//				.cacheService(cacheService)
				.cachePrefix(cachePrefix)
				.cacheKey(cacheKey)
				.message(message)
				.build()
				.isEntry();
	}
}