package com.braintree.order.service.impl;

import com.braintree.helpers.OrderVoidHelper;
import com.braintreegateway.CreditCard.CardType;
import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.core.enums.OrderStatus;
import de.hybris.platform.core.model.order.OrderModel;
import de.hybris.platform.core.model.order.payment.PaymentInfoModel;
import de.hybris.platform.payment.enums.PaymentTransactionType;
import de.hybris.platform.payment.model.PaymentTransactionEntryModel;
import de.hybris.platform.payment.model.PaymentTransactionModel;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

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

@UnitTest
public class DefaultBraintreeOrderBackofficeUtilServiceTest {

    @Mock
    private OrderVoidHelper orderVoidHelper;

    @InjectMocks
    private DefaultBraintreeOrderBackofficeUtilService unit;

    @Mock
    private OrderModel orderModel;
    @Mock
    private PaymentTransactionModel transaction;

    @Mock
    private PaymentTransactionEntryModel transactionEntry1;

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

    @Test
    public void isOrderAmountCapturedShouldReturnFalseIfNotCapturedAllAmount() {
        List<PaymentTransactionModel> transactions = new ArrayList<>();
        transactions.add(transaction);
        List<PaymentTransactionEntryModel> entries = new ArrayList<>();
        PaymentTransactionEntryModel entry = mock(PaymentTransactionEntryModel.class);
        entries.add(entry);

        when(orderModel.getPaymentTransactions()).thenReturn(transactions);
        when(transaction.getEntries()).thenReturn(entries);
        when(entry.getType()).thenReturn(PaymentTransactionType.PARTIAL_CAPTURE);
        when(entry.getAmount()).thenReturn(BigDecimal.ONE);
        when(orderModel.getTotalPrice()).thenReturn(BigDecimal.TEN.doubleValue());
        when(orderVoidHelper.isSuccessfulCaptureTransaction(entry)).thenReturn(true);

        assertFalse(unit.isOrderAmountCaptured(orderModel));
    }

    @Test
    public void isOrderAmountCapturedShouldReturnTrueWhenOrderCaptured() {
        List<PaymentTransactionModel> transactions = new ArrayList<>();
        transactions.add(transaction);
        List<PaymentTransactionEntryModel> entries = new ArrayList<>();
        PaymentTransactionEntryModel entry = mock(PaymentTransactionEntryModel.class);
        entries.add(entry);

        when(orderModel.getPaymentTransactions()).thenReturn(transactions);
        when(transaction.getEntries()).thenReturn(entries);
        when(entry.getType()).thenReturn(PaymentTransactionType.CAPTURE);
        when(entry.getAmount()).thenReturn(BigDecimal.TEN);
        when(orderModel.getTotalPrice()).thenReturn(BigDecimal.TEN.doubleValue());
        when(orderVoidHelper.isSuccessfulCaptureTransaction(entry)).thenReturn(true);

        assertTrue(unit.isOrderAmountCaptured(orderModel));
    }

    @Test
    public void shouldReturnTrueWhenOrderStatusCanceled() {
        when(orderModel.getStatus()).thenReturn(OrderStatus.CANCELLED);

        assertTrue(unit.isOrderVoided(orderModel));
    }

    @Test
    public void shouldReturnFalseWhenOrderStatusNotCanceled() {
        when(orderModel.getStatus()).thenReturn(OrderStatus.ORDER_SPLIT);

        assertFalse(unit.isOrderVoided(orderModel));
    }

    @Test
    public void paidByCardShouldReturnFalseIfCardTypePropertyNull() {
       PaymentInfoModel paymentInfo = mock(PaymentInfoModel.class);

        when(orderModel.getPaymentInfo()).thenReturn(paymentInfo);
        when(paymentInfo.getProperty("cardType")).thenReturn(null);

        assertFalse(unit.paidByCard(orderModel));
    }

    @Test
    public void paidByCardShouldReturnTrueIfCardTypePropertyNotNull() {
        PaymentInfoModel paymentInfo = mock(PaymentInfoModel.class);

        when(orderModel.getPaymentInfo()).thenReturn(paymentInfo);
        when(paymentInfo.getProperty("cardType")).thenReturn(CardType.MAESTRO);

        assertTrue(unit.paidByCard(orderModel));
    }

}
