package com.braintree.warehouse;

import com.braintree.constants.BraintreefulfilmentprocessConstants;
import de.hybris.platform.commerceservices.enums.WarehouseConsignmentState;
import de.hybris.platform.ordersplitting.model.ConsignmentModel;
import de.hybris.platform.ordersplitting.model.ConsignmentProcessModel;
import de.hybris.platform.processengine.BusinessProcessService;
import de.hybris.platform.servicelayer.model.ModelService;
import de.hybris.platform.warehouse.Warehouse2ProcessAdapter;
import de.hybris.platform.warehouse.WarehouseConsignmentStatus;

import java.util.Map;


public class DefaultBrainTreeWarehouse2ProcessAdapter implements Warehouse2ProcessAdapter {

    private Map<WarehouseConsignmentStatus, WarehouseConsignmentState> statusMap;
    private ModelService modelService;
    private BusinessProcessService businessProcessService;

    @Override
    public void receiveConsignmentStatus(final ConsignmentModel consignment, final WarehouseConsignmentStatus status) {
        for (final ConsignmentProcessModel process : consignment.getConsignmentProcesses()) {
            final WarehouseConsignmentState state = statusMap.get(status);
            if (state == null) {
                throw new IllegalStateException("No mapping for WarehouseConsignmentStatus: " + status);
            }
            process.setWarehouseConsignmentState(state);
            modelService.save(process);
            businessProcessService.triggerEvent(
                    process.getCode() + "_" + BraintreefulfilmentprocessConstants.WAIT_FOR_WAREHOUSE);
        }
    }

    public void setStatusMap(final Map<WarehouseConsignmentStatus, WarehouseConsignmentState> statusMap) {
        this.statusMap = statusMap;
    }

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

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

}
