package at.oefai.aaa.agent.jam;

import java.util.Random;
import java.util.Vector;

import at.oefai.aaa.agent.jam.types.Binding;

/**
 * Represents the runtime state of DoAny constructs
 * @author Marc Huber
 * @author Jaeho Lee
 */

class PlanRuntimeDoAnyState implements PlanRuntimeState {

    private PlanConstruct thisConstruct = null;
    private PlanRuntimeState substate = null;

    private int activeBranchNum;
    private Vector<Integer> branchesLeft; // Vector holding indices of branches that
    // have not yet completed (succeeded OR failed) 0-based

    /**  */
    PlanRuntimeDoAnyState(PlanDoAnyConstruct be) {
        this.thisConstruct = be;

        this.branchesLeft = new Vector<Integer>(be.getNumBranches());
        for (int i = 0; i < be.getNumBranches(); i++) {
            this.branchesLeft.addElement(new Integer(i));
        }

        setActiveBranchNum(selectRandomBranch());
        this.substate = be.getBranch(getActiveBranchNum()).newRuntimeState();
    }

    //
    // Member functions
    //
    int getActiveBranchNum() { return this.activeBranchNum; }


    /** Select a branch randomly from those still not completed or failed. */
    private int selectRandomBranch() {
        Random ran = new Random();
        return (this.branchesLeft.elementAt(Math.abs(ran.nextInt() % this.branchesLeft.size())).intValue());
    }

    /** Execute something in one of the branches */
    public State execute(Binding b, Goal thisGoal) {
        PlanConstruct currentConstruct;

        currentConstruct = ((PlanDoAnyConstruct)this.thisConstruct).getBranch(getActiveBranchNum());
        if (this.substate == null) {
            this.substate = currentConstruct.newRuntimeState();
        }

        State returnVal = this.substate.execute(b, thisGoal);

        //
        // FAILED!
        //
        // Something in the branch failed so this construct may fail
        if (returnVal == State.CONSTRUCT_FAILED) {

            // If we've gone through all branches then the construct has failed.
            if (this.branchesLeft.size() == 1) {
                return State.CONSTRUCT_FAILED;
            }

            // Find and remove the current branch from further consideration
            for (int i = 0; i < this.branchesLeft.size(); i++) {
                if (this.branchesLeft.elementAt(i).intValue() == getActiveBranchNum()) {
                    this.branchesLeft.removeElementAt(i);
                }
            }

            setActiveBranchNum(selectRandomBranch());
            this.substate = ((PlanDoAnyConstruct) this.thisConstruct).getBranch(getActiveBranchNum()).newRuntimeState();
            return State.CONSTRUCT_INCOMP;
        } else if (returnVal == State.CONSTRUCT_INCOMP) { // INCOMPLETE Nothing's been determined at this point
            return State.CONSTRUCT_INCOMP;
        } else { // COMPLETE!
            return State.CONSTRUCT_COMPLETE;
        }
    }

    /**  */
    void setActiveBranchNum(int n) {
        if (n >= 0 && n < ((PlanDoAnyConstruct)this.thisConstruct).getNumBranches()) {
            this.activeBranchNum = n;
        } else {
            this.activeBranchNum = -1;
        }
    }

}

