package com.braintree.customersupportbackoffice.actions.order.partialrefund;

import com.braintree.facade.backoffice.BraintreeBackofficePartialRefundFacade;
import com.braintree.model.BrainTreePaymentInfoModel;
import com.hybris.cockpitng.actions.ActionContext;
import com.hybris.cockpitng.actions.ActionResult;
import com.hybris.cockpitng.engine.impl.ComponentWidgetAdapter;
import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.basecommerce.model.site.BaseSiteModel;
import de.hybris.platform.core.model.order.OrderModel;
import de.hybris.platform.core.model.order.payment.CreditCardPaymentInfoModel;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

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

@UnitTest
@RunWith(MockitoJUnitRunner.class)
public class BrainTreePartialRefundOrderActionTest {
    @Mock
    private BraintreeBackofficePartialRefundFacade braintreeBackofficePartialRefundFacade;
    @Mock
    private ComponentWidgetAdapter componentWidgetAdapter;

    @InjectMocks
    private BrainTreePartialRefundOrderAction unit;

    @Mock
    private ActionContext<OrderModel> actionContext;
    @Mock
    private OrderModel order;
    @Mock
    private BrainTreePaymentInfoModel braintreePaymentInfo;
    @Mock
    private CreditCardPaymentInfoModel creditCardPaymentInfo;

    @Test
    public void shouldReturnTrueIfPartialRefundPossible() {
        when(actionContext.getData()).thenReturn(order);
        when(order.getPaymentInfo()).thenReturn(braintreePaymentInfo);
        when(braintreeBackofficePartialRefundFacade.isPartialRefundPossible(order)).thenReturn(true);

        boolean result = unit.canPerform(actionContext);

        assertTrue(result);
    }

    @Test
    public void shouldSetHiddenViewModeForNonBraintreePaymentInfo() {
        when(actionContext.getData()).thenReturn(order);
        when(order.getPaymentInfo()).thenReturn(creditCardPaymentInfo);
        when(braintreeBackofficePartialRefundFacade.isPartialRefundPossible(order)).thenReturn(true);

        unit.canPerform(actionContext);

        verify(actionContext).setParameter(ActionContext.VIEW_MODE_PARAM, ActionContext.VIEWMODE_HIDDEN);
    }

    @Test
    public void shouldReturnFalseIfActionContextDoesNotHaveOrder() {
        boolean result = unit.canPerform(actionContext);

        assertFalse(result);
    }

    @Test
    public void shouldSuccessfullyPerform() {
        ActionResult<OrderModel> result = unit.perform(actionContext);

        assertEquals(ActionResult.SUCCESS, result.getResultCode());
    }
}