package com.braintree.customersupportbackoffice.handler;

import com.braintree.enums.BraintreeThreeDSecureFlow;
import com.braintree.enums.BraintreeThreeDSecureSCAExemptionType;
import com.hybris.backoffice.editorarea.BackofficeEditorAreaLogicHandler;
import com.hybris.cockpitng.dataaccess.facades.object.exceptions.ObjectSavingException;
import com.hybris.cockpitng.engine.WidgetInstanceManager;
import com.hybris.cockpitng.util.notifications.NotificationService;
import com.hybris.cockpitng.util.notifications.event.NotificationEvent;
import de.hybris.platform.core.model.BraintreeGeneralConfigurationModel;
import de.hybris.platform.core.model.BraintreeHostedFieldsConfigurationModel;

public class BraintreeHostedFieldsConfigurationLogicHandler extends BackofficeEditorAreaLogicHandler {

    private static final String HOSTED_FIELDS_CONFIGURATION_DISABLED = "hostedFieldsConfigurationDisabled";
    private static final String HOSTED_FIELDS_ACCOUNT_LIMITATION_DISABLED = "hostedFieldsAccountLimitationDisabled";

    private NotificationService notificationService;

    @Override
    public Object performSave(WidgetInstanceManager widgetInstanceManager, Object currentObject) throws ObjectSavingException {
        BraintreeHostedFieldsConfigurationModel braintreeHostedFieldsConfigurationModel = (BraintreeHostedFieldsConfigurationModel) currentObject;
        if (!braintreeHostedFieldsConfigurationModel.isEnabled()) {
            notificationService.notifyUser(widgetInstanceManager, HOSTED_FIELDS_CONFIGURATION_DISABLED,
                    NotificationEvent.Level.INFO);
        }
        return super.performSave(widgetInstanceManager, currentObject);
    }

    @Override
    public void beforeEditorAreaRender(WidgetInstanceManager widgetInstanceManager, Object currentObject) {
        BraintreeHostedFieldsConfigurationModel braintreeHostedFieldsConfigurationModel = (BraintreeHostedFieldsConfigurationModel) currentObject;
        final BraintreeGeneralConfigurationModel generalConfigurationModel = braintreeHostedFieldsConfigurationModel.getPluginConfiguration()
                .getBraintreeGeneralConfiguration();
        if (!braintreeHostedFieldsConfigurationModel.isEnabled()) {
            notificationService.notifyUser(widgetInstanceManager, HOSTED_FIELDS_CONFIGURATION_DISABLED,
                    NotificationEvent.Level.WARNING);
        }
        if (generalConfigurationModel.isDropIn()) {
            notificationService.notifyUser(widgetInstanceManager, HOSTED_FIELDS_ACCOUNT_LIMITATION_DISABLED,
                    NotificationEvent.Level.WARNING);
        }
        super.beforeEditorAreaRender(widgetInstanceManager, currentObject);
    }

    public void setNotificationService(NotificationService notificationService) {
        this.notificationService = notificationService;
    }
}
