package com.braintree.actions.consignment;

import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.commerceservices.model.PickUpDeliveryModeModel;
import de.hybris.platform.core.enums.OrderStatus;
import de.hybris.platform.core.model.order.OrderModel;
import de.hybris.platform.ordersplitting.model.ConsignmentModel;
import de.hybris.platform.ordersplitting.model.ConsignmentProcessModel;
import de.hybris.platform.warehouse.Process2WarehouseAdapter;
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 java.util.NoSuchElementException;
import java.util.Set;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

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

    private static final String DELIVERY = "DELIVERY";
    private static final String PICKUP = "PICKUP";
    private static final String CANCEL = "CANCEL";
    private static final String ERROR = "ERROR";

    @Mock
    private Process2WarehouseAdapter process2WarehouseAdapter;

    @InjectMocks
    private BrainTreeAllowShipmentAction unit;

    @Mock
    private ConsignmentProcessModel process;
    @Mock
    private ConsignmentModel consignment;
    @Mock
    private OrderModel order;
    @Mock
    private PickUpDeliveryModeModel pickUpDeliveryMode;

    @Before
    public void setUp() {
        when(process.getConsignment()).thenReturn(consignment);
        when(consignment.getOrder()).thenReturn(order);
    }

    @Test
    public void shouldReturnErrorIfConsignmentIfNull() {
        //given
        when(process.getConsignment()).thenReturn(null);

        //when
        String result = unit.execute(process);

        //then
        assertEquals(BrainTreeAllowShipmentAction.Transition.ERROR.toString(), result);
    }

    @Test
    public void shouldReturnTransitionCancelIfOrderStatusCANCELLED() {
        //given
        when(order.getStatus()).thenReturn(OrderStatus.CANCELLED);

        //when
        String result = unit.execute(process);

        //then
        assertEquals(BrainTreeAllowShipmentAction.Transition.CANCEL.toString(), result);
    }

    @Test
    public void shouldReturnTransitionCancelIfOrderStatusCANCELLING() {
        //given
        when(order.getStatus()).thenReturn(OrderStatus.CANCELLING);

        //when
        String result = unit.execute(process);

        //then
        assertEquals(BrainTreeAllowShipmentAction.Transition.CANCEL.toString(), result);
    }

    @Test
    public void shouldShipConsignment() {
        //when
        String result = unit.execute(process);

        //then
        assertEquals(BrainTreeAllowShipmentAction.Transition.DELIVERY.toString(), result);
        verify(process2WarehouseAdapter).shipConsignment(consignment);
    }

    @Test
    public void shouldReturnTransitionPickupIfDeliveryModeIsInstanceOfPickUpDeliveryModeModel() {
        //given
        when(consignment.getDeliveryMode()).thenReturn(pickUpDeliveryMode);

        //when
        String result = unit.execute(process);

        //then
        assertEquals(BrainTreeAllowShipmentAction.Transition.PICKUP.toString(), result);
        verify(process2WarehouseAdapter).shipConsignment(consignment);
    }

    @Test
    public void shouldReturnTransitionErrorIfExceptionOccur() {
        //given
        when(consignment.getDeliveryMode()).thenThrow(NoSuchElementException.class);

        //when
        String result = unit.execute(process);

        //then
        assertEquals(BrainTreeAllowShipmentAction.Transition.ERROR.toString(), result);
    }

    @Test
    public void shouldGetTransition() {
        //when
        Set<String> transitions = unit.getTransitions();

        //then
        assertTrue(transitions.containsAll(List.of(DELIVERY, PICKUP, CANCEL, ERROR)));
    }

}
