package com.braintree.customersupportbackoffice.handler;

import com.braintree.enums.BraintreePaymentMethodLimit;
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.BraintreePayPalConfigurationModel;
import de.hybris.platform.servicelayer.model.ModelService;

public class BraintreePayPalConfigurationLogicHandler extends BackofficeEditorAreaLogicHandler {
    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";

    private NotificationService notificationService;
    private ModelService modelService;

    @Override
    public Object performSave(WidgetInstanceManager widgetInstanceManager, Object currentObject) throws ObjectSavingException {
        BraintreePayPalConfigurationModel braintreePayPalConfigurationModel = (BraintreePayPalConfigurationModel) currentObject;
        if (!braintreePayPalConfigurationModel.isPayPalEnabled()) {
            notificationService.notifyUser(widgetInstanceManager, PAYPAL_CONFIGURATION_DISABLED,
                    NotificationEvent.Level.INFO);
        }
        if (isChangePaymentMethodEnabled(braintreePayPalConfigurationModel)) {
            notificationService.notifyUser(widgetInstanceManager, CHANGE_PAYMENT_METHOD_ENABLED,
                    NotificationEvent.Level.INFO);
        }
        if (isPayPalLimitationChanged(braintreePayPalConfigurationModel)
                && braintreePayPalConfigurationModel.getSavedPayPalAccountLimit() != BraintreePaymentMethodLimit.ONE) {
            notificationService.notifyUser(widgetInstanceManager, PAYPAL_ACCOUNT_LIMIT_INCREASED,
                    NotificationEvent.Level.INFO);
        }

        return super.performSave(widgetInstanceManager, currentObject);
    }

    @Override
    public void beforeEditorAreaRender(WidgetInstanceManager widgetInstanceManager, Object currentObject) {
        BraintreePayPalConfigurationModel braintreePayPalConfigurationModel = (BraintreePayPalConfigurationModel) currentObject;
        final BraintreeGeneralConfigurationModel generalConfigurationModel = braintreePayPalConfigurationModel.getPluginConfiguration()
                .getBraintreeGeneralConfiguration();
        if (!braintreePayPalConfigurationModel.isPayPalEnabled()) {
            notificationService.notifyUser(widgetInstanceManager, PAYPAL_CONFIGURATION_DISABLED,
                    NotificationEvent.Level.WARNING);
        } else if (generalConfigurationModel.isDropIn()) {
            notificationService.notifyUser(widgetInstanceManager, PAYPAL_ACCOUNT_LIMITATION_DISABLED,
                    NotificationEvent.Level.WARNING);
        } else if (braintreePayPalConfigurationModel.getSavedPayPalAccountLimit() != BraintreePaymentMethodLimit.ONE) {
            notificationService.notifyUser(widgetInstanceManager, PAYPAL_ACCOUNT_LIMIT_INCREASED_POP_UP,
                    NotificationEvent.Level.WARNING);
        }
        super.beforeEditorAreaRender(widgetInstanceManager, currentObject);
    }

    private boolean isChangePaymentMethodEnabled(BraintreePayPalConfigurationModel braintreePayPalConfigurationModel) {
        BraintreePayPalConfigurationModel originalConfiguration = modelService.get(braintreePayPalConfigurationModel.getPk());
        return braintreePayPalConfigurationModel.isChangePaymentMethodButton() && !originalConfiguration.isChangePaymentMethodButton();
    }

    private boolean isPayPalLimitationChanged(BraintreePayPalConfigurationModel braintreePayPalConfigurationModel) {
        BraintreePayPalConfigurationModel originalConfiguration = modelService.get(braintreePayPalConfigurationModel.getPk());
        BraintreePaymentMethodLimit originalLimit = originalConfiguration.getSavedPayPalAccountLimit();
        BraintreePaymentMethodLimit currentLimit = braintreePayPalConfigurationModel.getSavedPayPalAccountLimit();
        return originalLimit != currentLimit;
    }

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

    public void setModelService(ModelService modelService) {
        this.modelService = modelService;
    }
}
