/*
Tiamat - a role-playing game framework
GameScript.java - runs the script for the game, processes events
    
 Copyright  (C) 2005 Hugues Johnson
 
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

package com.huguesjohnson.tiamat;

import com.huguesjohnson.serialization.Formatter;
import com.huguesjohnson.serialization.Storable;

import java.util.ArrayList;
import java.util.Hashtable;

/** GameScript - runs the script for the game, processes events
 * @author Hugues Johnson
 */
public class GameScript implements Storable{
    final static boolean debug=false;
    private final static boolean returnUnsupported=false;

/* begin read-only public members */
    private Hashtable<ScriptTableKey,ScriptTableValue> scriptTable;
    public Hashtable<ScriptTableKey,ScriptTableValue> getScriptTable(){ return(this.scriptTable); }
/* end read-only public members */
    
/* begin constructors */    
    public GameScript(){
        this.scriptTable=new Hashtable<ScriptTableKey,ScriptTableValue>();
    }
/* end constructors */
    
/* begin public methods */
    /** Process an Event triggered by the player.
     * @param event The Event triggered by the player.
     * @return A list of Events that were triggered by this initial Event.
     */
    public Event[] processEvent(Event event){
        ArrayList eventList=new ArrayList();
        return(this.processEvent(event,null));
    }
    /** Process an Event triggered by the player.
     * @param event The Event triggered by the player.
     * @param conditions A list of things that have happened in the game.
     * @return A list of Event that were triggered by this initial Event.
     */
    public Event[] processEvent(Event event,String[] conditions){
        ArrayList<Event> events=new ArrayList<Event>();
        //look up this event against the script table
        ScriptTableKey key=new ScriptTableKey(event,conditions);
        Object returnValue=this.scriptTable.get(key);
        //was anything returned for this event/conditions combo?
        if(returnValue==null){
            //try looking for a version with no conditions (the default)
            key.conditions=new ArrayList<String>();
            returnValue=this.scriptTable.get(key);
        }
        if(returnValue!=null){
            if(returnValue instanceof ScriptTableValue){
                ScriptTableValue scriptTableValue=(ScriptTableValue)returnValue;
                events=scriptTableValue.events;
            }
        }
        if(returnUnsupported){
            if(returnValue==null){
                Event e=new Event("draw-text");
                StringBuffer text=new StringBuffer("Unsupported event");
                text.append("\naction=");
                text.append(e.getAction());
                String params[][]=event.getParameters().getAll();
                if(params==null){
                    text.append("\nnull parameters");
                } else{
                    //[0][n]=key for item n, [1][n]=value for item n
                    int length=params[0].length;
                    for(int index=0;index<length;index++){
                        text.append("\n");
                        text.append(params[0][index]);
                        text.append("=");
                        text.append(params[1][index]);
                    }
                }
                int length=conditions.length;
                for(int index=0;index<length;index++){
                    text.append("\nconditions[");
                    text.append(index);
                    text.append("]=");
                    text.append(conditions[index]);
                }
                e.getParameters().set("text",text.toString());
                events.add(e);
            }
        }
        return(events.toArray(new Event[0]));
    }
    
    /** Add an event to the table. 
     * @param event The event triggering an action.
     * @param conditions Conditions from the game state.
     * @param returnEvents An array of events returned from this initial event.
     */
    public void add(Event event,String[] conditions,Event[] returnEvents){
        ScriptTableKey key=new ScriptTableKey(event,conditions);
        ScriptTableValue value=new ScriptTableValue();
        if(returnEvents!=null){
            int length=returnEvents.length;
            for(int index=0;index<length;index++){
                value.events.add(returnEvents[index]);
            }
        }
        this.scriptTable.put(key,value);
        if(debug){
            ScriptTableKey newKey=new ScriptTableKey(event,conditions);
            Object obj=this.scriptTable.get(newKey);
            if(obj==null){
                System.out.println("GameScript.add(..)\tFAILED TO FIND KEY");
            } else{
                System.out.println("GameScript.add(..)\tKEY FOUND");
            }
        }
    }
    
    /** The onDeserialize event should be fired after an object is de-serialized.
     * This allows objects to load transient members, set their working directory, and load Storable members if needed.
     * @param formatter the formatter to use to load any storable members
     * @param absoluteDir the working directory since all paths stored in the object should be relative
     */
    public void onDeserialize(Formatter formatter,String absoluteDir){ }
    
/* end public methods */
}