package controller;

import java.io.IOException;
import java.util.List;
import java.util.ResourceBundle;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import engine.GameEngine;
import gameFactory.GameFactory;
import parser.GameSaverAndLoader;
import stage.Game;
import reflection.Reflection;
/*
 * @Author: Justin (Zihao) Zhang
 */
public class DataController {
	public static final String DEFAULT_RESOURCE_PACKAGE = "engineResources/";
	public static final String DEFAULT_CREATEORMODIFY = "CreationOrModify";
	
	protected Game myGame;
    protected int currentLevelID;
    protected int currentSceneID;
	protected GameFactory myFactory;
	protected GameSaverAndLoader myGameSaverAndLoader;
	protected GameEngine myGameEngine;
	protected ResourceBundle myOrderReflector;
	
	public DataController(){
		myGameSaverAndLoader = new GameSaverAndLoader(); 
		myGame = new Game();
		myGameEngine = new GameEngine(myGame);
		myFactory = new GameFactory(myGameEngine);
		myOrderReflector = ResourceBundle.getBundle(DEFAULT_RESOURCE_PACKAGE + DEFAULT_CREATEORMODIFY);
	}
	
	public void initGameEngine(Game game){
		myGame = game;
		myGameEngine = new GameEngine(myGame);
		myFactory = new GameFactory(myGameEngine);
	}
	
	/*
	 * Called by Game Authorizing Environment to send the command String
	 * Input is a String order
	 */
	public void receiveOrder(String order){
		String[] orders = order.split(",");
		String methodName = myOrderReflector.getString(orders[0]);
		Reflection.callMethod(this, methodName, order);	
	}
	
	/*
	 * Called by Game Authorizing Environment to export the game data
	 * Input is a url to the XML file created by the GAE
	 */
	public void exportXML(String url) throws ParserConfigurationException{
//		myGameSaverAndLoader.save(myGame, url);
	}
	
	/*
	 * Called by PlayView to import the game data
	 * Input is a url to the XML file loaded by PlayView
	 */
	public void readXML(String url) throws ParserConfigurationException, SAXException, IOException{
//		List<String> orders = myParser.readFromFile(url);
//		for(String order: orders){
//			receiveOrder(order);
//		}
	}
	
//	/*
//	 * Called by PlayView to import the game data
//	 * Input is a url to the XML file loaded by PlayView
//	 */
//	public void readXML(String url) throws ParserConfigurationException, SAXException, IOException{
//		initGameEngine(myParser.readFromFile(url));
//	}
	
	/*
	 * Called by Game Authorizing Environment to read the info about a specific Game Object (i.e. Actor)
	 * Input is an id number matched to the Game Object
	 */
	public List<String> getObjectInfo(int id){
		return myGame.getGameObject(currentLevelID, currentSceneID, id).getAttributes();
	}
	
	public void callFactoryToProcess(String order) {
		try{
			myFactory.processOrder(myGame, currentLevelID, currentSceneID, order);	
		} catch (Exception e){
			e.printStackTrace(); // should never reach here
		}
	}
	
	public void switchToScene(String order){
		String[] orders = order.split(",");
		currentSceneID = Integer.parseInt(orders[2]);
		myGameEngine.setCurrentScene(currentLevelID, currentSceneID);
	}
	
	public void switchToLevel(String order){
		String[] orders = order.split(",");
		currentLevelID = Integer.parseInt(orders[2]);
	}
}

@@ -1240,32 +1240,34 @@
 meEngine);%0A%09%7D%0A%09%0A
+%09%0A
 %09/*%0A%09 * Called b
@@ -1549,24 +1549,26 @@
 der);%09%0A%09%7D%0A%09%0A
+%09%0A
 %09/*%0A%09 * Call
@@ -1755,20 +1755,31 @@
 xception
+, IOException
 %7B%0A
-//
 %09%09myGame
@@ -1813,24 +1813,26 @@
  url);%0A%09%7D%0A%09%0A
+%09%0A
 %09/*%0A%09 * Call
@@ -1973,452 +1973,70 @@
 ows 
-ParserConfigurationException, SAXException, IOException%7B%0A//%09%09List%3CString%3E orders = myParser.readFromFile(url);%0A//%09%09for(String order: orders)%7B%0A//%09%09%09receiveOrder(order);%0A//%09%09%7D%0A%09%7D%0A%09%0A//%09/*%0A//%09 * Called by PlayView to import the game data%0A//%09 * Input is a url to the XML file loaded by PlayView%0A//%09 */%0A//%09public void readXML(String url) throws ParserConfigurationException, SAXException, IOException%7B%0A//%09%09initGameEngine(myParser.readFromFile
+Exception %7B%0A%09%09initGameEngine(myGameSaverAndLoader.load
 (url));%0A
 //%09%7D
@@ -2035,12 +2035,12 @@
 ));%0A
-//
 %09%7D
+%0A%09
 %0A%09%0A%09
@@ -2326,32 +2326,142 @@
 ributes();%0A%09%7D%0A%09%0A
+%09%0A%09/*%0A%09 * Do not call this method directly%0A%09 * This method is called within DataController by Reflection%0A%09 */%0A
 %09public void cal
@@ -2657,24 +2657,132 @@
 re%0A%09%09%7D%0A%09%7D%0A%09%0A
+%09/*%0A%09 * Do not call this method directly%0A%09 * This method is called within DataController by Reflection%0A%09 */%0A
 %09public void
@@ -2958,32 +2958,140 @@
 tSceneID);%0A%09%7D%0A%09%0A
+%09/*%0A%09 * Do not call this method directly%0A%09 * This method is called within DataController by Reflection%0A%09 */%0A
 %09public void swi

modify DataController