package com.elmakers.mine.bukkit.action.builtin;

import java.util.ArrayList;
import java.util.List;

import com.elmakers.mine.bukkit.action.ActionContext;
import com.elmakers.mine.bukkit.action.ActionHandler;
import com.elmakers.mine.bukkit.action.CompoundAction;
import com.elmakers.mine.bukkit.api.action.CastContext;
import com.elmakers.mine.bukkit.api.spell.SpellResult;

public class ParallelAction extends CompoundAction
{
    private static class SubAction {
        private ActionContext action;
        private CastContext context;

        public SubAction(ActionContext action, CastContext context) {
            this.action = action;
            this.context = new com.elmakers.mine.bukkit.action.CastContext(context);
        }

        public SpellResult perform() {
            return action.perform(context);
        }
    }

    private List<SubAction> remaining = null;

    @Override
    public void reset(CastContext context) {
        super.reset(context);
        ActionHandler actions = getHandler("actions");
        if (actions != null)
        {
            remaining = new ArrayList<>();
            for (ActionContext action : actions.getActions()) {
                action.getAction().reset(context);
                remaining.add(new SubAction(action, context));
            }
        }
    }

    @Override
    public void finish(CastContext context) {
        super.finish(context);
        ActionHandler actions = getHandler("actions");
        if (actions != null)
        {
            actions.finish(context);
        }
    }

    @Override
    public SpellResult perform(CastContext context) {
        SpellResult result = SpellResult.NO_ACTION;
        int startingWork = context.getWorkAllowed();
        List<SubAction> subActions = new ArrayList<>(remaining);
        remaining.clear();
        context.setWorkAllowed(0);
        int splitWork = Math.max(1, startingWork / subActions.size());
        for (SubAction action : subActions) {
            context.setWorkAllowed(context.getWorkAllowed() + splitWork);
            SpellResult actionResult = action.perform();
            context.addResult(actionResult);
            if (actionResult.isStop()) {
                remaining.add(action);
            }
            result = result.min(actionResult);
        }

        return result;
    }
}

@@ -1677,16 +1677,17 @@
 ACTION;%0A
+%0A
         
@@ -1691,51 +1691,71 @@
    i
-nt startingWork = context.getWorkAllowed();
+f (remaining.isEmpty()) %7B%0A            return result;%0A        %7D%0A
 %0A   
@@ -1839,24 +1839,25 @@
 ng.clear();%0A
+%0A
         cont
@@ -1879,16 +1879,69 @@
 wed(0);%0A
+        int startingWork = context.getWorkAllowed();%0A
         

Fix division by zero when parallel does not have children