package com.braintree.customersupportbackoffice.renderers;

import com.braintree.enums.BraintreeApplePayColor;
import com.braintree.enums.BraintreeApplePayShape;
import com.braintree.enums.BraintreeApplePayType;
import com.hybris.cockpitng.core.config.impl.jaxb.editorarea.AbstractPanel;
import com.hybris.cockpitng.core.model.ModelObserver;
import com.hybris.cockpitng.core.model.WidgetModel;
import com.hybris.cockpitng.dataaccess.facades.type.DataType;
import com.hybris.cockpitng.engine.WidgetInstanceManager;
import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.core.model.BraintreeApplePayStylesConfigurationModel;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
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.Html;

import java.util.List;

import static com.hybris.backoffice.workflow.designer.handler.WorkflowDesignerCloseListener.VALUE_CHANGED;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

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

    private static final String APPLE_PAY_BUTTON = "<apple-pay-button buttonstyle='black' type='check-out' locale='en-US'></apple-pay-button>";
    private static final String APPLE_PAY_BUTTON_PILL_SHAPE = "--apple-pay-button-border-radius: 15pt";
    private static final String APPLE_PAY_BUTTON_SQUARE_SHAPE = "--apple-pay-button-border-radius: 0pt";
    private static final String CURRENT_OBJECT_DOTLESS = "currentObject";

    @Spy
    @InjectMocks
    private ApplePayPreviewButtonRenderer unit;
    @Mock
    private Component component;
    @Mock
    private AbstractPanel abstractPanel;
    @Mock
    private BraintreeApplePayStylesConfigurationModel stylesConfiguration;
    @Mock
    private DataType dataType;
    @Mock
    private WidgetInstanceManager widgetInstanceManager;
    @Mock
    private WidgetModel widgetModel;
    @Mock
    private Html childHtml;
    @Mock
    private Html applePayHtml;

    @Captor
    private ArgumentCaptor<ModelObserver> modelObserverArgumentCaptor;

    @Test
    public void shouldRenderApplePayButton() {
        doReturn(applePayHtml).when(unit).createHtml(any());

        when(widgetInstanceManager.getModel()).thenReturn(widgetModel);
        when(stylesConfiguration.getColor()).thenReturn(BraintreeApplePayColor.BLACK);
        when(stylesConfiguration.getType()).thenReturn(BraintreeApplePayType.CHECK_OUT);
        when(stylesConfiguration.getShape()).thenReturn(BraintreeApplePayShape.PILL);

        unit.render(component, abstractPanel, stylesConfiguration, dataType, widgetInstanceManager);

        verify(unit).createHtml(APPLE_PAY_BUTTON);
        verify(applePayHtml).setStyle(APPLE_PAY_BUTTON_PILL_SHAPE);
    }

    @Test
    public void shouldRenderApplePayButtonWithSquareShape() {
        doReturn(applePayHtml).when(unit).createHtml(any());

        when(widgetInstanceManager.getModel()).thenReturn(widgetModel);
        when(stylesConfiguration.getColor()).thenReturn(BraintreeApplePayColor.BLACK);
        when(stylesConfiguration.getType()).thenReturn(BraintreeApplePayType.CHECK_OUT);
        when(stylesConfiguration.getShape()).thenReturn(BraintreeApplePayShape.SQUARE);

        unit.render(component, abstractPanel, stylesConfiguration, dataType, widgetInstanceManager);

        verify(unit).createHtml(APPLE_PAY_BUTTON);
        verify(applePayHtml).setStyle(APPLE_PAY_BUTTON_SQUARE_SHAPE);
    }

    @Test
    public void shouldAddObserver() {
        doReturn(applePayHtml).when(unit).createHtml(any());

        when(widgetInstanceManager.getModel()).thenReturn(widgetModel);
        when(widgetModel.getValue(CURRENT_OBJECT_DOTLESS, BraintreeApplePayStylesConfigurationModel.class)).thenReturn(stylesConfiguration);
        when(component.getChildren()).thenReturn(List.of(childHtml));
        when(stylesConfiguration.getColor()).thenReturn(BraintreeApplePayColor.BLACK);
        when(stylesConfiguration.getType()).thenReturn(BraintreeApplePayType.CHECK_OUT);
        when(stylesConfiguration.getShape()).thenReturn(BraintreeApplePayShape.SQUARE);

        unit.render(component, abstractPanel, stylesConfiguration, dataType, widgetInstanceManager);

        verify(widgetModel).addObserver(eq(VALUE_CHANGED), modelObserverArgumentCaptor.capture());
        ModelObserver value = modelObserverArgumentCaptor.getValue();
        value.modelChanged();
        verify(component).removeChild(childHtml);
    }

}