package com.braintree.interceptors;

import com.braintree.configuration.service.BrainTreeConfigService;
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.model.ModelService;
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.Collections;
import java.util.Optional;

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

@UnitTest
public class BraintreeBraintreeGeneralConfigurationSaveInterceptorTest {

    private static final String PAYPAL_CONFIGURATION_ID = "paypal_configuration_id";

    @InjectMocks
    private BraintreeBraintreeGeneralConfigurationSaveInterceptor interceptor;

    @Mock
    private BrainTreeConfigService brainTreeConfigService;

    @Mock
    private SessionService sessionService;

    @Mock
    private ModelService modelService;

    @Mock
    private BraintreeGeneralConfigurationModel braintreeGeneralConfigurationModel;
    @Mock
    private InterceptorContext interceptorContext;

    @Mock
    private BaseSiteModel currentBaseSite;

    @Mock
    private BraintreePayPalConfigurationModel braintreePayPalConfigurationModel;

    @Mock
    private BraintreePluginConfigurationModel braintreePluginConfigurationModel;

    @Mock
    private BraintreePayPalStylesConfigurationModel braintreePayPalStylesConfiguration;


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

        when(braintreeGeneralConfigurationModel.isDropIn()).thenReturn(true);
        when(braintreePayPalStylesConfiguration.getBraintreePayPalConfiguration()).thenReturn(braintreePayPalConfigurationModel);
        when(braintreeGeneralConfigurationModel.getBraintreeGeneralConfigurationId()).thenReturn(PAYPAL_CONFIGURATION_ID);
        when(brainTreeConfigService.getBaseSiteForGeneralConfigurationId(PAYPAL_CONFIGURATION_ID))
                .thenReturn(Optional.of(currentBaseSite));
        when(sessionService.executeInLocalViewWithParams(any(), any())).thenReturn(braintreePluginConfigurationModel);

        when(braintreePluginConfigurationModel.getBraintreePayPalConfiguration()).thenReturn(braintreePayPalConfigurationModel);
        when(braintreePayPalConfigurationModel.getBraintreePayPalStylesConfiguration()).thenReturn(
                Collections.singletonList(braintreePayPalStylesConfiguration));
    }

    @Test
    public void shouldDoNothingWhenThereAreNoPaypalStylesButtonWithHeightLessThen35() {
        when(braintreePayPalStylesConfiguration.getHeight()).thenReturn(38);

        interceptor.onPrepare(braintreeGeneralConfigurationModel, interceptorContext);

        verifyZeroInteractions(modelService);
    }

    @Test
    public void shouldSaveUpdatedStylesHeightWhenThereIsPaypalStylesButtonWithHeightLessThen35() {
        when(braintreePayPalStylesConfiguration.getHeight()).thenReturn(25);

        interceptor.onPrepare(braintreeGeneralConfigurationModel, interceptorContext);

        verify(braintreePayPalStylesConfiguration).setHeight(35);
        verify(modelService).save(braintreePayPalStylesConfiguration);
    }

}
