/**
 * 
 */
package ti.modules.titanium.android;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.titanium.TiContext;
import org.appcelerator.titanium.util.Log;
import org.appcelerator.titanium.util.TiConfig;
import org.appcelerator.titanium.util.TiConvert;

import android.content.Intent;
import android.net.Uri;

@Kroll.proxy(creatableInModule=AndroidModule.class)
public class IntentProxy extends KrollProxy 
{
	private static final String LCAT = "TiIntent";
	private static boolean DBG = TiConfig.LOGD;
	
	protected Intent intent;
	
	public IntentProxy(TiContext tiContext) 
	{
		super(tiContext);
	}
	
	public void handleCreationDict(KrollDict dict) {
		intent = new Intent();
		
		// See which set of options we have to work with.
		String action = dict.getString("action");
		String data = dict.getString("data");
		String classname = dict.getString("className");

		if (action != null) {
			if (DBG) {
				Log.d(LCAT, "Setting action: " + action);
			}
			intent.setAction(action);
		}
		
		if (data != null) {
			if (DBG) {
				Log.d(LCAT, "Setting data uri: " + data);
			}
			intent.setData(Uri.parse(data));
		}
		
		if (classname != null) {
			try {
				Class<?> c = getClass().getClassLoader().loadClass(classname);
				intent.setClass(getTiContext().getActivity().getApplicationContext(), c);
			} catch (ClassNotFoundException e) {
				Log.e(LCAT, "Unable to locate class for name: " + classname);
				throw new IllegalStateException("Missing class for name: " + classname, e);
			}
		}
	}	
	
	@Kroll.method
	public void putExtra(String key, Object value) 
	{
		if (value instanceof String) {
			intent.putExtra(key, (String) value);
		} else if (value instanceof Boolean) {
			intent.putExtra(key, (Boolean) value);
		} else if (value instanceof Double) {
			intent.putExtra(key, (Double) value);
		} else if (value instanceof Integer) {
			intent.putExtra(key, (Integer) value);
		} else {
			Log.w(LCAT, "Warning unimplemented put conversion for " + value.getClass().getCanonicalName() + " trying String");
			intent.putExtra(key, TiConvert.toString(value));
		}
	}
	
	protected Intent getIntent() { 
		return intent;
	}
}

@@ -971,16 +971,56 @@
 sName%22);
+%0A%09%09String type = dict.getString(%22type%22);
 %0A%0A%09%09if (
@@ -1644,16 +1644,260 @@
 %09%09%7D%0A%09%09%7D%0A
+%09%09%0A%09%09if (type != null) %7B%0A%09%09%09if (DBG) %7B%0A%09%09%09%09Log.d(LCAT, %22Setting type: %22 + type);%0A%09%09%09%7D %0A%09%09%7D else %7B%0A%09%09%09if (DBG) %7B%0A%09%09%09%09Log.d(LCAT, %22Intent type not set, defaulting to text/plain%22);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09intent.setType( (type == null) ? %22text/plain%22 : type);%0A
 %09%7D%09%0A%09%0A%09@

Support setting intent type, and default to text/plain