/**
 * Appcelerator Titanium Mobile
 * Copyright (c) 2011-2012 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the Apache Public License
 * Please see the LICENSE included with this distribution for details.
 */
package org.appcelerator.kroll.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import org.appcelerator.kroll.KrollApplication;
import org.appcelerator.kroll.KrollRuntime;
import org.appcelerator.kroll.util.KrollStreamHelper;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Application;
import android.os.Environment;

/**
 * This class represents deployment data that is published to
 * /sdcard/[app id]/deploy.json as part of the build process
 */
public class TiDeployData
{
	private static final String TAG = "TiDeployData";

	protected static final String DEBUGGER_ENABLED = "debuggerEnabled";
	protected static final String DEBUGGER_PORT = "debuggerPort";
	protected static final String PROFILER_ENABLED = "profilerEnabled";
	protected static final String PROFILER_PORT = "profilerPort";
	protected static final String FASTDEV_PORT = "fastdevPort";
	protected static final String FASTDEV_LISTEN = "fastdevListen";

	private JSONObject deployData;

	/**
	 * Parses the deploy.json file if it exists
	 */
	public TiDeployData(Application app)
	{
		File extStorage = Environment.getExternalStorageDirectory();
		File deployJson = new File(new File(extStorage, app.getPackageName()), "deploy.json");

		if (deployJson.exists()) {
			readDeployData(deployJson);
		}
	}

	protected void readDeployData(File deployJson)
	{
		try {
			FileInputStream stream = new FileInputStream(deployJson);
			deployData = new JSONObject(KrollStreamHelper.toString(stream));
			Log.d(TAG, "Read deploy data: " + deployData.toString(), Log.DEBUG_MODE);

		} catch (FileNotFoundException e) {
			Log.e(TAG, e.getMessage(), e);

		} catch (JSONException e) {
			Log.e(TAG, e.getMessage(), e);
		}
	}

	/**
	 * @return Whether or not the debug server is enabled
	 */
	public boolean isDebuggerEnabled()
	{
		if (deployTypeDisabled()) {
			return false;
		}

		return deployData.optBoolean(DEBUGGER_ENABLED, false);
	}

	/**
	 * @return The debug server port, or -1
	 */
	public int getDebuggerPort()
	{
		if (deployTypeDisabled()) {
			return -1;
		}

		return deployData.optInt(DEBUGGER_PORT, -1);
	}

	/**
	 * @return Whether or not the profiler server is enabled
	 */
	public boolean isProfilerEnabled()
	{
		if (deployTypeDisabled()) {
			return false;
		}

		return deployData.optBoolean(PROFILER_ENABLED, false);
	}

	/**
	 * @return The profiler server port, or -1
	 */
	public int getProfilerPort()
	{
		if (deployTypeDisabled()) {
			return -1;
		}

		return deployData.optInt(PROFILER_PORT, -1);
	}

	/**
	 * @return The "fastdev" http server port, or -1
	 */
	public int getFastDevPort()
	{
		if (deployTypeDisabled()) {
			return -1;
		}

		return deployData.optInt(FASTDEV_PORT, -1);
	}

	/**
	 * @return Whether or not Fastdev mode should listen for a connection (default false)
	 *  This is useful for situations where adb forward or other external methods are necessary
	 */
	public boolean getFastDevListen()
	{
		if (deployTypeDisabled()) {
			return false;
		}

		return deployData.optBoolean(FASTDEV_LISTEN, false);
	}
	
	private boolean deployTypeDisabled() {
	    KrollApplication app = KrollRuntime.getInstance().getKrollApplication();
	    String deployType = null;
	    if (app != null) {
	        deployType = app.getDeployType();
	    }

		return (deployData == null || "production".equals(deployType));
	}
}

@@ -415,52 +415,8 @@
 on;%0A
-import org.appcelerator.kroll.KrollRuntime;%0A
 impo
@@ -544,23 +544,30 @@
 oid.
-app.Application
+content.ContextWrapper
 ;%0Aim
@@ -1198,16 +1198,52 @@
 sten%22;%0A%0A
+%09private KrollApplication krollApp;%0A
 %09private
@@ -1343,16 +1343,21 @@
 loyData(
+Kroll
 Applicat
@@ -1481,19 +1481,38 @@
 torage, 
-app
+((ContextWrapper) app)
 .getPack
@@ -1540,16 +1540,34 @@
 son%22);%0A%0A
+%09%09krollApp = app;%0A
 %09%09if (de
@@ -2139,33 +2139,35 @@
 bled()%0A%09%7B%0A%09%09if (
-d
+isD
 eployTypeDisable
@@ -2338,33 +2338,35 @@
 Port()%0A%09%7B%0A%09%09if (
-d
+isD
 eployTypeDisable
@@ -2547,33 +2547,35 @@
 bled()%0A%09%7B%0A%09%09if (
-d
+isD
 eployTypeDisable
@@ -2749,33 +2749,35 @@
 Port()%0A%09%7B%0A%09%09if (
-d
+isD
 eployTypeDisable
@@ -2943,33 +2943,35 @@
 Port()%0A%09%7B%0A%09%09if (
-d
+isD
 eployTypeDisable
@@ -3276,25 +3276,27 @@
 ()%0A%09%7B%0A%09%09if (
-d
+isD
 eployTypeDis
@@ -3386,17 +3386,16 @@
 se);%0A%09%7D%0A
-%09
 %0A%09privat
@@ -3404,17 +3404,19 @@
 boolean 
-d
+isD
 eployTyp
@@ -3430,94 +3430,14 @@
 ed()
- %7B%0A%09    KrollApplication app = KrollRuntime.getInstance().getKrollApplication();%0A%09    
+%0A%09%7B%0A%09%09
 Stri
@@ -3463,17 +3463,19 @@
 l;%0A%09
-    if (a
+%09if (krollA
 pp !
@@ -3489,16 +3489,10 @@
  %7B%0A%09
-        
+%09%09
 depl
@@ -3500,17 +3500,22 @@
 yType = 
-a
+krollA
 pp.getDe
@@ -3531,12 +3531,9 @@
 );%0A%09
-    
+%09
 %7D%0A%0A%09

Fix error where we check for krollApplication before runtime is initialized.