package com.braintree.customersupportbackoffice.actions.transaction.voidaction;

import com.braintree.customersupportbackoffice.facade.BrainTreeCustomerSupportFacade;
import com.braintree.customersupportbackoffice.services.BrainTreeTransactionManagementActionService;
import com.braintree.hybris.data.BrainTreeResponseResultData;
import com.braintree.model.BrainTreeTransactionDetailModel;
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 org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
import org.zkoss.zul.Messagebox;

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

@UnitTest
@RunWith(MockitoJUnitRunner.class)
public class BrainTreeVoidTransactionActionTest {
    private static final String VOID_ACTION_CONFIRMATION_MESSAGE = "void.action.confirmation.message";
    @Mock
    private BrainTreeCustomerSupportFacade brainTreeCustomerSupportFacade;
    @Mock
    private BrainTreeTransactionManagementActionService brainTreeTransactionManagementActionService;
    @Mock
    private ComponentWidgetAdapter componentWidgetAdapter;

    @Spy
    @InjectMocks
    private BrainTreeVoidTransactionAction unit;

    @Mock
    private ActionContext<BrainTreeTransactionDetailModel> actionContext;
    @Mock
    private BrainTreeTransactionDetailModel transactionDetail;

    @Test
    public void shouldSuccessfullyPerform() {
        doNothing().when(unit).showMessage(null, null, Messagebox.OK, Messagebox.INFORMATION);

        BrainTreeResponseResultData responseResultData = new BrainTreeResponseResultData();
        responseResultData.setSuccess(true);

        when(actionContext.getData()).thenReturn(transactionDetail);
        when(brainTreeCustomerSupportFacade.voidTransaction(transactionDetail)).thenReturn(responseResultData);

        ActionResult<BrainTreeTransactionDetailModel> result = unit.perform(actionContext);

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

    @Test
    public void shouldUnsuccessfullyPerform() {
        doNothing().when(unit).showMessage(null, null, Messagebox.OK, Messagebox.ERROR);

        BrainTreeResponseResultData responseResultData = new BrainTreeResponseResultData();
        responseResultData.setSuccess(false);

        when(actionContext.getData()).thenReturn(transactionDetail);
        when(brainTreeCustomerSupportFacade.voidTransaction(transactionDetail)).thenReturn(responseResultData);

        ActionResult<BrainTreeTransactionDetailModel> result = unit.perform(actionContext);

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

    @Test
    public void shouldReturnTrueIfVoidActionPossible() {
        when(actionContext.getData()).thenReturn(transactionDetail);
        when(brainTreeTransactionManagementActionService.isVoidPossible(transactionDetail)).thenReturn(true);

        boolean result = unit.canPerform(actionContext);

        assertTrue(result);
    }

    @Test
    public void shouldReturnTrueForNeedsConfirmation() {
        boolean result = unit.needsConfirmation(actionContext);

        assertTrue(result);
    }

    @Test
    public void shouldReturnConfirmationMessage() {
        unit.getConfirmationMessage(actionContext);

        verify(actionContext).getLabel(VOID_ACTION_CONFIRMATION_MESSAGE);
    }

}