package com.braintree.customersupportbackoffice.editor;

import com.hybris.cockpitng.editors.EditorContext;
import com.hybris.cockpitng.editors.EditorListener;
import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.core.model.BraintreeHostedFieldsConfigurationModel;
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 BrainTreeHostedFieldsConfigurationBooleanEditorTest {

    private static final String PARENT_OBJECT = "parentObject";

    @Mock
    private Component parent;
    @Mock
    private EditorContext<Boolean> context;
    @Mock
    private BraintreeHostedFieldsConfigurationModel hostedFieldsConfigurationModel;
    @Mock
    private Radiogroup radiogroup;
    @Mock
    private List<Component> divList;
    @Mock
    private EditorListener<Boolean> listener;
    @Mock
    private Div div;
    @Spy
    @InjectMocks
    private BrainTreeHostedFieldsConfigurationBooleanEditor editor;

    @Test
    public void shouldSetConfigToDisabled() {
        setUpCommonBraintreeConfiguration(false);
        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(hostedFieldsConfigurationModel).isEnabled();
        verify(radiogroup).getChildren();
        verify(editor).setDisabled(div);
    }

    @Test
    public void shouldNotSetConfigToDisabled() {
        setUpCommonBraintreeConfiguration(true);

        editor.disableConfig(parent, context);

        verify(parent, never()).getFirstChild();
        verify(hostedFieldsConfigurationModel).isEnabled();
        verify(editor, never()).setDisabled(div);
    }

    private void setUpCommonBraintreeConfiguration(boolean isHostedFieldsEnabled) {
        when(context.getParameter(PARENT_OBJECT)).thenReturn(hostedFieldsConfigurationModel);
        when(hostedFieldsConfigurationModel.isEnabled()).thenReturn(isHostedFieldsEnabled);
    }
}
