package com.battlelancer.seriesguide;

import android.content.Context;
import android.database.sqlite.SQLiteException;
import android.support.annotation.Nullable;
import android.util.Log;
import com.battlelancer.seriesguide.thetvdbapi.TvdbException;
import com.battlelancer.seriesguide.thetvdbapi.TvdbTraktException;
import com.battlelancer.seriesguide.util.Utils;
import com.crashlytics.android.Crashlytics;
import java.net.UnknownHostException;
import timber.log.Timber;

/**
 * A customized {@link timber.log.Timber.DebugTree} that logs to Crashlytics and Google Analytics.
 * Always drops debug and verbose logs.
 */
public class AnalyticsTree extends Timber.DebugTree {

    public static final String CATEGORY_THETVDB_ERROR = "TheTVDB Error";

    private final Context context;

    public AnalyticsTree(Context context) {
        this.context = context.getApplicationContext();
    }

    @Override
    protected void log(int priority, String tag, @Nullable String message, Throwable t) {
        if (priority == Log.ERROR) {
            // remove any stack trace attached by Timber
            if (message != null) {
                int newLine = message.indexOf('\n');
                if (newLine > 0) {
                    message = message.substring(0, newLine);
                }
            }

            // special treatment for some exceptions
            if (t instanceof TvdbException) {
                if (t instanceof TvdbTraktException) {
                    return; // already tracked as trakt error
                }
                TvdbException e = (TvdbException) t;
                Throwable cause = e.getCause();
                if (cause != null && cause instanceof UnknownHostException) {
                    return; // do not track
                }
                Utils.trackCustomEvent(context,
                        CATEGORY_THETVDB_ERROR,
                        tag + ": " + message,
                        e.getMessage());
                return;
            }
        }

        // drop empty messages
        if (message == null) {
            return;
        }
        // drop debug and verbose logs
        if (priority == Log.DEBUG || priority == Log.VERBOSE) {
            return;
        }

        // transform priority into string
        String level = null;
        switch (priority) {
            case Log.INFO:
                level = "INFO";
                break;
            case Log.WARN:
                level = "WARN";
                break;
            case Log.ERROR:
                level = "ERROR";
                break;
        }

        // finally log to crashlytics
        Crashlytics.log(level + "/" + tag + ": " + message);

        // track some non-fatal exceptions with crashlytics
        if (priority == Log.ERROR) {
            if (t instanceof SQLiteException) {
                Crashlytics.logException(t);
            }
        }
    }
}

@@ -68,56 +68,8 @@
 xt;%0A
-import android.database.sqlite.SQLiteException;%0A
 impo
@@ -938,24 +938,250 @@
 owable t) %7B%0A
+        // drop empty messages%0A        if (message == null) %7B%0A            return;%0A        %7D%0A        // drop debug and verbose logs%0A        if (priority == Log.DEBUG %7C%7C priority == Log.VERBOSE) %7B%0A            return;%0A        %7D%0A%0A
         if (
@@ -1266,47 +1266,8 @@
 ber%0A
-            if (message != null) %7B%0A    
     
@@ -1319,28 +1319,24 @@
             
-    
 if (newLine 
@@ -1358,20 +1358,16 @@
         
-  
-  
 message 
@@ -1399,34 +1399,16 @@
 wLine);%0A
-                %7D%0A
         
@@ -1500,29 +1500,126 @@
 eof 
-TvdbException) %7B%0A    
+UnknownHostException /* mostly devices loosing connection */) %7B%0A                return; // do not track%0A            %7D%0A
     
@@ -1657,36 +1657,32 @@
 aktException) %7B%0A
-    
                 
@@ -1727,37 +1727,79 @@
 ror%0A            
+%7D%0A
     
-%7D
+        if (t instanceof TvdbException) %7B
 %0A               
@@ -2211,32 +2211,8 @@
 ));%0A
-                return;%0A
     
@@ -2236,234 +2236,8 @@
  %7D%0A%0A
-        // drop empty messages%0A        if (message == null) %7B%0A            return;%0A        %7D%0A        // drop debug and verbose logs%0A        if (priority == Log.DEBUG %7C%7C priority == Log.VERBOSE) %7B%0A            return;%0A        %7D%0A%0A
     
@@ -2779,32 +2779,137 @@
 == Log.ERROR) %7B%0A
+            // SQLite exceptions%0A            // Retrofit Call exceptions (IOException, RuntimeException)%0A
             if (
@@ -2921,22 +2921,16 @@
 tanceof 
-SQLite
 Exceptio

Send more exceptions to Crashlytics.