/**
 * Appcelerator Titanium Mobile
 * Copyright (c) 2009-2014 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 ti.modules.titanium.analytics;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollModule;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiContext;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.util.TiPlatformHelper;
import org.json.JSONException;
import org.json.JSONObject;

import com.appcelerator.analytics.APSAnalytics;
import com.appcelerator.analytics.APSAnalyticsEvent;

@Kroll.module
public class AnalyticsModule extends KrollModule
{
	protected static final String PROPERTY_APP_NAV = "app.nav";
	protected static final String PROPERTY_APP_TIMED = "app.timed";
	protected static final String PROPERTY_APP_FEATURE = "app.feature";
	protected static final String PROPERTY_APP_SETTINGS = "app.settings";
	protected static final String PROPERTY_APP_USER = "app.user";

	public AnalyticsModule()
	{
		super();
	}

	public AnalyticsModule(TiContext tiContext)
	{
		this();
	}

	@Kroll.method
	public void navEvent(String from, String to,
		@Kroll.argument(optional=true) String event,
		@Kroll.argument(optional=true) KrollDict data)
	{
		if (TiApplication.getInstance().isAnalyticsEnabled()) {
			APSAnalytics.sendAppNavEvent(from, to, event, TiConvert.toJSON(data));
		}
	}

	@Kroll.method
	public void featureEvent(String event, @Kroll.argument(optional = true) KrollDict data)
	{
		if (TiApplication.getInstance().isAnalyticsEnabled()) {
			APSAnalytics.sendFeatureEvent(event, TiConvert.toJSON(data));
		}
	}

	@Kroll.getProperty @Kroll.method
	public String getLastEvent()
	{
		if (TiApplication.getInstance().isAnalyticsEnabled()) {
			try {
				APSAnalyticsEvent event = APSAnalytics.getLastEvent();
				if (event == null) {
					return null;
				}
				JSONObject json = new JSONObject();
				json.put("ver", "2");
				json.put("id", TiPlatformHelper.getInstance().getLastEventID());
				json.put("event", event.getEventType());
				json.put("ts", event.getEventTimestamp());
				json.put("mid", event.getEventMid());
				json.put("sid", event.getEventSid());
				json.put("aguid", event.getEventAppGuid());
				if (event.mustExpandPayload()) {
					json.put("data", new JSONObject(event.getEventPayload()));
				} else {
					json.put("data", event.getEventPayload());
				}
				return json.toString();
			} catch (JSONException e) {
			}
		}
		return null;
	}

	@Override
	public String getApiName()
	{
		return "Ti.Analytics";
	}
}

@@ -271,16 +271,43 @@
 ytics;%0A%0A
+import java.util.HashMap;%0A%0A
 import o
@@ -688,16 +688,42 @@
 bject;%0A%0A
+import android.util.Log;%0A%0A
 import c
@@ -881,16 +881,72 @@
 odule%0A%7B%0A
+%09private static final String TAG = %22AnalyticsModule%22;%0A%09%0A
 %09protect
@@ -1583,32 +1583,67 @@
 icsEnabled()) %7B%0A
+%09%09%09if (data instanceof HashMap) %7B%0A%09
 %09%09%09APSAnalytics.
@@ -1691,32 +1691,233 @@
 t.toJSON(data));
+%0A%0A%09%09%09%7D else %7B%0A%09%09%09%09try %7B%0A%09%09%09%09%09APSAnalytics.sendAppNavEvent(from, to, event, new JSONObject(data.toString()));%0A%09%09%09%09%7D catch (JSONException e) %7B%0A%09%09%09%09%09Log.e(TAG, %22Cannot convert data into JSON%22);%0A%09%09%09%09%7D%0A%09%09%09%7D
 %0A%09%09%7D%0A%09%7D%0A%0A%09@Kroll
@@ -2074,16 +2074,51 @@
 ed()) %7B%0A
+%09%09%09if (data instanceof HashMap) %7B%0A%09
 %09%09%09APSAn
@@ -2174,16 +2174,207 @@
 data));%0A
+%09%09%09%7D else %7B%0A%09%09%09%09try %7B%0A%09%09%09%09%09APSAnalytics.sendFeatureEvent(event, new JSONObject(data.toString()));%0A%09%09%09%09%7D catch (JSONException e) %7B%0A%09%09%09%09%09Log.e(TAG, %22Cannot convert data into JSON%22);%0A%09%09%09%09%7D%0A%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%0A

Support passing in strings for feature and nav events