package com.braintree.facade.impl;

import com.braintree.enums.BrainTreePaymentMethod;
import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.b2bacceleratorfacades.order.data.ScheduledCartData;
import de.hybris.platform.b2bacceleratorservices.customer.B2BCustomerAccountService;
import de.hybris.platform.commercefacades.order.data.CCPaymentInfoData;
import de.hybris.platform.core.model.user.CustomerModel;
import de.hybris.platform.orderscheduling.model.CartToOrderCronJobModel;
import de.hybris.platform.servicelayer.dto.converter.Converter;
import de.hybris.platform.servicelayer.user.UserService;
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.Collections;
import java.util.List;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.when;

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

    @Mock
    private UserService userService;
    @Mock
    private B2BCustomerAccountService customerAccountService;
    @Mock
    private Converter<CartToOrderCronJobModel, ScheduledCartData> scheduledCartConverter;

    @InjectMocks
    private DefaultBrainTreeB2BOrderFacade unit;

    @Mock
    private CustomerModel customer;
    @Mock
    private CartToOrderCronJobModel cartToOrderCronJob;

    @Test
    public void shouldReturnFalseWhenCustomerDoesNotHaveReplenishment() {
        when(userService.getCurrentUser()).thenReturn(customer);
        when(customerAccountService.getCartToOrderCronJobsForUser(customer)).thenReturn(Collections.emptyList());

        assertFalse(unit.isCustomerHasActiveReplenishmentForSavedPayPalOrCreditCard());
    }

    @Test
    public void shouldReturnFalseIfCustomerHasGooglePayReplenishment() {
        CCPaymentInfoData ccPaymentInfoData = new CCPaymentInfoData();
        ccPaymentInfoData.setSubscriptionId(BrainTreePaymentMethod.ANDROIDPAYCARD.getCode());
        ScheduledCartData cartData = new ScheduledCartData();
        cartData.setActive(true);
        cartData.setPaymentInfo(ccPaymentInfoData);

        when(userService.getCurrentUser()).thenReturn(customer);
        doReturn(List.of(cartToOrderCronJob)).when(customerAccountService).getCartToOrderCronJobsForUser(customer);
        when(scheduledCartConverter.convert(cartToOrderCronJob)).thenReturn(cartData);

        boolean result = unit.isCustomerHasActiveReplenishmentForSavedPayPalOrCreditCard();

        assertFalse(result);
    }

    @Test
    public void shouldReturnTrueWhenCustomerHasPayPalReplenishment() {
        CCPaymentInfoData ccPaymentInfoData = new CCPaymentInfoData();
        ccPaymentInfoData.setSubscriptionId(BrainTreePaymentMethod.PAYPALACCOUNT.getCode());
        ScheduledCartData cartData = new ScheduledCartData();
        cartData.setActive(true);
        cartData.setPaymentInfo(ccPaymentInfoData);

        when(userService.getCurrentUser()).thenReturn(customer);
        doReturn(List.of(cartToOrderCronJob)).when(customerAccountService).getCartToOrderCronJobsForUser(customer);
        when(scheduledCartConverter.convert(cartToOrderCronJob)).thenReturn(cartData);

        boolean result = unit.isCustomerHasActiveReplenishmentForSavedPayPalOrCreditCard();

        assertTrue(result);
    }

    @Test
    public void shouldReturnTrueWhenCustomerHasCreditCardReplenishment() {
        CCPaymentInfoData ccPaymentInfoData = new CCPaymentInfoData();
        ccPaymentInfoData.setSubscriptionId(BrainTreePaymentMethod.CREDITCARD.getCode());
        ScheduledCartData cartData = new ScheduledCartData();
        cartData.setActive(true);
        cartData.setPaymentInfo(ccPaymentInfoData);

        when(userService.getCurrentUser()).thenReturn(customer);
        doReturn(List.of(cartToOrderCronJob)).when(customerAccountService).getCartToOrderCronJobsForUser(customer);
        when(scheduledCartConverter.convert(cartToOrderCronJob)).thenReturn(cartData);

        boolean result = unit.isCustomerHasActiveReplenishmentForSavedPayPalOrCreditCard();

        assertTrue(result);
    }

}