package com.anly.githubapp.data.net.client;

import android.app.Application;

import com.anly.githubapp.common.util.NetworkUtil;
import com.anly.githubapp.data.net.client.core.BaseOkHttpClient;

import java.io.File;
import java.io.IOException;

import javax.inject.Inject;

import okhttp3.Cache;
import okhttp3.CacheControl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by mingjun on 16/7/20.
 */
public class CacheHttpClient extends BaseOkHttpClient {

    private static final long CACHE_SIZE = 1024 * 1024 * 50;

    @Inject
    Application mContext;

    @Inject
    public CacheHttpClient() {
    }

    @Override
    public OkHttpClient.Builder customize(OkHttpClient.Builder builder) {
        File cacheFile = new File(mContext.getCacheDir(), "github_repo");
        Cache cache = new Cache(cacheFile, CACHE_SIZE);
        builder.cache(cache);

        builder.addNetworkInterceptor(mCacheControlInterceptor);
        return builder;
    }

    private final Interceptor mCacheControlInterceptor = new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            if (!NetworkUtil.isNetworkAvailable(mContext)) {
                request = request.newBuilder()
                        .cacheControl(CacheControl.FORCE_CACHE)
                        .build();
            }

            Response originalResponse = chain.proceed(request);

            if (NetworkUtil.isNetworkAvailable(mContext)) {

                String cacheControl = request.cacheControl().toString();

                return originalResponse.newBuilder()
                        .header("Cache-Control", cacheControl)
                        .build();
            } else {
                return originalResponse.newBuilder()
                        .header("Cache-Control", CacheControl.FORCE_CACHE.toString())
                        .build();
            }
        }
    };
}

@@ -759,24 +759,49 @@
  builder) %7B%0A
+        // set cache dir%0A
         File
@@ -862,16 +862,16 @@
 repo%22);%0A
-
         
@@ -1254,16 +1254,108 @@
 quest();
+%0A%0A            // Add FORCE_CACHE cache control for each request if network is not available.
 %0A       
@@ -1764,24 +1764,127 @@
 oString();%0A%0A
+                // Add cache control header for response same as request's while network is available.%0A
             
@@ -2013,32 +2013,32 @@
       .build();%0A
-
             %7D el
@@ -2042,16 +2042,120 @@
  else %7B%0A
+                // Add cache control header for response to FORCE_CACHE while network is not available.%0A
         

add some comment for cache http client.