/**
 *
 */
package de.rico.editor.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import de.rico.adventure.entity.character.NonPlayerCharacter;
import de.rico.adventure.entity.hotspot.AbstractInteractiveHotspot;
import de.rico.adventure.entity.item.Item;
import de.rico.adventure.script.trigger.AbstractTrigger;
import de.rico.adventure.structure.Room;

/**
 * This class can be use to generate and export the method bodies
 * for a room's script file
 * @author Frank Bruns
 *
 */
public class ScriptFileExporter
{
	private Room room = null;
	private String text = ""; // the script file's text

	public ScriptFileExporter(Room room)
	{
		this.room = room;
	}

	/**
	 * Creates and exports the script file by generating the method
	 * bodies for the rooms entities and triggers.
	 * @param filePath filepath to export to
	 */
	public void exportToFile(String filePath)
	{
		// add some useful import statments
		this.text += "import de.rico.adventure.enums.State;\n";
		this.text += "import de.rico.adventure.actions.*;\n";
		this.text += "import de.rico.adventure.actions.custom.*;\n";
		this.text += "import de.rico.adventure.actions.custom.concurrent.*;\n\n";
		this.text += "import de.rico.engine.path.*;\n";
		this.text += "import de.rico.engine.effect.sound.*;\n";
		this.text += "import de.rico.engine.camera.recorder.*;\n\n\n";

		// add room enter and exit methods
		this.text += this.room.getScriptId()+"OnEnter()\n{\n\n}\n\n";
		this.text += this.room.getScriptId()+"OnExit()\n{\n\n}\n\n";


		// add hotspot methods
		for(AbstractInteractiveHotspot hot : this.room.getHotspots())
		{
			this.text += hot.getScriptId()+"OnLookAt()\n{\n\n}\n\n";
			this.text += hot.getScriptId()+"OnUse()\n{\n\n}\n\n";
		}

		// add item methods
		for(Item i : this.room.getItems())
		{
			this.text += i.getScriptId()+"OnLookAt()\n{\n\n}\n\n";
			this.text += i.getScriptId()+"OnPickUp()\n{\n\n}\n\n";
		}

		// add character methods
		for(NonPlayerCharacter c : this.room.getCharacters())
		{
			this.text += c.getScriptId()+"OnLookAt()\n{\n\n}\n\n";
			this.text += c.getScriptId()+"OnTalkTo()\n{\n\n}\n\n";
			this.text += c.getScriptId()+"OnUse()\n{\n\n}\n\n";
		}

		// add trigger methods
		for(AbstractTrigger trigger : this.room.getTriggers())
		{
			this.text += trigger.getScriptId()+"OnEnter()\n{\n\n}\n\n";
			this.text += trigger.getScriptId()+"OnExit()\n{\n\n}\n\n";
		}


		// create the file and save it with the script text
		File script = new File(filePath);
		try
		{
			FileOutputStream stream = new FileOutputStream(script);
			OutputStreamWriter out = new OutputStreamWriter(stream);
			out.write(this.text);
			out.flush();
		}
		catch (FileNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
