package com.braintree.customersupportbackoffice.editor;

import com.hybris.cockpitng.editors.EditorListener;
import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.core.model.BraintreePluginConfigurationModel;
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 com.hybris.cockpitng.editors.EditorContext;
import de.hybris.platform.core.model.BraintreeACHConfigurationModel;
import de.hybris.platform.core.model.BraintreeGeneralConfigurationModel;
import org.zkoss.zul.Button;

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 BrainTreeACHConfigurationSavedAccountLimitEditorTest {

    private static final String PARENT_OBJECT = "parentObject";

    @Mock
    private Component parent;
    @Mock
    private EditorContext<Object> context;
    @Mock
    private EditorListener<Object> listener;
    @Mock
    private BraintreeACHConfigurationModel achConfigurationModel;
    @Mock
    private BraintreePluginConfigurationModel pluginConfigurationModel;
    @Mock
    private BraintreeGeneralConfigurationModel generalConfigurationModel;
    @Mock
    private Button disabled;
    @Spy
    @InjectMocks
    private BrainTreeACHConfigurationSavedAccountLimitEditor editor;

    @Test
    public void shouldSetConfigToDisabled() {
        setUpCommonBraintreeConfiguration(false);
        when(parent.getFirstChild()).thenReturn(disabled);
        doNothing().when(disabled).setDisabled(true);
        doNothing().when(editor).callParentRender(parent, context, listener);

        editor.render(parent, context, listener);

        verify(parent).getFirstChild();
        verify(achConfigurationModel).isEnabled();
        verify(disabled).setDisabled(true);
    }

    @Test
    public void shouldNotSetConfigToDisabled() {
        setUpCommonBraintreeConfiguration(true);
        when(generalConfigurationModel.isStoreInVault()).thenReturn(true);
        when(generalConfigurationModel.isDropIn()).thenReturn(false);

        editor.disableConfig(parent, context);

        verify(parent, never()).getFirstChild();
        verify(achConfigurationModel).isEnabled();
        verify(disabled, never()).setDisabled(true);
    }

    private void setUpCommonBraintreeConfiguration(boolean isACHEnabled) {
        when(context.getParameter(PARENT_OBJECT)).thenReturn(achConfigurationModel);
        when(achConfigurationModel.getPluginConfiguration()).thenReturn(pluginConfigurationModel);
        when(pluginConfigurationModel.getBraintreeGeneralConfiguration()).thenReturn(generalConfigurationModel);
        when(achConfigurationModel.isEnabled()).thenReturn(isACHEnabled);
    }
}
