package com.braintreeocc.validators.impl;

import com.braintree.hybris.data.ws.ApplePayPaymentRequestWsDTO;
import com.braintreeocc.validators.BrainTreeValidator;
import com.braintreeocc.constants.BraintreeoccWebConstants;
import org.springframework.util.Assert;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

public class BraintreeApplePayPaymentRequestValidator implements BrainTreeValidator {

    private Validator paymentAddressValidator;

    @Override
    public boolean supports(Class<?> clazz) {
        return ApplePayPaymentRequestWsDTO.class.isAssignableFrom(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        final ApplePayPaymentRequestWsDTO paymentResponse = (ApplePayPaymentRequestWsDTO) target;

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "nonce", BraintreeoccWebConstants.FIELD_REQUIRED_MESSAGE_ID);

        Assert.notNull(paymentResponse.getBillingContact(), "BillingAddress object must not be null");
        validate(paymentResponse.getBillingContact(), "billingAddress", getPaymentAddressValidator());

    }

    public Validator getPaymentAddressValidator() {
        return paymentAddressValidator;
    }

    public void setPaymentAddressValidator(Validator paymentAddressValidator) {
        this.paymentAddressValidator = paymentAddressValidator;
    }
}
