package com.braintree.customersupportbackoffice.services.impl;

import com.braintree.configuration.service.BrainTreeConfigService;
import com.braintree.model.BrainTreePaymentInfoModel;
import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.core.enums.OrderStatus;
import de.hybris.platform.core.model.order.AbstractOrderEntryModel;
import de.hybris.platform.core.model.order.OrderModel;
import de.hybris.platform.core.model.order.payment.PaymentInfoModel;
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.returns.ReturnService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

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

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


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

    @InjectMocks
    private BrainTreeOrderManagementActionsServiceImpl unit;
    @Mock
    private BrainTreeConfigService brainTreeConfigService;
    @Mock
    private ReturnService returnService;
    @Mock
    private OrderModel order;
    @Mock
    private BrainTreePaymentInfoModel paymentInfo;
    @Mock
    private PaymentTransactionModel paymentTransaction;
    @Mock
    private PaymentTransactionEntryModel transactionEntry;

    @Test
    public void shouldIsMultipleCapturePossibleWhenOrderIsNullReturnFalse() {
        assertFalse(unit.isMultipleCapturePossible(null));
    }

    @Test
    public void shouldIsMultipleCapturePossibleWhenPaymentInfoIsNotBrainTreeReturnFalse() {
        when(order.getPaymentInfo()).thenReturn(mock(PaymentInfoModel.class));
        assertFalse(unit.isMultipleCapturePossible(order));
    }

    @Test
    public void shouldIsPartialRefundPossibleWhenOrderIsNullReturnFalse() {
        assertFalse(unit.isPartialRefundPossible(null));
    }

    @Test
    public void shouldIsPartialRefundPossibleWhenNoCapturedTransactionsShouldReturnFalse() {
        when(order.getPaymentTransactions()).thenReturn(List.of());
        assertFalse(unit.isPartialRefundPossible(order));
    }

    @Test
    public void shouldIsMultiCaptureEnabledWhenMultiCaptureEnabledReturnTrue() {
        when(brainTreeConfigService.isMultiCaptureEnabled()).thenReturn(true);
        assertTrue(unit.isMultiCaptureEnabled());
    }

    @Test
    public void shouldIsMultiCaptureEnabledWhenMultiCaptureDisabledReturnFalse() {
        when(brainTreeConfigService.isMultiCaptureEnabled()).thenReturn(false);
        assertFalse(unit.isMultiCaptureEnabled());
    }

    @Test
    public void shouldIsRefundPossibleWhenOrderIsAuthorizedAndCompletedReturnTrue() {
        when(order.getStatus()).thenReturn(OrderStatus.COMPLETED);
        when(returnService.getAllReturnableEntries(order)).thenReturn(Map.of(mock(AbstractOrderEntryModel.class), 1L));
        assertTrue(unit.isRefundPossible(order));
    }

    @Test
    public void shouldIsReplacePossibleWhenOrderHasVersionIDReturnFalse() {
        when(order.getVersionID()).thenReturn("someVersionId");
        assertFalse(unit.isReplacePossible(order));
    }

    @Test
    public void shouldIsReplacePossibleWhenOrderHasNoVersionIDReturnTrue() {
        when(order.getVersionID()).thenReturn("");
        assertTrue(unit.isReplacePossible(order));
    }

}
