package com.alexanderlogistics;

import java.util.logging.Logger;

import javax.inject.Inject;

import org.imixs.workflow.ItemCollection;
import org.imixs.workflow.SignalAdapter;
import org.imixs.workflow.engine.WorkflowService;
import org.imixs.workflow.exceptions.AccessDeniedException;
import org.imixs.workflow.exceptions.AdapterException;
import org.imixs.workflow.exceptions.ModelException;
import org.imixs.workflow.exceptions.PluginException;
import org.imixs.workflow.exceptions.ProcessingErrorException;

/**
 * Der InvoiceDispatchAdapter verteilt die vom Abteilungsleiter ausgewählten
 * Rechnungen
 * 
 * @version 1.0
 * @author rsoika
 */
public class InvoiceDispatchAdapter implements SignalAdapter {

    private static Logger logger = Logger.getLogger(InvoiceDispatchAdapter.class.getName());

    @Inject
    WorkflowService workflowService;

    /**
     * This method computes the cargosoft export data file
     * 
     * @throws PluginException
     */
    @Override
    public ItemCollection execute(ItemCollection document, ItemCollection event)
            throws AdapterException, PluginException {

        logger.info("......stapelverarbeitung pruefung...");

        String spaceRef = document.getItemValueString("space.ref");
        String sbTeam = document.getItemValueString("sb.team");
        String sbAssist = document.getItemValueString("sb.assist");
        logger.info("...... space.ref=" + spaceRef);
        logger.info("...... sb.team=" + sbTeam);
        logger.info("...... sb.assist=" + sbAssist);
        // read selection
        String sSelection = document.getItemValueString("invoice.selection");
        // cut first ,
        if (sSelection.startsWith(",")) {
            sSelection=sSelection.substring(1);
        }
        String[] ids = sSelection.split(",");

        if (ids.length==0 || ids[0].isEmpty()) {
            throw new PluginException(InvoiceDispatchAdapter.class.getSimpleName(), "PROCESSING_ERROR",
                    "Es wurden keine Rechnungen zur Verteilung ausgewählt.");
        }
        
        for (String id : ids) {
            // load invoice
            ItemCollection invoice = workflowService.getWorkItem(id);
            if (invoice != null) {
                logger.info("...stapelverarbeitung : " + id);

                invoice.setItemValue("space.ref", spaceRef);
                invoice.setItemValue("sb.team", sbTeam);
                invoice.setItemValue("sb.assist", sbAssist);

                // append workitemref
                document.appendItemValue("$workitemref", invoice.getUniqueID());
                // now process invoice..... event = 20

                try {
                    workflowService.processWorkItem(invoice, 20);
                } catch (AccessDeniedException | ProcessingErrorException | PluginException | ModelException e) {
                    throw new PluginException(InvoiceDispatchAdapter.class.getSimpleName(), "PROCESSING_ERROR",
                            "Failed to process invoice : " + invoice.getUniqueID());
                }

            }

        }

        return document;
    }

}