package gameFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import gameFactory.FactoryException;
import objects.GameObject;
import reflection.Reflection;
import stage.Game;
import jgame.JGObject;
import jgame.platform.JGEngine;
import reflection.*;
/**
 * @Author: Steve (Siyang) Wang
 */
public class GameFactory {
    private JGEngine myEngine;
    private String myOrder;
    private int  myLevelID, mySceneID;
    private Game myGame;
    public static final String RESOURCE_PACKAGE = "engineResources/";
    public static final String DEFAULT_FORMAT= "DataFormat";
    public static final String DEFAULT_PATH= "FactoryOrderPath";
    public static final String DEFAULT_NULL = "null";

    private static final String TEST_STRING = ""; 
    protected ResourceBundle myFormat, myPath;
    public GameFactory(JGEngine engine){
        myEngine = engine;
        myFormat = ResourceBundle.getBundle(RESOURCE_PACKAGE + DEFAULT_FORMAT);
        myPath = ResourceBundle.getBundle(RESOURCE_PACKAGE + DEFAULT_PATH);
    }
    /**
     * Only couple things as argument, use reflection to create or modify object instance.
     */
    public GameObject processOrder(Game game, int levelID, int sceneID, String order) throws FactoryException{
        testLegitimateOrder(order);
        String[] orderSplit = order.split("=");
        String instruction = orderSplit[0];
        List<String> parameterList = parseOrder(instruction, orderSplit[1]);
        Object myObject = Reflection.createInstance(myPath.getString(instruction), 
                                                    parameterList.get(0), 
                                                    parameterList.get(1),
                                                    parameterList.get(2),
                                                    parameterList.get(3),
                                                    parameterList.get(4));  
        return (GameObject) myObject;
    }
    
    /*
     * discuss this again
     */
    private List<String> parseOrder (String instruction, String orderValue) {
        //          checkModifyOrCreate(orderSplit[0]);
        String tokensList = myFormat.getString(instruction);
        List<String> answerList = new ArrayList<String>();
        List<String> inputParameterSplit = Arrays.asList(orderValue.split("\\,"));

        for(int i = 0; i < inputParameterSplit.size(); i ++){
            if(inputParameterSplit.get(i).equals("ParameterToken")){
                answerList.add(inputParameterSplit.get(i));
            }
        }
        return answerList;
    }

    private void testLegitimateOrder (String order) {
        if (!order.contains("=")) 
            throw new IllegalArgumentException("String " + order + " does not contain =");
    }
}

@@ -974,16 +974,20 @@
         
+    
 myEngine
@@ -993,24 +993,28 @@
 e = engine;%0A
+    
         myFo
@@ -1089,16 +1089,20 @@
         
+ 
+   
 myPath =
@@ -1175,16 +1175,17 @@
     /**%0A
+ 
      * O
@@ -1268,24 +1268,25 @@
 tance.%0A     
+ 
 */%0A    publi
@@ -1395,16 +1395,20 @@
         
+   
+ 
 testLegi
@@ -1427,16 +1427,20 @@
 order);%0A
+    
         
@@ -1475,24 +1475,28 @@
 split(%22=%22);%0A
+    
         Stri
@@ -1527,32 +1527,36 @@
 lit%5B0%5D;%0A        
+    
 List%3CString%3E par
@@ -1604,24 +1604,28 @@
 rSplit%5B1%5D);%0A
+    
         Obje
@@ -1740,32 +1740,36 @@
                 
+    
 parameterList.ge
@@ -1819,32 +1819,36 @@
                 
+    
 parameterList.ge
@@ -1897,32 +1897,36 @@
                 
+ 
+   
 parameterList.ge
@@ -1931,16 +1931,20 @@
 get(2),%0A
+    
         
@@ -2053,32 +2053,36 @@
                 
+ 
+   
 parameterList.ge
@@ -2090,16 +2090,20 @@
 (4));  %0A
+    
         
@@ -2136,26 +2136,31 @@
 ct;%0A
+ 
     %7D%0A
+%0A
     
-%0A
     /*%0A
+    
     
@@ -2182,27 +2182,35 @@
  again%0A     
+  
+  
 */%0A
+    
     private 
@@ -2283,16 +2283,20 @@
         
+   
+ 
 //      
@@ -2331,24 +2331,28 @@
 rSplit%5B0%5D);%0A
+    
         Stri
@@ -2400,32 +2400,36 @@
 ction);%0A        
+   
+ 
 List%3CString%3E ans
@@ -2463,16 +2463,20 @@
 ing%3E();%0A
+    
         
@@ -2559,16 +2559,20 @@
         
+   
+ 
 for(int 
@@ -2617,16 +2617,20 @@
  i ++)%7B%0A
+    
         
@@ -2706,16 +2706,20 @@
         
+   
+ 
 answerLi
@@ -2758,34 +2758,42 @@
 ));%0A            
-%7D%0A
+    %7D%0A    
         %7D%0A      
@@ -2790,24 +2790,28 @@
   %7D%0A        
+    
 return answe
@@ -2817,23 +2817,31 @@
 erList;%0A
+    
     %7D%0A%0A
+    
     priv
@@ -2882,24 +2882,28 @@
 ng order) %7B%0A
+    
         if (
@@ -2937,16 +2937,20 @@
         
+    
 throw ne
@@ -3016,20 +3016,24 @@
 ontain =%22);%0A
+    
     %7D%0A%7D%0A

change constructor to public