package com.braintree.actions.order;


import de.hybris.platform.core.enums.OrderStatus;
import de.hybris.platform.core.model.order.OrderModel;
import de.hybris.platform.fraud.events.OrderFraudEmployeeNotificationEvent;
import de.hybris.platform.orderprocessing.model.OrderProcessModel;
import de.hybris.platform.processengine.action.AbstractProceduralAction;
import de.hybris.platform.servicelayer.event.EventService;
import de.hybris.platform.servicelayer.util.ServicesUtil;
import org.apache.log4j.Logger;


/**
 * Action in order business process, which sets the order status to {@link OrderStatus#WAIT_FRAUD_MANUAL_CHECK} and
 * sends event {@link OrderFraudEmployeeNotificationEvent}. <br> To notify back-end user about potential order frauds, a
 * proper business logic needs to be implemented in appropriate event listener.
 */
public class BrainTreePrepareOrderForManualCheckAction extends AbstractProceduralAction<OrderProcessModel> {

    private static final Logger LOG = Logger.getLogger(BrainTreePrepareOrderForManualCheckAction.class);
    private static final String PROCESS_CANNOT_BE_NULL_MESSAGE = "Process cannot be null";
    private static final String PROCESS_ORDER_CANNOT_BE_NULL_MESSAGE = "Process order cannot be null";

    private EventService eventService;

    @Override
    public void executeAction(final OrderProcessModel process) throws Exception {
        ServicesUtil.validateParameterNotNull(process, PROCESS_CANNOT_BE_NULL_MESSAGE);
        final OrderModel order = process.getOrder();
        ServicesUtil.validateParameterNotNull(order, PROCESS_ORDER_CANNOT_BE_NULL_MESSAGE);
        order.setStatus(OrderStatus.WAIT_FRAUD_MANUAL_CHECK);
        modelService.save(order);

        eventService.publishEvent(new OrderFraudEmployeeNotificationEvent(order));
        LOG.info("Process: " + process.getCode() + " in step " + getClass());

    }

    public void setEventService(final EventService eventService) {
        this.eventService = eventService;
    }

}
