/*
 *  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
 *
 * Roll.java
 *
 * Created on October 29, 2002, 6:14 PM
 */

package org.evilsoft.gaming.RPGeneration;

import org.evilsoft.gaming.Dice;
import java.util.*;
import org.jdom.*;
/**
 *
 * @author  devon
 */
public class Roll {
    private VariableHashMap allVars;
    private String sides = "0";
    private String num = "0";
    private String bonus = "0";
    private String multiplier = "0";
    private String name;
    private ArrayList operations = new ArrayList();

    /** Creates a new instance of Roll */
    public Roll(Element rollElement, String name, VariableHashMap allVars) {
        this.allVars = allVars;
        this.name = name;

        try {
            if(rollElement.getAttribute("Die") != null) {
                sides = rollElement.getAttribute("Die").getValue();
            }
            if(rollElement.getAttribute("Num") != null) {
                num = rollElement.getAttribute("Num").getValue();
            }
            if(rollElement.getAttribute("Bonus") != null) {
                bonus = rollElement.getAttribute("Bonus").getValue();
            }
            if(rollElement.getAttribute("Multiplier") != null) {
                multiplier = rollElement.getAttribute("Multiplier").getValue();
            }

            List varMods = rollElement.getChildren("VarMod");
            ListIterator varModsIterator = varMods.listIterator();
            while(varModsIterator.hasNext()) {
                Element varModElement = (Element)varModsIterator.next();
                operations.add(new Operation(varModElement));
            }
        }
        catch(Exception e) {
            System.out.println("XML ERROR");
            e.printStackTrace();
        }
    }

    public int getRoll(Random rand) {
        int rSides = new Integer(allVars.parse(sides)).intValue();
        int rNum = new Integer(allVars.parse(num)).intValue();
        int rBonus = new Integer(allVars.parse(bonus)).intValue();
        int rMultiplier = new Integer(allVars.parse(multiplier)).intValue();

        Dice die = new Dice(rNum, rSides, rBonus, rand);

        int roll = die.roll() * rMultiplier;
        if(operations.size() > 0) {
            for(int i = 0; i < operations.size(); i++) {
                Operation op = (Operation)operations.get(i);
                String type = op.getType();
                String key = op.getKey();
                String value = op.getValue();
                value = value.replaceAll("#", roll + "");
                try {
                    allVars.doOperation(new Operation(type, key, value));
                }
                catch(variableException e) {
                    System.out.println(e.getMessage());
                }
            }
        }
        return roll;
    }
}
