package uk.co.tekkies.readings.util;

import uk.co.tekkies.readings.R;
import uk.co.tekkies.readings.activity.BaseActivity;
import uk.co.tekkies.readings.model.Prefs;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.MapBuilder;

public class Analytics {

    private static final String CATEGORY_ACTION = "action";
    public  static final String CATEGORY_PREFS = "prefs";
    public static final String CATEGORY_MP3_CONTENT = "mp3_content";
    private static final String CATEGORY_CAUGHT_EXCEPTION = "caught_exception";

    public static final String ACTION_BUTTON_PRESS = "button_press";
    public static final String ACTION_GENERAL = "action_general";
    public static final String ACTION_FOUND = "mp3_found";
    private static final String ACTION_NONE = "none";
    
    public static final String LABEL_NIGHT_MODE = "night_mode";
    public static final String LABEL_TEXT_SIZE = "text_size";
    public static final String LABEL_MP3_SEARCH = "settings_mp3_search";
    public static final String LABEL_TOTAL = "total";

    public static void startActivity(BaseActivity activity) {
        if (isEnabled(activity)) {
            EasyTracker.getInstance(activity).activityStart(activity);
        }
    }

    public static void stopActivity(BaseActivity activity) {
        if (isEnabled(activity)) {
            EasyTracker.getInstance(activity).activityStop(activity);
        }
    }

    public static void EventDayNightToggle(Context context, Boolean nightMode) {
        SendEvent(context, CATEGORY_ACTION, ACTION_BUTTON_PRESS, LABEL_NIGHT_MODE, (long) (nightMode ? 1 : 0));
    }

    public static void EventTextSize(Context context, double textSize) {
        long textSizeLong = (long) (textSize * 100.0);
        SendEvent(context, CATEGORY_ACTION, ACTION_GENERAL, LABEL_TEXT_SIZE, textSizeLong);
    }

    private static boolean isEnabled(Context context) {
        return (new Prefs(context)).loadAnalyticsEnabled();
    }


    public static void SendEvent(Context context, String category, String action, String label, long value) {
        if (isEnabled(context)) {
            EasyTracker easyTracker = EasyTracker.getInstance(context);
            easyTracker.send(MapBuilder.createEvent(category, action, label, value).build());
        }
    }

    public static void UIClick(Context context, String label) {
        SendEvent(context, CATEGORY_ACTION, ACTION_GENERAL, label, 0);
    }

    public static void PrefsChange(Context context, String key) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        String value = "(unsupported)";
        value = getPrefValue(key, prefs, value);
        //Case for enable/disable analytics
        if(key.equals(context.getString(R.string.pref_key_enable_analytics))) {
            EasyTracker easyTracker = EasyTracker.getInstance(context);
            easyTracker.send(MapBuilder.createEvent(CATEGORY_PREFS, key, value, (long)0).build());
        } else {
            SendEvent(context, CATEGORY_PREFS, key, value, 0);
        }
    }

    private static String getPrefValue(String key, SharedPreferences prefs, String value) {
        try {
            value = prefs.getString(key, "default");
        } catch (Exception e) {
            try {
                value = prefs.getBoolean(key, false) ? "true" : "false";
            } catch (Exception e1) {
            }
        }
        return value;
    }

    public static void reportCaughtException(Context context, Exception e) {
        if (isEnabled(context)) {
            SendEvent(context, CATEGORY_CAUGHT_EXCEPTION, ACTION_NONE, e.toString(), 0);
        }
    }
}

@@ -275,24 +275,50 @@
 nceManager;%0A
+import android.util.Log;%0A%0A
 import com.g
@@ -3722,32 +3722,695 @@
 led(context)) %7B%0A
+            String message=e.getClass().getSimpleName();%0A            if(e.getMessage() != null) %7B%0A                message += %22:%22+e.getMessage();%0A            %7D%0A            StackTraceElement%5B%5D stackTraceElements = e.getStackTrace();%0A            String location=%22%22;%0A            for(int i=0;i%3CstackTraceElements.length; i++) %7B%0A                StackTraceElement stackTraceElement = stackTraceElements%5Bi%5D;%0A                if(stackTraceElement.getClassName().contains(%22uk.co.tekkies%22)) %7B%0A                    location += stackTraceElement.getFileName().replace(%22.java%22,%22%22)+%22:%22+stackTraceElement.getLineNumber();%0A                    break;%0A                %7D%0A            %7D%0A
             Send
@@ -4455,33 +4455,25 @@
 ON, 
-ACTION_NONE, e.toString()
+message, location
 , 0)

Improve caught exception reporting data: Exception type+message, File + line no.