package com.braintree.email.listeners;

import com.braintree.email.emailEvents.BraintreeLocalPaymentReversedEmailEvent;
import com.braintree.model.BraintreeLocalPaymentReversedEmailProcessModel;
import de.hybris.platform.acceleratorservices.site.AbstractAcceleratorSiteEventListener;
import de.hybris.platform.basecommerce.model.site.BaseSiteModel;
import de.hybris.platform.commerceservices.enums.SiteChannel;
import de.hybris.platform.processengine.BusinessProcessService;
import de.hybris.platform.servicelayer.model.ModelService;

public class BraintreeLocalPaymentReversedEmailEventListener extends AbstractAcceleratorSiteEventListener<BraintreeLocalPaymentReversedEmailEvent> {

    private ModelService modelService;
    private BusinessProcessService businessProcessService;

    @Override
    protected SiteChannel getSiteChannelForEvent(BraintreeLocalPaymentReversedEmailEvent event) {
        final BaseSiteModel site = event.getSite();

        return site.getChannel();
    }

    @Override
    protected void onSiteEvent(final BraintreeLocalPaymentReversedEmailEvent event) {
        final BraintreeLocalPaymentReversedEmailProcessModel braintreeEmailProcess = businessProcessService
                .createProcess("BraintreeLocalPaymentReversed- " + event.getCustomer().getUid() + "-" + System.currentTimeMillis(),
                        "braintreeLocalPaymentReversedEmailProcess");
        braintreeEmailProcess.setSite(event.getSite());
        braintreeEmailProcess.setCustomer(event.getCustomer());
        braintreeEmailProcess.setLanguage(event.getLanguage());
        braintreeEmailProcess.setCurrency(event.getCurrency());
        braintreeEmailProcess.setStore(event.getBaseStore());

        braintreeEmailProcess.setContent(event.getContent());
        braintreeEmailProcess.setOrderId(event.getOrderModel().getCode());
        braintreeEmailProcess.setOrderTotalPrice(event.getOrderModel().getTotalPrice().toString());

        modelService.save(braintreeEmailProcess);
        businessProcessService.startProcess(braintreeEmailProcess);
    }

    public void setBusinessProcessService(final BusinessProcessService businessProcessService) {
        this.businessProcessService = businessProcessService;
    }

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

}
