package ckmah.mangoreader.parse;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.squareup.okhttp.Cache;
import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import com.william.mangoreader.R;

import java.io.File;
import java.io.IOException;
import java.util.List;

import ckmah.mangoreader.activity.MangaItemActivity;
import ckmah.mangoreader.model.MangaEdenImageItem;
import ckmah.mangoreader.model.MangaEdenMangaChapterItem;
import ckmah.mangoreader.model.MangaEdenMangaDetailItem;
import ckmah.mangoreader.model.MangaEdenMangaListItem;
import retrofit.Call;
import retrofit.GsonConverterFactory;
import retrofit.Retrofit;
import retrofit.http.GET;
import retrofit.http.Path;

public class MangaEden {
    public static final String MANGAEDEN_IMAGE_CDN = "https://cdn.mangaeden.com/mangasimg/";

    public class MangaEdenList {
        public List<MangaEdenMangaListItem> manga;
    }

    public class MangaEdenChapter {
        public List<MangaEdenImageItem> images;
    }

    public interface MangaEdenService {
        @GET("/api/list/0")
        Call<MangaEdenList> listAllManga();

        @GET("/api/manga/{id}")
        Call<MangaEdenMangaDetailItem> getMangaDetails(@Path("id") String mangaId);


        @GET("/api/chapter/{id}")
        Call<MangaEdenChapter> getMangaImages(@Path("id") String mangaId);
    }

    private static MangaEdenService service;

    public static MangaEdenService getMangaEdenService(Context context) {
        if (service == null) {
            Log.d("SORTING", "CREATING NEW CACHE");

            OkHttpClient okHttpClient = new OkHttpClient();
            // TODO try wiping cache to see what happens
            File httpCacheDirectory = new File(context.getCacheDir(), "responses");
            Cache cache = new Cache(httpCacheDirectory, 20 * 1024 * 1024);
            okHttpClient.setCache(cache);
            okHttpClient.interceptors().add(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Response response = chain.proceed(chain.request());
//                    response.cacheControl().
                    return response;
                }
            });

            Gson gson = new GsonBuilder()
                    .registerTypeAdapter(MangaEdenMangaChapterItem.class, new MangaEdenMangaChapterItem.ChapterDeserializer())
                    .registerTypeAdapter(MangaEdenImageItem.class, new MangaEdenImageItem.ImageDeserializer())
                    .create();

            service = new Retrofit.Builder()
                    .baseUrl("https://www.mangaeden.com/")
                    .client(okHttpClient)
                    .addConverterFactory(GsonConverterFactory.create(gson))
//                    .setRequestInterceptor(new RequestInterceptor() {
//                        @Override
//                        public void intercept(RequestFacade request) {
//                            // TODO test to make sure it does get cached i.e. doesn't use internet for the next day
//                            int maxStale = 60 * 60 * 24; // tolerate 1 day stale
//                            request.addHeader("Cache-Control", "public, max-age=" + maxStale);
//                        }
//                    })
//                    .setProfiler(new Profiler() {
//                        @Override
//                        public Object beforeCall() {
//                            return null;
//                        }
//
//                        @Override
//                        public void afterCall(RequestInformation requestInfo, long elapsedTime, int statusCode, Object beforeCallData) {
//                            Log.d("Retrofit Profiler", String.format("HTTP %d %s %s (%dms)",
//                                    statusCode, requestInfo.getMethod(), requestInfo.getRelativePath(), elapsedTime));
//                        }
//                    })
                    .build()
                    .create(MangaEdenService.class);
        }
        return service;
    }

    static public void setThumbnail(String url, Context context, final ImageView imageView) {
        url = MANGAEDEN_IMAGE_CDN + url;
        Picasso.with(context)
                .load(url)
                .placeholder(R.drawable.ic_image_white)
                .fit().centerCrop()
                .transform(PaletteTransformation.instance())
                .into(imageView);
    }

    static public void setMangaArt(String url, final ImageView imageView, final MangaItemActivity activity) {
        url = MANGAEDEN_IMAGE_CDN + url;

        Picasso.with(activity)
                .load(url)
                .fit().centerCrop()
                .placeholder(R.drawable.ic_image_white)
                .transform(PaletteTransformation.instance())
                .into(imageView, new Callback.EmptyCallback() {
                    @Override
                    public void onSuccess() {
                        Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
                        imageView.setImageBitmap(bitmap);
                    }
                });
    }

    static public void setMangaImage(String url, Context context, final ImageView imageView) {
        url = MANGAEDEN_IMAGE_CDN + url;

        Picasso.with(context)
                .load(url)
                .fit().centerInside()
                .noFade()
                .placeholder(R.drawable.ic_image_white)
                .into(imageView);
    }
}

@@ -2345,24 +2345,98 @@
 Exception %7B%0A
+                    int maxStale = 60 * 60 * 24; // tolerate 1 day stale%0A%0A
             
@@ -2491,26 +2491,24 @@
 request());%0A
-//
             
@@ -2528,23 +2528,79 @@
 nse.
-c
+newBuilder().addHeader(%22C
 ache
+-
 Control
-().
+%22, %22public, max-age=%22 + maxStale);
 %0A   
@@ -3126,16 +3126,16 @@
 Client)%0A
+
         
@@ -3206,1201 +3206,8 @@
 n))%0A
-//                    .setRequestInterceptor(new RequestInterceptor() %7B%0A//                        @Override%0A//                        public void intercept(RequestFacade request) %7B%0A//                            // TODO test to make sure it does get cached i.e. doesn't use internet for the next day%0A//                            int maxStale = 60 * 60 * 24; // tolerate 1 day stale%0A//                            request.addHeader(%22Cache-Control%22, %22public, max-age=%22 + maxStale);%0A//                        %7D%0A//                    %7D)%0A//                    .setProfiler(new Profiler() %7B%0A//                        @Override%0A//                        public Object beforeCall() %7B%0A//                            return null;%0A//                        %7D%0A//%0A//                        @Override%0A//                        public void afterCall(RequestInformation requestInfo, long elapsedTime, int statusCode, Object beforeCallData) %7B%0A//                            Log.d(%22Retrofit Profiler%22, String.format(%22HTTP %25d %25s %25s (%25dms)%22,%0A//                                    statusCode, requestInfo.getMethod(), requestInfo.getRelativePath(), elapsedTime));%0A//                        %7D%0A//                    %7D)%0A
     

Set one day cache