package com.braintree.interceptors;

import com.braintree.configuration.service.BrainTreeConfigService;
import com.braintree.enums.BraintreePayPalLayout;
import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.basecommerce.model.site.BaseSiteModel;
import de.hybris.platform.core.model.BraintreeGeneralConfigurationModel;
import de.hybris.platform.core.model.BraintreePayPalConfigurationModel;
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.session.SessionService;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.Optional;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;

@UnitTest
public class BrainTreePayPalStylesConfigurationValidationInterceptorTest {

    private static final String PAYPAL_CONFIGURATION_ID = "paypal_configuration_id";
    @InjectMocks
    private BrainTreePayPalStylesConfigurationValidationInterceptor interceptor;

    @Mock
    private BrainTreeConfigService brainTreeConfigService;

    @Mock
    private SessionService sessionService;

    @Mock
    private BraintreePayPalStylesConfigurationModel braintreePayPalStylesConfiguration;

    @Mock
    private BraintreePayPalConfigurationModel braintreePayPalConfigurationModel;

    @Mock
    private BraintreePluginConfigurationModel braintreePluginConfigurationModel;

    @Mock
    private BraintreeGeneralConfigurationModel braintreeGeneralConfigurationModel;

    @Mock
    private InterceptorContext interceptorContext;

    @Mock
    private BaseSiteModel currentBaseSite;

    @Before
    public void initDefaults() {
        MockitoAnnotations.initMocks(this);

        when(braintreePayPalStylesConfiguration.getBraintreePayPalConfiguration()).thenReturn(braintreePayPalConfigurationModel);
        when(braintreePayPalConfigurationModel.getBraintreePayPalConfigurationId()).thenReturn(PAYPAL_CONFIGURATION_ID);
        when(brainTreeConfigService.getBaseSiteForPayPalConfigurationId(PAYPAL_CONFIGURATION_ID))
                .thenReturn(Optional.of(currentBaseSite));
        when(sessionService.executeInLocalViewWithParams(any(), any())).thenReturn(braintreePluginConfigurationModel);

        when(braintreePluginConfigurationModel.getBraintreeGeneralConfiguration()).thenReturn(braintreeGeneralConfigurationModel);
        when(braintreePayPalStylesConfiguration.getLayout()).thenReturn(BraintreePayPalLayout.HORIZONTAL);
        when(braintreePayPalStylesConfiguration.isTagline()).thenReturn(true);
        when(braintreePayPalStylesConfiguration.getHeight()).thenReturn(45);
    }


    @Test(expected = InterceptorException.class)
    public void shouldThrowExceptionWhenHeightIs24() throws InterceptorException {
        when(braintreeGeneralConfigurationModel.isDropIn()).thenReturn(false);
        when(braintreePayPalStylesConfiguration.getHeight()).thenReturn(24);

        interceptor.onValidate(braintreePayPalStylesConfiguration, interceptorContext);
    }

    @Test(expected = InterceptorException.class)
    public void shouldThrowExceptionWhenHeightIs56() throws InterceptorException {
        when(braintreeGeneralConfigurationModel.isDropIn()).thenReturn(false);
        when(braintreePayPalStylesConfiguration.getHeight()).thenReturn(56);

        interceptor.onValidate(braintreePayPalStylesConfiguration, interceptorContext);
    }

    @Test(expected = InterceptorException.class)
    public void shouldThrowExceptionWhenHeightIs24AndDropinTurnOn() throws InterceptorException {
        when(braintreeGeneralConfigurationModel.isDropIn()).thenReturn(true);
        when(braintreePayPalStylesConfiguration.getHeight()).thenReturn(24);

        interceptor.onValidate(braintreePayPalStylesConfiguration, interceptorContext);
    }

    @Test(expected = InterceptorException.class)
    public void shouldThrowExceptionWhenHeightIs56AndDropinTurnOn() throws InterceptorException {
        when(braintreeGeneralConfigurationModel.isDropIn()).thenReturn(true);
        when(braintreePayPalStylesConfiguration.getHeight()).thenReturn(56);

        interceptor.onValidate(braintreePayPalStylesConfiguration, interceptorContext);
    }

    @Test
    public void shouldNotThrowExceptionWhenHeightIs55AndDropinTurnOn() throws InterceptorException {
        when(braintreeGeneralConfigurationModel.isDropIn()).thenReturn(true);
        when(braintreePayPalStylesConfiguration.getHeight()).thenReturn(55);

        interceptor.onValidate(braintreePayPalStylesConfiguration, interceptorContext);
    }

    @Test
    public void shouldNotThrowExceptionWhenHeightIs35AndDropinTurnOn() throws InterceptorException {
        when(braintreeGeneralConfigurationModel.isDropIn()).thenReturn(true);
        when(braintreePayPalStylesConfiguration.getHeight()).thenReturn(35);

        interceptor.onValidate(braintreePayPalStylesConfiguration, interceptorContext);
    }

    @Test
    public void shouldNotThrowExceptionWhenHeightIs25() throws InterceptorException {
        when(braintreeGeneralConfigurationModel.isDropIn()).thenReturn(false);
        when(braintreePayPalStylesConfiguration.getHeight()).thenReturn(25);

        interceptor.onValidate(braintreePayPalStylesConfiguration, interceptorContext);
    }

    @Test
    public void shouldNotThrowExceptionWhenHeightIs55() throws InterceptorException {
        when(braintreeGeneralConfigurationModel.isDropIn()).thenReturn(false);
        when(braintreePayPalStylesConfiguration.getHeight()).thenReturn(55);

        interceptor.onValidate(braintreePayPalStylesConfiguration, interceptorContext);
    }

    @Test(expected = InterceptorException.class)
    public void shouldThrowExceptionWhenLayoutIsVerticalAndTaglineIsTrue() throws InterceptorException {
        when(braintreePayPalStylesConfiguration.getLayout()).thenReturn(BraintreePayPalLayout.VERTICAL);
        when(braintreePayPalStylesConfiguration.isTagline()).thenReturn(true);

        interceptor.onValidate(braintreePayPalStylesConfiguration, interceptorContext);
    }

    @Test
    public void shouldNotThrowExceptionWhenLayoutIsHorizontalAndTaglineIsTrue() throws InterceptorException {
        when(braintreePayPalStylesConfiguration.getLayout()).thenReturn(BraintreePayPalLayout.HORIZONTAL);
        when(braintreePayPalStylesConfiguration.isTagline()).thenReturn(true);

        interceptor.onValidate(braintreePayPalStylesConfiguration, interceptorContext);
    }

    @Test
    public void shouldNotThrowExceptionWhenLayoutIsVerticalAndTaglineIsFalse() throws InterceptorException {
        when(braintreePayPalStylesConfiguration.getLayout()).thenReturn(BraintreePayPalLayout.VERTICAL);
        when(braintreePayPalStylesConfiguration.isTagline()).thenReturn(false);

        interceptor.onValidate(braintreePayPalStylesConfiguration, interceptorContext);
    }

}
