package com.lukekorth.screennotifications;

import android.app.Application;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.lukekorth.mailable_log.MailableLog;
import com.lukekorth.screennotifications.helpers.DatabaseMigrations;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

import io.realm.Realm;
import io.realm.RealmConfiguration;

public class ScreenNotificationsApplication extends Application implements Thread.UncaughtExceptionHandler {

    private static final String VERSION = "version";

    private Thread.UncaughtExceptionHandler mDefaultExceptionHandler;

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

        Realm.init(this);
        RealmConfiguration realmConfiguration = new RealmConfiguration.Builder()
                .schemaVersion(1)
                .migration(new DatabaseMigrations())
                .build();
        Realm.setDefaultConfiguration(realmConfiguration);

        migrate();

        MailableLog.init(this, BuildConfig.DEBUG);

        mDefaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(this);
    }

    private void migrate() {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        int version = prefs.getInt(VERSION, 0);
        if (BuildConfig.VERSION_CODE > version) {
            String now = new Date().toString();

            prefs.edit()
                    .putString("upgrade_date", now)
                    .putInt(VERSION, BuildConfig.VERSION_CODE)
                    .apply();

            MailableLog.clearLog(this);
        }
    }

    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        Logger logger = LoggerFactory.getLogger("Exception");

        logger.error("thread.toString(): " + thread.toString());
        logger.error("Exception: " + ex.toString());
        logger.error("Exception stacktrace:");
        for (StackTraceElement trace : ex.getStackTrace()) {
            logger.error(trace.toString());
        }

        logger.error("");

        logger.error("cause.toString(): " + ex.getCause().toString());
        logger.error("Cause: " + ex.getCause().toString());
        logger.error("Cause stacktrace:");
        for (StackTraceElement trace : ex.getCause().getStackTrace()) {
            logger.error(trace.toString());
        }

        mDefaultExceptionHandler.uncaughtException(thread, ex);
    }
}

@@ -278,66 +278,8 @@
 s;%0A%0A
-import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0A
 impo
@@ -426,51 +426,8 @@
 ion 
-implements Thread.UncaughtExceptionHandler 
 %7B%0A%0A 
@@ -483,79 +483,8 @@
 %22;%0A%0A
-    private Thread.UncaughtExceptionHandler mDefaultExceptionHandler;%0A%0A
     
@@ -903,146 +903,8 @@
 UG);
-%0A%0A        mDefaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();%0A        Thread.setDefaultUncaughtExceptionHandler(this);
 %0A   
@@ -1400,829 +1400,7 @@
    %7D
-%0A%0A    @Override%0A    public void uncaughtException(Thread thread, Throwable ex) %7B%0A        Logger logger = LoggerFactory.getLogger(%22Exception%22);%0A%0A        logger.error(%22thread.toString(): %22 + thread.toString());%0A        logger.error(%22Exception: %22 + ex.toString());%0A        logger.error(%22Exception stacktrace:%22);%0A        for (StackTraceElement trace : ex.getStackTrace()) %7B%0A            logger.error(trace.toString());%0A        %7D%0A%0A        logger.error(%22%22);%0A%0A        logger.error(%22cause.toString(): %22 + ex.getCause().toString());%0A        logger.error(%22Cause: %22 + ex.getCause().toString());%0A        logger.error(%22Cause stacktrace:%22);%0A        for (StackTraceElement trace : ex.getCause().getStackTrace()) %7B%0A            logger.error(trace.toString());%0A        %7D%0A%0A        mDefaultExceptionHandler.uncaughtException(thread, ex);%0A    %7D
 %0A%7D%0A

Remove exception logging in favor of crash reporting