package com.braintree.facade.impl;

import com.braintree.enums.BraintreeLocation;
import com.braintree.enums.BraintreePageType;
import com.braintree.hybris.data.ApplePayButtonStyleData;
import com.braintree.hybris.data.GooglePayButtonStyleData;
import com.braintree.hybris.data.PayPalButtonStyleData;
import com.braintree.services.BrainTreeStylesConfigurationService;
import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.core.model.BraintreeApplePayStylesConfigurationModel;
import de.hybris.platform.core.model.BraintreeGooglePayStylesConfigurationModel;
import de.hybris.platform.core.model.BraintreePayPalStylesConfigurationModel;
import de.hybris.platform.servicelayer.dto.converter.Converter;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.List;
import java.util.Optional;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@UnitTest
@RunWith(MockitoJUnitRunner.class)
public class DefaultBrainTreeStylesConfigurationFacadeTest {

    @Mock
    private BrainTreeStylesConfigurationService stylesConfigurationService;
    @Mock
    private Converter<BraintreeApplePayStylesConfigurationModel, ApplePayButtonStyleData> applePayButtonStyleDataConverter;
    @Mock
    private Converter<BraintreeGooglePayStylesConfigurationModel, GooglePayButtonStyleData> googlePayButtonStyleDataConverter;
    @Mock
    private Converter<BraintreePayPalStylesConfigurationModel, PayPalButtonStyleData> payPalButtonStyleDataConverter;

    @InjectMocks
    private DefaultBrainTreeStylesConfigurationFacade unit;

    @Mock
    private BraintreeApplePayStylesConfigurationModel applePayStylesConfigurationModel;
    @Mock
    private BraintreePayPalStylesConfigurationModel payPalStylesConfigurationModel;
    @Mock
    private BraintreeGooglePayStylesConfigurationModel googlePayStylesConfigurationModel;

    @Test
    public void shouldReturnApplePayStylesConfiguration(){
        when(stylesConfigurationService.getApplePayStyleConfigurations()).thenReturn(List.of(applePayStylesConfigurationModel));
        when(applePayStylesConfigurationModel.getLocation()).thenReturn(BraintreeLocation.MARK);

        unit.getApplePayStyleConfigurationFor(BraintreePageType.MARK);

        verify(applePayButtonStyleDataConverter).convert(applePayStylesConfigurationModel);
    }

    @Test
    public void shouldReturnEmptyOptionalIfApplePayStylesConfigurationNotExistForGivenPageType(){
        when(stylesConfigurationService.getApplePayStyleConfigurations()).thenReturn(List.of(applePayStylesConfigurationModel));
        when(applePayStylesConfigurationModel.getLocation()).thenReturn(BraintreeLocation.MARK);

        Optional<ApplePayButtonStyleData> styleData = unit.getApplePayStyleConfigurationFor(BraintreePageType.OTHER);

        Assert.assertFalse(styleData.isPresent());
    }

    @Test
    public void shouldReturnPayPalStylesConfiguration(){
        when(stylesConfigurationService.getPayPalStyleConfigurations()).thenReturn(List.of(payPalStylesConfigurationModel));
        when(payPalStylesConfigurationModel.getLocation()).thenReturn(BraintreeLocation.MARK);

        unit.getPayPalStyleConfiguration(BraintreePageType.MARK);

        verify(payPalButtonStyleDataConverter).convert(payPalStylesConfigurationModel);
    }

    @Test()
    public void shouldReturnEmptyOptionalIfPayPalStylesConfigurationNotExistForGivenPageType(){
        when(stylesConfigurationService.getPayPalStyleConfigurations()).thenReturn(List.of(payPalStylesConfigurationModel));
        when(payPalStylesConfigurationModel.getLocation()).thenReturn(BraintreeLocation.MARK);

        Optional<PayPalButtonStyleData> styleData = unit.getPayPalStyleConfiguration(BraintreePageType.OTHER);

        Assert.assertFalse(styleData.isPresent());
    }

    @Test
    public void shouldReturnGooglePayStylesConfiguration(){
        when(stylesConfigurationService.getGooglePayStyleConfigurations()).thenReturn(List.of(googlePayStylesConfigurationModel));
        when(googlePayStylesConfigurationModel.getLocation()).thenReturn(BraintreeLocation.MARK);

        unit.getGooglePayStyleConfiguration(BraintreePageType.MARK);

        verify(googlePayButtonStyleDataConverter).convert(googlePayStylesConfigurationModel);
    }

    @Test
    public void shouldReturnEmptyOptionalIfGooglePayStylesConfigurationNotExistForGivenPageType(){
        when(stylesConfigurationService.getGooglePayStyleConfigurations()).thenReturn(List.of(googlePayStylesConfigurationModel));
        when(googlePayStylesConfigurationModel.getLocation()).thenReturn(BraintreeLocation.MARK);

        Optional<GooglePayButtonStyleData> styleData = unit.getGooglePayStyleConfiguration(BraintreePageType.OTHER);

        Assert.assertFalse(styleData.isPresent());
    }
}