package com.braintree.interceptors;

import de.hybris.platform.core.model.BraintreeGeneralConfigurationModel;
import de.hybris.platform.servicelayer.interceptor.InterceptorContext;
import de.hybris.platform.servicelayer.interceptor.InterceptorException;
import de.hybris.platform.servicelayer.interceptor.ValidateInterceptor;

import java.util.Objects;
import java.util.regex.Pattern;

public class BrainTreeGeneralConfigurationValidationInterceptor implements ValidateInterceptor<BraintreeGeneralConfigurationModel> {

    private static final String PRODUCT_NAME_EXCEEDS_MAXIMUM_LENGTH = "Invalid Product Name";
    private static final String BUSINESS_NAME_HAS_WRONG_LENGTH = "Invalid Business Name";
    private static final String INVALID_PHONE_NUMBER = "Invalid Phone Number";
    private static final String INVALID_URL = "Invalid Url";
    private static final String NAME_PATTERN = "[a-zA-Z0-9.\\-+ ]";
    private static final String PRODUCT_NAME_REGEX = "^" + NAME_PATTERN + "{1,9}$";
    private static final String BUSINESS_NAME_REGEX = "^" + NAME_PATTERN + "{3}$|^" + NAME_PATTERN + "{7}$|^" + NAME_PATTERN + "{12}$";
    private static final String PHONE_NUMBER_HAS_10_DIGIT_REGEX = "^[0-9]{10}$";
    private static final String PHONE_NUMBER_CONTAIN_UP_TO_4_SPECIFIC_CHAR_REGEX = "^[\\(\\)-]{0,4}$";
    private static final String PHONE_NUMBER_REGEX = "^[0-9\\-()]{10,14}$";
    private static final String URL_REGEX = "^[a-zA-Z0-9.]{1,13}$";


    @Override
    public void onValidate(BraintreeGeneralConfigurationModel config, InterceptorContext ctx) throws InterceptorException {
        validateBusinessName(config.getDescriptorBusinessName());
        validateProductName(config.getDescriptorProductName());
        validatePhoneNumber(config.getDescriptorPhoneNumber());
        validateUrl(config.getDescriptorUrl());
    }

    private void validateBusinessName(String businessName) throws InterceptorException {
        if (Objects.isNull(businessName)) {
            return;
        }

        validateFieldWithRegex(BUSINESS_NAME_REGEX, BUSINESS_NAME_HAS_WRONG_LENGTH, businessName);
    }

    private void validateProductName(String productName) throws InterceptorException {
        if (Objects.isNull(productName)) {
            return;
        }

        validateFieldWithRegex(PRODUCT_NAME_REGEX, PRODUCT_NAME_EXCEEDS_MAXIMUM_LENGTH, productName);
    }

    private void validatePhoneNumber(String phoneNumber) throws InterceptorException {
        if (Objects.isNull(phoneNumber)) {
            return;
        }
        validateFieldWithRegex(PHONE_NUMBER_REGEX, INVALID_PHONE_NUMBER, phoneNumber);
        validateFieldWithRegex(PHONE_NUMBER_CONTAIN_UP_TO_4_SPECIFIC_CHAR_REGEX,
                INVALID_PHONE_NUMBER, phoneNumber.replaceAll("\\d", ""));
        validateFieldWithRegex(PHONE_NUMBER_HAS_10_DIGIT_REGEX, INVALID_PHONE_NUMBER,
                phoneNumber.replaceAll("\\D", ""));

    }

    private void validateUrl(String url) throws InterceptorException {
        if (Objects.isNull(url)) {
            return;
        }

        validateFieldWithRegex(URL_REGEX, INVALID_URL, url);
    }

    private void validateFieldWithRegex(String regex, String error, String fieldToValidate) throws InterceptorException {
        Pattern pattern = Pattern.compile(regex);
        if (!pattern.matcher(fieldToValidate).matches()) {
            throw new InterceptorException(error);
        }
    }
}
