package com.braintree.customersupportbackoffice.editor;

import com.braintree.enums.BraintreeThreeDSecureFlow;
import com.braintree.enums.BraintreeThreeDSecureSCAExemptionType;
import com.hybris.cockpitng.editors.EditorContext;
import com.hybris.cockpitng.editors.EditorListener;
import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.core.model.BraintreeGeneralConfigurationModel;
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.zk.ui.Component;
import org.zkoss.zul.Div;
import org.zkoss.zul.Radiogroup;

import java.util.List;

import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

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

    private static final String PARENT_OBJECT = "parentObject";

    @Mock
    private Component parent;
    @Mock
    private EditorContext<Boolean> context;
    @Mock
    private EditorListener<Boolean> listener;
    @Mock
    private BraintreeGeneralConfigurationModel generalConfigurationModel;
    @Mock
    private Radiogroup radiogroup;
    @Mock
    private List<Component> divList;
    @Mock
    private Div div;
    @Spy
    @InjectMocks
    private BrainTreeGeneralConfigurationBooleanEditor editor;

    @Test
    public void shouldSetConfigToDisabled() {
        setUpCommonBraintreeConfiguration(BraintreeThreeDSecureFlow.DISABLED);
        when(parent.getFirstChild()).thenReturn(radiogroup);
        when(radiogroup.getChildren()).thenReturn(divList);
        when(divList.get(0)).thenReturn(div);
        doNothing().when(editor).setDisabled(div);
        doNothing().when(editor).callParentRender(parent, context, listener);

        editor.render(parent, context, listener);

        verify(parent).getFirstChild();
        verify(generalConfigurationModel).getBraintreeThreeDSecureFlow();
        verify(radiogroup).getChildren();
        verify(editor).setDisabled(div);
    }

    @Test
    public void shouldNotSetConfigToDisabled() {
        setUpCommonBraintreeConfiguration(BraintreeThreeDSecureFlow.ALWAYS);

        editor.disableConfig(parent, context);

        verify(parent, never()).getFirstChild();
        verify(generalConfigurationModel).getBraintreeThreeDSecureFlow();
        verify(editor, never()).setDisabled(div);
    }

    private void setUpCommonBraintreeConfiguration(BraintreeThreeDSecureFlow threeDSecureFlow) {
        when(context.getParameter(PARENT_OBJECT)).thenReturn(generalConfigurationModel);
        when(generalConfigurationModel.getBraintreeThreeDSecureFlow()).thenReturn(threeDSecureFlow);
        when(generalConfigurationModel.getBraintreeThreeDSecureSCAExemption()).thenReturn(BraintreeThreeDSecureSCAExemptionType.DISABLED);
    }
}
