package com.braintree.actions.order;

import com.braintree.constants.BraintreeConstants;
import com.braintree.enums.BrainTreePaymentMethod;
import com.braintree.facade.impl.DefaultBrainTreeCheckoutFacade;
import com.braintree.model.BrainTreePaymentInfoModel;
import com.braintree.transaction.service.BrainTreeTransactionService;
import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.commerceservices.service.data.CommerceOrderParameter;
import de.hybris.platform.core.enums.OrderStatus;
import de.hybris.platform.core.model.order.OrderModel;
import de.hybris.platform.core.model.order.payment.InvoicePaymentInfoModel;
import de.hybris.platform.orderprocessing.model.OrderProcessModel;
import de.hybris.platform.payment.dto.TransactionStatus;
import de.hybris.platform.payment.enums.PaymentTransactionType;
import de.hybris.platform.payment.model.PaymentTransactionEntryModel;
import de.hybris.platform.payment.model.PaymentTransactionModel;
import de.hybris.platform.processengine.action.AbstractSimpleDecisionAction;
import de.hybris.platform.servicelayer.model.ModelService;
import org.junit.Before;
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 static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;


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

    private static final String PAYPAL_INTENT = "paypalIntent";
    private static final String CARD_NUMBER = "cardNumber";

    @Mock
    private BrainTreeTransactionService brainTreeTransactionService;
    @Mock
    private DefaultBrainTreeCheckoutFacade brainTreeCheckoutFacade;
    @Mock
    private ModelService modelService;

    @InjectMocks
    private BrainTreeCheckAuthorizeOrderPaymentAction unit;

    @Mock
    private InvoicePaymentInfoModel invoicePaymentInfoModel;
    @Mock
    private OrderModel orderModel;
    @Mock
    private OrderProcessModel orderProcessModel;
    @Mock
    private BrainTreePaymentInfoModel brainTreePaymentInfoModel;
    @Mock
    private PaymentTransactionModel paymentTransactionModel;
    @Mock
    private PaymentTransactionEntryModel paymentTransactionEntryModel;

    @Before
    public void setUp() {
        when(orderProcessModel.getOrder()).thenReturn(orderModel);
    }

    @Test
    public void executeActionShouldReturnNOkStatus() {

        AbstractSimpleDecisionAction.Transition result = unit.executeAction(orderProcessModel);
        assertEquals(result, AbstractSimpleDecisionAction.Transition.NOK);
    }

    @Test
    public void executeActionShouldReturnOkStatus() {
        when(orderModel.getPaymentInfo()).thenReturn(invoicePaymentInfoModel);

        AbstractSimpleDecisionAction.Transition result = unit.executeAction(orderProcessModel);

        assertEquals(result, AbstractSimpleDecisionAction.Transition.OK);
    }

    @Test
    public void shouldCreateAuthorizationTransactionWhenVenmoPayment() {
        //given
        when(orderModel.getPaymentInfo()).thenReturn(brainTreePaymentInfoModel);
        when(brainTreePaymentInfoModel.getPayPalIntent()).thenReturn(PAYPAL_INTENT);
        when(brainTreePaymentInfoModel.getCardNumber()).thenReturn(CARD_NUMBER);
        when(brainTreePaymentInfoModel.getPaymentProvider()).thenReturn(BrainTreePaymentMethod.VENMOACCOUNT.getCode());

        //when
        AbstractSimpleDecisionAction.Transition result = unit.executeAction(orderProcessModel);

        //then
        assertEquals(AbstractSimpleDecisionAction.Transition.OK, result);
        verify(brainTreeTransactionService).createAuthorizationTransaction(any(CommerceOrderParameter.class));
    }

    @Test
    public void shouldCreateAuthorizationTransactionWhenPayPalIntentAuthorize() {
        //given
        when(orderModel.getPaymentInfo()).thenReturn(brainTreePaymentInfoModel);
        when(brainTreePaymentInfoModel.getPayPalIntent()).thenReturn(BraintreeConstants.PayPalIntent.PAYPAL_INTENT_AUTHORIZE);

        //when
        AbstractSimpleDecisionAction.Transition result = unit.executeAction(orderProcessModel);

        //then
        assertEquals(AbstractSimpleDecisionAction.Transition.OK, result);
        verify(brainTreeTransactionService).createAuthorizationTransaction(any(CommerceOrderParameter.class));
    }

    @Test
    public void shouldSetOrderStatusPaymentAuthorizedWhenPayPalSale() {
        //given
        when(orderModel.getPaymentInfo()).thenReturn(brainTreePaymentInfoModel);
        when(brainTreePaymentInfoModel.getPayPalIntent()).thenReturn(BraintreeConstants.PayPalIntent.PAYPAL_INTENT_SALE);
        when(orderModel.getPaymentTransactions()).thenReturn(List.of(paymentTransactionModel));
        when(paymentTransactionModel.getEntries()).thenReturn(List.of(paymentTransactionEntryModel));
        when(paymentTransactionEntryModel.getType()).thenReturn(PaymentTransactionType.AUTHORIZATION);
        when(paymentTransactionEntryModel.getTransactionStatus()).thenReturn(TransactionStatus.ACCEPTED.name());

        //when
        AbstractSimpleDecisionAction.Transition result = unit.executeAction(orderProcessModel);

        //then
        assertEquals(AbstractSimpleDecisionAction.Transition.OK, result);
        verify(orderModel).setStatus(OrderStatus.PAYMENT_AUTHORIZED);
        verify(modelService).save(orderModel);
    }

    @Test
    public void shouldNotSetOrderStatusPaymentAuthorizedWhenPayPalSaleIfTransactionNotAccepted() {
        //given
        when(orderModel.getPaymentInfo()).thenReturn(brainTreePaymentInfoModel);
        when(orderModel.getPaymentTransactions()).thenReturn(List.of(paymentTransactionModel));
        when(paymentTransactionModel.getEntries()).thenReturn(List.of(paymentTransactionEntryModel));
        when(paymentTransactionEntryModel.getType()).thenReturn(PaymentTransactionType.AUTHORIZATION);
        when(paymentTransactionEntryModel.getTransactionStatus()).thenReturn(TransactionStatus.REJECTED.name());

        //when
        AbstractSimpleDecisionAction.Transition result = unit.executeAction(orderProcessModel);

        //then
        assertEquals(AbstractSimpleDecisionAction.Transition.NOK, result);
        verify(orderModel, never()).setStatus(any(OrderStatus.class));
        verify(modelService, never()).save(any(OrderModel.class));
    }

    @Test
    public void shouldNotSetOrderStatusPaymentAuthorizedWhenPayPalSaleIfTransactionNotAuthorization() {
        //given
        when(orderModel.getPaymentInfo()).thenReturn(brainTreePaymentInfoModel);
        when(orderModel.getPaymentTransactions()).thenReturn(List.of(paymentTransactionModel));
        when(paymentTransactionModel.getEntries()).thenReturn(List.of(paymentTransactionEntryModel));
        when(paymentTransactionEntryModel.getType()).thenReturn(PaymentTransactionType.CAPTURE);

        //when
        AbstractSimpleDecisionAction.Transition result = unit.executeAction(orderProcessModel);

        //then
        assertEquals(AbstractSimpleDecisionAction.Transition.NOK, result);
        verify(orderModel, never()).setStatus(any(OrderStatus.class));
        verify(modelService, never()).save(any(OrderModel.class));
    }

}

