package com.braintree.actions.order;

import de.hybris.platform.core.enums.OrderStatus;
import de.hybris.platform.core.model.order.OrderModel;
import de.hybris.platform.orderhistory.model.OrderHistoryEntryModel;
import de.hybris.platform.orderprocessing.model.OrderProcessModel;
import de.hybris.platform.servicelayer.util.ServicesUtil;
import de.hybris.platform.task.RetryLaterException;

import java.util.HashSet;
import java.util.Set;


public class BrainTreeOrderManualCheckedAction extends AbstractOrderAction<OrderProcessModel> {

    private static final String PROCESS_CANNOT_BE_NULL_MESSAGE = "Process cannot be null";
    private static final String ORDER_IN_PROCESS_CANNOT_BE_NULL_MESSAGE = "Order in process cannot be null";

    public enum Transition {
        OK, NOK, UNDEFINED;

        public static Set<String> getStringValues() {
            final Set<String> res = new HashSet<String>();
            for (final Transition transitions : Transition.values()) {
                res.add(transitions.toString());
            }
            return res;
        }
    }

    @Override
    public Set<String> getTransitions() {
        return Transition.getStringValues();
    }

    @Override
    public final String execute(final OrderProcessModel process) throws RetryLaterException, Exception {
        return executeAction(process).toString();
    }

    protected Transition executeAction(final OrderProcessModel process) {
        ServicesUtil.validateParameterNotNull(process, PROCESS_CANNOT_BE_NULL_MESSAGE);

        final OrderModel order = process.getOrder();
        ServicesUtil.validateParameterNotNull(order, ORDER_IN_PROCESS_CANNOT_BE_NULL_MESSAGE);
        if (order.getFraudulent() != null) {
            final OrderHistoryEntryModel historyLog = createHistoryLog(
                    "Order Manually checked by CSA - Fraud = " + order.getFraudulent(), order);
            modelService.save(historyLog);
            if (order.getFraudulent().booleanValue()) {
                order.setStatus(OrderStatus.SUSPENDED);
                getModelService().save(order);
                return Transition.NOK;
            }
            return Transition.OK;
        }
        return Transition.UNDEFINED;
    }

}
