package com.apobates.forum.thrones.controller.helper;

import java.lang.reflect.InvocationTargetException;
import org.apache.commons.beanutils.BeanUtils;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
/**
 *
 * @author xiaofanku
 * @since 20200606
 */
public class FieldMatchValidator implements ConstraintValidator<FieldMatch, Object>{
    private String firstFieldName;
    private String secondFieldName;
    private String message;
    
    @Override
    public void initialize(final FieldMatch constraintAnnotation) {
        firstFieldName = constraintAnnotation.first();
        secondFieldName = constraintAnnotation.second();
        message = constraintAnnotation.message();
    }
    
    @Override
    public boolean isValid(final Object value, final ConstraintValidatorContext context) {
        boolean valid = true;
        try
        {
            final Object firstObj = BeanUtils.getProperty(value, firstFieldName);
            final Object secondObj = BeanUtils.getProperty(value, secondFieldName);
            
            valid =  firstObj == null && secondObj == null || firstObj != null && firstObj.equals(secondObj);
        }catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e){
            // ignore
        }
        
        if (!valid){
            context.buildConstraintViolationWithTemplate(message)
                    .addPropertyNode(firstFieldName)
                    .addConstraintViolation()
                    .disableDefaultConstraintViolation();
        }
        return valid;
    }
}