package com.pr0gramm.app;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.StrictMode;

import com.crashlytics.android.Crashlytics;
import com.crashlytics.android.answers.Answers;
import com.f2prateek.dart.Dart;
import com.facebook.stetho.Stetho;
import com.facebook.stetho.okhttp.StethoInterceptor;
import com.orm.SugarApp;
import com.pr0gramm.app.ui.ActivityErrorHandler;
import com.pr0gramm.app.util.CrashlyticsLogHandler;
import com.pr0gramm.app.util.HandlerThreadScheduler;
import com.pr0gramm.app.util.Lazy;
import com.squareup.okhttp.OkHttpClient;

import net.danlew.android.joda.JodaTimeAndroid;

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

import butterknife.ButterKnife;
import io.fabric.sdk.android.Fabric;
import pl.brightinventions.slf4android.LoggerConfiguration;
import rx.Scheduler;
import rx.android.plugins.RxAndroidPlugins;
import rx.android.plugins.RxAndroidSchedulersHook;

import static com.pr0gramm.app.ui.dialogs.ErrorDialogFragment.setGlobalErrorDialogHandler;

/**
 * Global application class for pr0gramm app.
 */
public class ApplicationClass extends SugarApp {
    private static final Logger logger = LoggerFactory.getLogger("Pr0grammApplication");

    final Lazy<AppComponent> appComponent = Lazy.of(() -> DaggerAppComponent.builder()
            .appModule(new AppModule(this))
            .httpModule(new HttpModule())
            .build());

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

        JodaTimeAndroid.init(this);

        if (BuildConfig.DEBUG) {
            logger.info("This is a development version.");
            StrictMode.enableDefaults();
            ButterKnife.setDebug(true);
            Dart.setDebug(true);

        } else {
            Settings settings = Settings.of(this);
            if (settings.analyticsEnabled()) {
                logger.info("Initialize Fabric");
                Fabric.with(this, new Crashlytics());
                Fabric.with(this, new Answers());

                LoggerConfiguration.configuration()
                        .removeRootLogcatHandler()
                        .addHandlerToRootLogger(new CrashlyticsLogHandler());

            } else {
                // just disable logging
                LoggerConfiguration.configuration()
                        .removeRootLogcatHandler();
            }
        }

        // initialize this to show errors always in the context of the current activity.
        setGlobalErrorDialogHandler(new ActivityErrorHandler(this));

        RxAndroidPlugins.getInstance().registerSchedulersHook(new RxAndroidSchedulersHook() {
            @Override
            public Scheduler getMainThreadScheduler() {
                return HandlerThreadScheduler.INSTANCE;
            }
        });

        Dagger.initEagerSingletons(this);

        if (BuildConfig.DEBUG) {
            logger.info("Setup stetho");
            Stetho.initializeWithDefaults(this);

            OkHttpClient okHttpClient = appComponent.get().okHttpClient();
            okHttpClient.networkInterceptors().add(new StethoInterceptor());
        }

    }

    /**
     * Opens the community in the playstore.
     */
    public static void openCommunityWebpage(Activity activity) {
        Intent intent = new Intent(Intent.ACTION_VIEW,
                Uri.parse("https://play.google.com/apps/testing/io.github.mopsalarm.pr0gramm.gp2"));

        activity.startActivity(intent);
    }

    public static void openDownloadFreeVersion(Activity activity) {
        Intent intent = new Intent(Intent.ACTION_VIEW,
                Uri.parse("https://app.pr0gramm.com/#download"));

        activity.startActivity(intent);
    }

    public static ApplicationClass get(Context context) {
        return (ApplicationClass) context.getApplicationContext();
    }
}

@@ -404,16 +404,68 @@
 garApp;%0A
+import com.pr0gramm.app.services.SingleShotService;%0A
 import c
@@ -497,32 +497,32 @@
 tyErrorHandler;%0A
-
 import com.pr0gr
@@ -1621,32 +1621,79 @@
 id.init(this);%0A%0A
+        Settings settings = Settings.of(this);%0A
         if (Buil
@@ -1904,59 +1904,8 @@
 e %7B%0A
-            Settings settings = Settings.of(this);%0A
     
@@ -3214,24 +3214,560 @@
 %0A        %7D%0A%0A
+        SingleShotService singleShotService = appComponent.get().singleShotService();%0A        if (singleShotService.isFirstTime(%22migrate.MpegDecoderToVPX%22)) %7B%0A            if (settings.raw().getBoolean(%22pref_force_mpeg_decoder%22, false)) %7B%0A                logger.info(%22Switch to normal software decoder%22);%0A%0A                settings.edit()%0A                        .putBoolean(%22pref_force_mpeg_decoder%22, false)%0A                        .putBoolean(%22pref_use_software_decoder%22, true)%0A                        .apply();%0A            %7D%0A        %7D%0A
     %7D%0A%0A    /

Disable java decoder setting and move to normal software decoder.