/*
 *  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
 *
 * PersonalityArrayList.java
 *
 * Created on October 22, 2002, 3:27 PM
 */

package org.evilsoft.gaming.RPGeneration;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import java.io.*;
import java.util.*;

/**
 *
 * @author  devon
 */
public class PersonalityArrayList extends ArrayList {
    private VariableHashMap allVars;

    /** Creates a new instance of PersonalityArrayList */
    public PersonalityArrayList(HashMap tables, String directoryName) {
        allVars = new VariableHashMap(tables);
        //Matrix File
        try {
            File defaultFile = new File(directoryName + "/matrix.xml");
            if(defaultFile.exists()) {
                SAXBuilder builder = new SAXBuilder();
                Document character = builder.build(defaultFile);
                if(character.getRootElement().getName().equals("Personality")) {
                    Element personality = character.getRootElement();

                    if(personality.getChild("Declare") != null) {
                        Element declare = personality.getChild("Declare");

                        List vars = declare.getChildren("Var");
                        ListIterator varIterator = vars.listIterator();
                        while(varIterator.hasNext()) {
                            Element varElement = (Element)varIterator.next();
                            allVars.newVar(varElement);
                        }

                        List varMods = declare.getChildren("VarMod");
                        ListIterator varModsIterator = varMods.listIterator();
                        while(varModsIterator.hasNext()) {
                            Element varModElement = (Element)varModsIterator.next();
                            allVars.addInitialOperations(new Operation(varModElement));
                        }
                    }

                    List traits = personality.getChildren("Trait");
                    ListIterator elementIterator = traits.listIterator();
                    while(elementIterator.hasNext()) {
                        Element traitElement = (Element)elementIterator.next();
                        this.add(new Trait(traitElement, allVars));
                    }
                }
            }
        }
        catch(variableException ve) {
            System.out.println("Variable Error");
            System.out.println("Cannot Initialize: " + ve.getMessage());
        }
        catch(Exception e) {
            System.out.println("XML ERROR");
            e.printStackTrace();
        }
        // Tables file
        try {
            File defaultFile = new File(directoryName + "/tables.xml");
            if(defaultFile.exists()) {
                SAXBuilder builder = new SAXBuilder();
                Document character = builder.build(defaultFile);
                if(character.getRootElement().getName().equals("Tables")) {
                    Element personality = character.getRootElement();
                    List table = personality.getChildren("Table");
                    ListIterator tableIterator = table.listIterator();
                    while(tableIterator.hasNext()) {
                        Element tableElement = (Element)tableIterator.next();
                        allVars.addTable(tableElement);
                    }
                }
            }
        }
        catch(Exception e) {
            System.out.println("XML ERROR");
            e.printStackTrace();
        }
    }

    public void initialize() {
        try {
            allVars.initialize();
        }
        catch(variableException e) {
            System.out.println("Cannot Initialize: " + e.getMessage());
        }
    }

    public void doOperation(ArrayList operations) {
        try {
            allVars.doOperation(operations);
        }
        catch(variableException e) {
            System.out.println(e.getMessage());
        }
    }
}
