package me.vickychijwani.spectre;

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

import com.crashlytics.android.Crashlytics;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.otto.DeadEvent;
import com.squareup.otto.Subscribe;
import com.squareup.picasso.OkHttpDownloader;
import com.squareup.picasso.Picasso;

import java.net.HttpURLConnection;

import me.vickychijwani.spectre.event.ApiErrorEvent;
import me.vickychijwani.spectre.event.BusProvider;
import me.vickychijwani.spectre.network.NetworkService;

public class SpectreApplication extends Application {

    public static final String TAG = "SpectreApplication";
    private static SpectreApplication sInstance;

    private Picasso mPicasso = null;

    @Override
    public void onCreate() {
        super.onCreate();
        Crashlytics.start(this);
        Crashlytics.log(Log.DEBUG, TAG, "APP LAUNCHED");
        BusProvider.getBus().register(this);
        sInstance = this;
        new NetworkService().start(this, getOkHttpClient());
    }

    public static SpectreApplication getInstance() {
        return sInstance;
    }

    protected OkHttpClient getOkHttpClient() {
        return new OkHttpClient();
    }

    public Picasso getPicasso() {
        if (mPicasso == null) {
            mPicasso = new Picasso.Builder(this)
                    .downloader(new OkHttpDownloader(getOkHttpClient()))
                    .listener((picasso, uri, exception) -> {
                        Log.e("Picasso", "Failed to load image: " + uri + "\n"
                                + Log.getStackTraceString(exception));
                    })
                    .build();
        }
        return mPicasso;
    }

    @Subscribe
    public void onApiErrorEvent(ApiErrorEvent event) {
        if (event.error.getResponse() != null &&
                event.error.getResponse().getStatus() != HttpURLConnection.HTTP_NOT_MODIFIED) {
            Log.e(TAG, Log.getStackTraceString(event.error));
        }
    }

    @Subscribe
    public void onDeadEvent(DeadEvent event) {
        Log.w(TAG, "Dead event ignored: " + event.event.getClass().getName());
    }

}

@@ -60,16 +60,74 @@
 cation;%0A
+import android.content.Context;%0Aimport android.os.StatFs;%0A
 import a
@@ -143,16 +143,16 @@
 il.Log;%0A
-
 %0Aimport 
@@ -188,16 +188,50 @@
 lytics;%0A
+import com.squareup.okhttp.Cache;%0A
 import c
@@ -419,16 +419,37 @@
 casso;%0A%0A
+import java.io.File;%0A
 import j
@@ -801,16 +801,229 @@
 tance;%0A%0A
+    private static final String IMAGE_CACHE_PATH = %22images%22;%0A    private static final int MIN_DISK_CACHE_SIZE = 5 * 1024 * 1024; // 5MB%0A    private static final int MAX_DISK_CACHE_SIZE = 50 * 1024 * 1024; // 50MB%0A
     priv
@@ -1479,24 +1479,191 @@
 pClient() %7B%0A
+        File cacheDir = createCacheDir(this, IMAGE_CACHE_PATH);%0A        long size = calculateDiskCacheSize(cacheDir);%0A        Cache cache = new Cache(cacheDir, size);%0A
         retu
@@ -1683,16 +1683,32 @@
 Client()
+.setCache(cache)
 ;%0A    %7D%0A
@@ -2166,24 +2166,24 @@
 ;%0A        %7D%0A
-
         retu
@@ -2194,32 +2194,1020 @@
 Picasso;%0A    %7D%0A%0A
+    static long calculateDiskCacheSize(File dir) %7B%0A        long size = MIN_DISK_CACHE_SIZE;%0A        try %7B%0A            StatFs statFs = new StatFs(dir.getAbsolutePath());%0A            long available = statFs.getBlockCountLong() * statFs.getBlockSizeLong();%0A            // Target 2%25 of the total space.%0A            size = available / 50;%0A        %7D catch (IllegalArgumentException ignored) %7B%0A        %7D%0A        // Bound inside min/max size for disk cache.%0A        return Math.max(Math.min(size, MAX_DISK_CACHE_SIZE), MIN_DISK_CACHE_SIZE);%0A    %7D%0A%0A    private static File createCacheDir(Context context, String path) %7B%0A        File cacheDir = context.getApplicationContext().getExternalCacheDir();%0A        if (cacheDir == null)%0A            cacheDir = context.getApplicationContext().getCacheDir();%0A        File cache = new File(cacheDir, path);%0A        if (!cache.exists()) %7B%0A            //noinspection ResultOfMethodCallIgnored%0A            cache.mkdirs();%0A        %7D%0A        return cache;%0A    %7D%0A%0A
     @Subscribe%0A 

Fix #60: Images don't seem to be cached offline.