package com.braintree.interceptors;

import com.braintree.configuration.service.BrainTreeConfigService;
import com.braintree.enums.BraintreePayPalLayout;
import de.hybris.platform.basecommerce.model.site.BaseSiteModel;
import de.hybris.platform.core.model.BraintreePayPalStylesConfigurationModel;
import de.hybris.platform.core.model.BraintreePluginConfigurationModel;
import de.hybris.platform.servicelayer.interceptor.InterceptorContext;
import de.hybris.platform.servicelayer.interceptor.InterceptorException;
import de.hybris.platform.servicelayer.interceptor.ValidateInterceptor;
import de.hybris.platform.servicelayer.session.SessionExecutionBody;
import de.hybris.platform.servicelayer.session.SessionService;

import java.util.Map;
import java.util.Optional;

import static com.braintree.constants.BraintreeConstants.CURRENT_SITE;
import static com.braintree.constants.BraintreeConstants.MAX_PAYPAL_BTN_HEIGHT;
import static com.braintree.constants.BraintreeConstants.MIN_PAYPAL_BTN_HEIGHT;
import static com.braintree.constants.BraintreeConstants.MIN_PAYPAL_DROP_IN_BTN_HEIGHT;

public class BrainTreePayPalStylesConfigurationValidationInterceptor implements
        ValidateInterceptor<BraintreePayPalStylesConfigurationModel> {

    private static final String EXPECTED_HEIGHT_TO_BE_BETWEEN_35_PX_AND_55_PX =
            "Expected height to be between 35px and 55px";
    private static final String EXPECTED_HEIGHT_TO_BE_BETWEEN_25_PX_AND_55_PX =
            "Expected height to be between 25px and 55px";
    private static final String TAGLINE_IS_NOT_ALLOWED_FOR_VERTICAL_LAYOUT =
            "Tagline is not allowed for vertical layout";
    private BrainTreeConfigService brainTreeConfigService;
    private SessionService sessionService;

    @Override
    public void onValidate(
            BraintreePayPalStylesConfigurationModel braintreePayPalStylesConfiguration,
            InterceptorContext interceptorContext) throws InterceptorException {
        Optional<BaseSiteModel> currentBaseSite = brainTreeConfigService
                .getBaseSiteForPayPalConfigurationId(braintreePayPalStylesConfiguration.getBraintreePayPalConfiguration().getBraintreePayPalConfigurationId());

        if (currentBaseSite.isPresent()) {
            BraintreePluginConfigurationModel pluginConfigurationModel = sessionService.executeInLocalViewWithParams(
                    Map.of(CURRENT_SITE, currentBaseSite.get()), new SessionExecutionBody() {
                        @Override
                        public Object execute() {
                            return brainTreeConfigService.getBrainTreePluginConfiguration();
                        }
                    });
            if (pluginConfigurationModel.getBraintreeGeneralConfiguration().isDropIn()) {
                if (braintreePayPalStylesConfiguration.getHeight() > MAX_PAYPAL_BTN_HEIGHT || braintreePayPalStylesConfiguration.getHeight() <
                        MIN_PAYPAL_DROP_IN_BTN_HEIGHT) {
                    throw new InterceptorException(EXPECTED_HEIGHT_TO_BE_BETWEEN_35_PX_AND_55_PX);
                }
            } else {
                if (braintreePayPalStylesConfiguration.getHeight() > MAX_PAYPAL_BTN_HEIGHT || braintreePayPalStylesConfiguration.getHeight() <
                        MIN_PAYPAL_BTN_HEIGHT) {
                    throw new InterceptorException(EXPECTED_HEIGHT_TO_BE_BETWEEN_25_PX_AND_55_PX);
                }
            }

            if (braintreePayPalStylesConfiguration.getLayout().equals(BraintreePayPalLayout.VERTICAL) &&
                    braintreePayPalStylesConfiguration.isTagline()) {
                throw new InterceptorException(TAGLINE_IS_NOT_ALLOWED_FOR_VERTICAL_LAYOUT);
            }

        }

    }

    public void setBrainTreeConfigService(BrainTreeConfigService brainTreeConfigService) {
        this.brainTreeConfigService = brainTreeConfigService;
    }

    public void setSessionService(SessionService sessionService) {
        this.sessionService = sessionService;
    }

}
