/*
 *  RPGeneration - A role playing utility generate interesting things
 *  Copyright (C) 2002 Devon D Jones
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  The author of this program grants you the ability to use this code
 *  in conjunction with code that is covered under the Open Gaming License
 *
 * Select.java
 *
 * Created on November 1, 2002, 12:21 PM
 */

package org.evilsoft.gaming.RPGeneration;

import org.jdom.*;
import java.util.*;
import org.evilsoft.gaming.Dice;
/**
 *
 * @author  devon
 */
public class Select extends ArrayList {
    private VariableHashMap allVars;
    private String numSelections = "1";

    /** Creates a new instance of Select */
    public Select(Element selectElement, VariableHashMap allVars) {
        this.allVars = allVars;
        try {
            if(selectElement.getAttribute("Selections") != null) {
                numSelections = selectElement.getAttribute("Selections").getValue();
            }
            List selectList = selectElement.getChildren("Choice");
            ListIterator selectIterator = selectList.listIterator();
            while(selectIterator.hasNext()) {
                Element choiceElement = (Element)selectIterator.next();
                add(new Choice(choiceElement, allVars));
            }
        }
        catch(Exception e) {
            System.out.println("XML ERROR");
            e.printStackTrace();
        }
    }

    public ArrayList getSelection() throws variableException {
        ArrayList retInformation = new ArrayList();
        if(this.size() > 0) {
            int selections = new Integer(allVars.parse(numSelections)).intValue();

            int size = 0;
            for(int i = 0; i < this.size(); i++) {
                Choice choice = (Choice)get(i);
                size = size + choice.getChances();
            }
            if(size <= 0) {
                size = 1;
            }
            Dice traitDie = new Dice(1, size, -1, allVars.getRandom());
            for(int j = 0; j < selections; j++) {
                int dieRoll = traitDie.roll();
                if(dieRoll < 0) {
                    dieRoll = 0;
                }
                else if(dieRoll > size) {
                    dieRoll = size;
                }
                Choice currChoice = null;

                int chance = 0;
                for(int i = 0; i < this.size(); i++) {
                    Choice chkChoice = (Choice)get(i);
                    int currChances = chkChoice.getChances();
                    if(currChances > 0) {
                        chance = chance +  currChances;
                        if(chance >= dieRoll) {
                            currChoice = chkChoice;
                            break;
                        }
                    }
                }
                if(currChoice != null) {
                    allVars.doOperation(currChoice.getOperations());
                    retInformation.addAll(currChoice.getInformation());
                }
            }
        }
        return retInformation;
    }

}
