package com.braintree.customersupportbackoffice.handler;

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.BraintreeACHConfigurationModel;
import de.hybris.platform.core.model.BraintreeGeneralConfigurationModel;

public class BraintreeACHConfigurationLogicHandler extends BackofficeEditorAreaLogicHandler {

    public static final String ACH_CONFIGURATION_DISABLED = "achConfigurationDisabled";
    private static final String ACH_ACCOUNT_LIMITATION_DISABLED = "achAccountLimitationDisabled";

    private NotificationService notificationService;

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

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

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