/*
 *  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
 *
 * Choice.java
 *
 * Created on November 1, 2002, 12:21 PM
 */

package org.evilsoft.gaming.RPGeneration;

import org.jdom.*;
import java.util.*;
/**
 *
 * @author  devon
 */
public class Choice {
    private VariableHashMap allVars;
    private String chance = "1";
    private ArrayList information = new ArrayList();
    private ArrayList operations = new ArrayList();
    private ArrayList select = new ArrayList();
    private TableCheck tableCheck;

    /** Creates a new instance of Choice */
    public Choice(Element choiceElement, VariableHashMap allVars) {
        this.allVars = allVars;
        try {
            if(choiceElement.getAttribute("Chance") != null) {
                chance = choiceElement.getAttribute("Chance").getValue();
            }

            List informationList = choiceElement.getChildren("Information");
            ListIterator informationIterator = informationList.listIterator();
            while(informationIterator.hasNext()) {
                Element informationElement = (Element)informationIterator.next();
                information.add(new Information(informationElement, allVars));
            }

            List varMods = choiceElement.getChildren("VarMod");
            ListIterator varModsIterator = varMods.listIterator();
            while(varModsIterator.hasNext()) {
                Element varModElement = (Element)varModsIterator.next();
                operations.add(new Operation(varModElement));
            }

            List selectList = choiceElement.getChildren("Select");
            ListIterator selectIterator = selectList.listIterator();
            while(selectIterator.hasNext()) {
                Element selectElement = (Element)selectIterator.next();
                select.add(new Select(selectElement, allVars));
            }
            Element tabChk = choiceElement.getChild("TableCheck");
            if(tabChk != null) {
                this.tableCheck = new TableCheck(tabChk, allVars);
            }
        }
        catch(Exception e) {
            System.out.println("XML ERROR");
            e.printStackTrace();
        }
    }

    public int getChances() {
        return new Integer(allVars.parse(chance)).intValue();
    }

    public ArrayList getInformation() {
        ArrayList retInformation = new ArrayList();
        retInformation.addAll(information);
        if(select.size() > 0) {
            for(int i = 0; i < select.size(); i++) {
                Select selection = (Select)select.get(i);
                try {
                    retInformation.addAll(selection.getSelection());
                }
                catch(variableException e) {
                    System.out.println(e.getMessage());
                }
            }
        }
        if(tableCheck != null) {
            retInformation.addAll(tableCheck.getInformation());
        }
        return retInformation;
    }

    public ArrayList getOperations() {
        return operations;
    }
}
