package fi.cie.chiru.servicefusionar.serviceApi;

import util.IO;
import util.Vec;


import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import fi.cie.chiru.servicefusionar.gdx.GDXLoader;
import fi.cie.chiru.servicefusionar.gdx.GDXMesh;
import fi.cie.chiru.servicefusionar.commands.CommandFactory;

import android.util.Log;

public class SceneParser 
{
	private static final String LOG_TAG = "SceneParser";
	String sceneContent;
	private GDXLoader gdxLoader;

	public SceneParser()
	{
		gdxLoader = new GDXLoader();
	}
	public Vector<ServiceApplication> parseFile(ServiceManager serviceManager, String fileName)
	{
		Vector<ServiceApplication> serviceApplications = new Vector<ServiceApplication>();
		
		InputStream in = null;
		try 
		{
			in = serviceManager.getSetup().myTargetActivity.getAssets().open(fileName);
			sceneContent = IO.convertInputStreamToString(in);
		} 
		catch (IOException e) 
		{
			Log.e(LOG_TAG, "Could not read file: " +fileName);
			e.printStackTrace();
		} finally {
			try {
				Log.i(LOG_TAG, "Closing scene input stream");
				in.close();
			} catch (IOException e) {
				Log.e(LOG_TAG, e.toString());
			}
		}
		
		JSONObject jsonObj = null;
		try 
		{
			jsonObj = new JSONObject(sceneContent);
			
		} catch (JSONException e) {
			Log.e(LOG_TAG, "parsing failed");
			e.printStackTrace();
		}
		JSONArray entries = null;
        try 
        {
        	
        	entries = jsonObj.getJSONArray("ServiceApplication");
        	
        	for(int i=0; i<entries.length(); i++)
        	{
        		JSONObject ServiceAppObj = entries.getJSONObject(i);
        		String name = ServiceAppObj.getString("name");
        		
        		ServiceApplication serviceApp = new ServiceApplication(serviceManager,name);
        		
        		String meshName = ServiceAppObj.getString("meshRef");
        		String textureName = ServiceAppObj.getString("textRef");
        		
        		if(meshName!=null && textureName!=null)
        		{
        		    GDXMesh gdxMesh = gdxLoader.loadModelFromFile(meshName, textureName);
        		    gdxMesh.enableMeshPicking();
        		    serviceApp.setMesh(gdxMesh);
        		    
            		JSONObject posObj = ServiceAppObj.getJSONObject("position");
            		JSONObject rotObj = ServiceAppObj.getJSONObject("rotation");
            		JSONObject scaleObj = ServiceAppObj.getJSONObject("scale");
            		
        		    serviceApp.setPosition(new Vec((float)posObj.getDouble("x"), (float)posObj.getDouble("y"), (float)posObj.getDouble("z")));
        		    serviceApp.setRotation(new Vec((float)rotObj.getDouble("x"), (float)rotObj.getDouble("y"), (float)rotObj.getDouble("z")));
        		    serviceApp.setScale(new Vec((float)scaleObj.getDouble("x"), (float)scaleObj.getDouble("y"), (float)scaleObj.getDouble("z")));
        		}
        		
        		JSONObject commandObj = ServiceAppObj.getJSONObject("commands");
        		String clickCommand = commandObj.getString("CLICK");
        		String dropCommand = commandObj.getString("DROP");
        		String longClickCommand = commandObj.getString("LONG_CLICK");

        		serviceApp.setOnClickCommand(CommandFactory.createCommand(clickCommand, serviceManager, name));
        		serviceApp.setOnDoubleClickCommand(CommandFactory.createCommand(dropCommand, serviceManager, name));
        		serviceApp.setOnLongClickCommand(CommandFactory.createCommand(longClickCommand, serviceManager, name));
        		
        		boolean visible = ServiceAppObj.getBoolean("visible");
        		
        		if(!visible)
        			serviceApp.setvisible(false);
        		
        		boolean attached = ServiceAppObj.getBoolean("attached");
                serviceApp.attachToCamera(attached);
        		
        		serviceManager.getSetup().world.add(serviceApp);
        		serviceApplications.add(serviceApp);

        	}
			
		} catch (JSONException e) {
			Log.e(LOG_TAG, "parsing failed");
			e.printStackTrace();
		}
		
		return serviceApplications; 
	}
    
}

@@ -2917,32 +2917,637 @@
  %09%09%7D%0A        %09%09%0A
+        %09%09JSONObject geoLocation = null;%0A        %09%09try %7B%0A        %09%09%09geoLocation = ServiceAppObj.getJSONObject(%22geoLocation%22);%0A        %09%09%7D catch (JSONException e)%7B%7D%0A        %09%09if (geoLocation != null)%0A        %09%09%7B%0A        %09%09%09double latitude = 0.0d;%0A        %09%09%09double longitude = 0.0d;%0A        %09%09%09try %7B%0A        %09%09%09%09latitude = geoLocation.getDouble(%22Latitude%22);%0A        %09%09%09%09longitude = geoLocation.getDouble(%22Longitude%22);%0A        %09%09%09%7D catch (JSONException e) %7B%7D%0A        %09%09%09Log.i(LOG_TAG, %22Location: %22 + geoLocation.toString());%0A        %09%09%09serviceApp.setGeoLocation(latitude, longitude);%0A        %09%09%7D%0A        %09%09%0A
         %09%09JSONOb

Add geolocation parser code to SceneParser.