package com.braintree.customersupportbackoffice.handler;


import com.braintree.enums.BraintreePaymentMethodLimit;
import com.hybris.cockpitng.dataaccess.facades.object.ObjectFacade;
import com.hybris.cockpitng.engine.WidgetInstanceManager;
import com.hybris.cockpitng.util.notifications.NotificationService;
import com.hybris.cockpitng.util.notifications.event.NotificationEvent;
import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.core.model.BraintreeGeneralConfigurationModel;
import de.hybris.platform.core.model.BraintreePayPalConfigurationModel;
import de.hybris.platform.core.model.BraintreePluginConfigurationModel;
import de.hybris.platform.servicelayer.model.ModelService;
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.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.never;

@UnitTest
@RunWith(MockitoJUnitRunner.class)
public class BraintreePayPalConfigurationLogicHandlerTest {
    private static final String CHANGE_PAYMENT_METHOD_ENABLED = "changePaymentMethodEnabled";
    private static final String PAYPAL_CONFIGURATION_DISABLED = "payPalConfigurationDisabled";
    private static final String PAYPAL_ACCOUNT_LIMITATION_DISABLED = "payPalAccountLimitationDisabled";
    private static final String PAYPAL_ACCOUNT_LIMIT_INCREASED = "payPalAccountLimitIncreased";
    private static final String PAYPAL_ACCOUNT_LIMIT_INCREASED_POP_UP = "payPalAccountLimitIncreasedPopUp";
    @Mock
    private NotificationService notificationService;
    @Mock
    private ObjectFacade objectFacade;
    @Mock
    private WidgetInstanceManager widgetInstanceManager;
    @Mock
    private ModelService modelService;
    @Mock
    private BraintreeGeneralConfigurationModel generalConfigurationModel;
    @Mock
    private BraintreePluginConfigurationModel pluginConfigurationModel;
    @Mock
    private BraintreePayPalConfigurationModel braintreePayPalConfigurationModel;
    @Mock
    private BraintreePayPalConfigurationModel originalConfiguration;
    @InjectMocks
    private BraintreePayPalConfigurationLogicHandler unit;

    @Test
    public void shouldPerformSaveWhenPayPalIsDisabled() throws Exception {
        when(braintreePayPalConfigurationModel.isPayPalEnabled()).thenReturn(false);
        when(modelService.get(braintreePayPalConfigurationModel.getPk())).thenReturn(originalConfiguration);
        unit.performSave(widgetInstanceManager,braintreePayPalConfigurationModel);
        verify(notificationService).notifyUser(widgetInstanceManager, PAYPAL_CONFIGURATION_DISABLED,
                NotificationEvent.Level.INFO);
    }

    @Test
    public void shouldPerformSaveWhenPayPalLimitationIsChanged() throws Exception {
        when(braintreePayPalConfigurationModel.isPayPalEnabled()).thenReturn(true);
        when(modelService.get(braintreePayPalConfigurationModel.getPk())).thenReturn(originalConfiguration);
        when(originalConfiguration.getSavedPayPalAccountLimit()).thenReturn(BraintreePaymentMethodLimit.ONE);
        when(braintreePayPalConfigurationModel.getSavedPayPalAccountLimit()).thenReturn(BraintreePaymentMethodLimit.TWO);
        unit.performSave(widgetInstanceManager,braintreePayPalConfigurationModel);
        verify(notificationService).notifyUser(widgetInstanceManager, PAYPAL_ACCOUNT_LIMIT_INCREASED,
                NotificationEvent.Level.INFO);
    }

    @Test
    public void shouldPerformSaveWhenChangePaymentMethodIsEnabled() throws Exception {
        when(braintreePayPalConfigurationModel.isChangePaymentMethodButton()).thenReturn(true);
        when(modelService.get(braintreePayPalConfigurationModel.getPk())).thenReturn(originalConfiguration);
        when(originalConfiguration.isChangePaymentMethodButton()).thenReturn(false);
        unit.performSave(widgetInstanceManager,braintreePayPalConfigurationModel);
        verify(notificationService).notifyUser(widgetInstanceManager, CHANGE_PAYMENT_METHOD_ENABLED,
                NotificationEvent.Level.INFO);
    }

    @Test
    public void shouldBeforeEditorAreaRenderWhenPayPalIsDisabled() throws Exception {
        when(braintreePayPalConfigurationModel.getPluginConfiguration()).thenReturn(pluginConfigurationModel);
        when(pluginConfigurationModel.getBraintreeGeneralConfiguration()).thenReturn(generalConfigurationModel);
        when(braintreePayPalConfigurationModel.isPayPalEnabled()).thenReturn(false);
        unit.beforeEditorAreaRender(widgetInstanceManager,braintreePayPalConfigurationModel);
        verify(notificationService).notifyUser(widgetInstanceManager, PAYPAL_CONFIGURATION_DISABLED,
                NotificationEvent.Level.WARNING);
    }

    @Test
    public void shouldBeforeEditorAreaRenderWhenDropInIsEnabled() throws Exception {
        when(braintreePayPalConfigurationModel.getPluginConfiguration()).thenReturn(pluginConfigurationModel);
        when(pluginConfigurationModel.getBraintreeGeneralConfiguration()).thenReturn(generalConfigurationModel);
        when(braintreePayPalConfigurationModel.isPayPalEnabled()).thenReturn(true);
        when(generalConfigurationModel.isDropIn()).thenReturn(true);
        unit.beforeEditorAreaRender(widgetInstanceManager,braintreePayPalConfigurationModel);
        verify(notificationService).notifyUser(widgetInstanceManager, PAYPAL_ACCOUNT_LIMITATION_DISABLED,
                NotificationEvent.Level.WARNING);
    }

    @Test
    public void shouldBeforeEditorAreaRenderWhenPaymentMethodLimitIsMoreThanOne() throws Exception {
        when(braintreePayPalConfigurationModel.getPluginConfiguration()).thenReturn(pluginConfigurationModel);
        when(pluginConfigurationModel.getBraintreeGeneralConfiguration()).thenReturn(generalConfigurationModel);
        when(braintreePayPalConfigurationModel.isPayPalEnabled()).thenReturn(true);
        when(generalConfigurationModel.isDropIn()).thenReturn(false);
        when(braintreePayPalConfigurationModel.getSavedPayPalAccountLimit()).thenReturn(BraintreePaymentMethodLimit.TWO);
        unit.beforeEditorAreaRender(widgetInstanceManager,braintreePayPalConfigurationModel);
        verify(notificationService).notifyUser(widgetInstanceManager, PAYPAL_ACCOUNT_LIMIT_INCREASED_POP_UP,
                NotificationEvent.Level.WARNING);
    }

    @Test
    public void shouldNotBeforeEditorAreaRenderWhenPaymentMethodLimitIsOne() throws Exception {
        when(braintreePayPalConfigurationModel.getPluginConfiguration()).thenReturn(pluginConfigurationModel);
        when(pluginConfigurationModel.getBraintreeGeneralConfiguration()).thenReturn(generalConfigurationModel);
        when(braintreePayPalConfigurationModel.isPayPalEnabled()).thenReturn(true);
        when(generalConfigurationModel.isDropIn()).thenReturn(false);
        when(braintreePayPalConfigurationModel.getSavedPayPalAccountLimit()).thenReturn(BraintreePaymentMethodLimit.ONE);
        unit.beforeEditorAreaRender(widgetInstanceManager,braintreePayPalConfigurationModel);
        verify(notificationService, never()).notifyUser(widgetInstanceManager, PAYPAL_ACCOUNT_LIMIT_INCREASED_POP_UP,
                NotificationEvent.Level.WARNING);
    }

}
