package com.emmaguy.todayilearned;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import com.emmaguy.todayilearned.storage.UniqueIdentifierStorage;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;

import javax.inject.Inject;
import javax.inject.Named;

import retrofit.RetrofitError;
import timber.log.Timber;

public class App extends Application {
    private final boolean mIsDebug = BuildConfig.DEBUG;

    private AppComponent mAppComponent;

    private static GoogleAnalytics sGoogleAnalytics;
    private static Tracker sTracker;

    @Inject @Named("analytics") UniqueIdentifierStorage mStorage;

    public static App with(Context context) {
        return (App) context.getApplicationContext();
    }

    @Override
    public void onCreate() {
        super.onCreate();

        mAppComponent = DaggerAppComponent.builder().appModule(new AppModule(this)).build();
        mAppComponent.inject(this);

        sGoogleAnalytics = GoogleAnalytics.getInstance(this);
        sGoogleAnalytics.setLocalDispatchPeriod(1800);

        sTracker = sGoogleAnalytics.newTracker(getString(R.string.google_analytics_id));
        sTracker.set("&uid", mStorage.getUniqueIdentifier());
        sTracker.enableExceptionReporting(true);
        sTracker.enableAdvertisingIdCollection(false);
        sTracker.enableAutoActivityTracking(true);

        if (mIsDebug) {
            Timber.plant(new Timber.DebugTree());
        } else {
            Timber.plant(new GoogleAnalyticsTree(sTracker));
        }
    }

    public AppComponent getAppComponent() {
        return mAppComponent;
    }

    public void sendEvent(String action, String label) {
        if (mIsDebug) {
            Timber.d("Sending event: " + action + " " + label);
        } else {
            sTracker.send(new HitBuilders.EventBuilder()
                    .setCategory("RedditWear" + BuildConfig.VERSION_NAME)
                    .setAction(action)
                    .setLabel(label)
                    .build());
        }
    }

    public boolean isDebug() {
        return mIsDebug;
    }

    private static class GoogleAnalyticsTree extends Timber.Tree {
        private final Tracker mTracker;

        private GoogleAnalyticsTree(Tracker tracker) {
            mTracker = tracker;
        }

        @Override protected void log(int priority, String tag, String message, Throwable t) {
            if (priority == Log.ERROR) {
                final String description;
                final String throwableMessage = message + ", msg: " + t.getMessage();
                if (t instanceof RetrofitError) {
                    final String info;
                    if (((RetrofitError) t).getKind() == RetrofitError.Kind.NETWORK) {
                        info = throwableMessage;
                    } else {
                        info = throwableMessage + ", stack trace: " + Log.getStackTraceString(t);
                    }
                    description = "error kind: retrofit, " + ((RetrofitError) t).getKind() + " info: " + info;
                } else {
                    description = "error kind: non-retrofit, info: " + throwableMessage + ", stack trace: " + Log.getStackTraceString(t);
                }

                mTracker.send(new HitBuilders.ExceptionBuilder().setDescription(description).build());
            }
        }
    }
}

@@ -2561,50 +2561,8 @@
 ) %7B%0A
-                final String description;%0A
     
@@ -2639,24 +2639,65 @@
 tMessage();%0A
+                final String stackTrace;%0A
             
@@ -2764,74 +2764,93 @@
 nal 
-String info;%0A                    if (((RetrofitError) t).getKind()
+RetrofitError.Kind kind = ((RetrofitError) t).getKind();%0A                    if (kind
  == 
@@ -2903,39 +2903,38 @@
         
-info = throwableMessage
+stackTrace = %22Network%22
 ;%0A      
@@ -2983,54 +2983,21 @@
     
- info = throwableMessage + %22,
  stack
- t
+T
 race
-: %22 +
+ =
  Log
@@ -3063,237 +3063,49 @@
     
-    description = %22error kind: retrofit, %22 + ((RetrofitError) t).getKind() + %22 info: %22 + info;%0A                %7D else %7B%0A                    description = %22error kind: non-retrofit, info: %22 + throwableMessage + %22,
+%7D else %7B%0A                   
  stack
- t
+T
 race
-: %22 +
+ =
  Log
@@ -3195,24 +3195,20 @@
 ilders.E
-xception
+vent
 Builder(
@@ -3212,36 +3212,210 @@
 er()
-.setDescription(description)
+%0A                        .setCategory(%22RedditWear%22 + BuildConfig.VERSION_NAME)%0A                        .setAction(stackTrace)%0A                        .setLabel(throwableMessage)%0A                        
 .bui

Change exception logging to be reduce noise