package de.fau.amos.virtualledger.android.dagger.module;

import android.app.Application;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

import java.lang.reflect.Type;
import java.util.Date;

import dagger.Module;
import dagger.Provides;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * Created by Simon on 07.05.2017. (taken from https://adityaladwa.wordpress.com/2016/05/09/
 * dagger-2-with-retrofit-and-okhttp-and-gson/)
 */
@Module
public class NetModule {

    /**
     *
     */
    private String baseUrl;

    /**
     * @param baseUrl
     * @methodtype constructor
     */
    public NetModule(String baseUrl) {
        this.baseUrl = baseUrl;
    }

    /**
     * @param application
     * @return cache
     */
    @Provides
    Cache provideHttpCache(Application application) {
        int cacheSize = 10 * 1024 * 1024;
        Cache cache = new Cache(application.getCacheDir(), cacheSize);
        return cache;
    }

    /**
     * @return Gson
     */
    @Provides
    Gson provideGson() {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
        gsonBuilder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
            @Override
            public Date deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
                return new Date(json.getAsJsonPrimitive().getAsLong());
            }
        });
        return gsonBuilder.create();
    }

    /**
     * @param cache
     * @return OkHttpClient
     */
    @Provides
    OkHttpClient provideOkhttpClient(Cache cache) {
        OkHttpClient.Builder client = new OkHttpClient.Builder();
        client.cache(cache);
        return client.build();
    }

    /**
     * @param gson
     * @param okHttpClient
     * @return Retrofit
     */
    @Provides
    Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) {
        return new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create(gson))
                .baseUrl(baseUrl)
                .client(okHttpClient)
                .build();
    }

}

@@ -78,24 +78,49 @@
 Application;
+%0Aimport android.util.Log;
 %0A%0Aimport com
@@ -399,52 +399,225 @@
 ort 
-java.lang.reflect.Type;%0Aimport java.util.Dat
+org.apache.commons.lang3.math.NumberUtils;%0A%0Aimport java.lang.reflect.Type;%0Aimport java.text.DateFormat;%0Aimport java.text.ParseException;%0Aimport java.text.SimpleDateFormat;%0Aimport java.util.Date;%0Aimport java.util.Local
 e;%0A%0A
@@ -1943,63 +1943,770 @@
     
-return new Date(json.getAsJsonPrimitive().getAsLong());
+//FIXME Quick fix to parse two different date formats. Should make sure the server only returns one format instead!%0A                final String jsonString = json.getAsJsonPrimitive().getAsString();%0A                if(NumberUtils.isParsable(jsonString)) %7B%0A                    return new Date(NumberUtils.createLong(jsonString));%0A                %7D else %7B%0A                    DateFormat dateFormat = new SimpleDateFormat(%22yyyy-MM-dd'T'HH:mm:ss.SSSZ%22, Locale.ENGLISH);%0A                    try %7B%0A                        return dateFormat.parse(jsonString.replaceAll(%22Z$%22, %22+0000%22));%0A                    %7D catch (ParseException e) %7B%0A                        Log.e(%22%22, %22Failed parsing date%22);%0A                        return null;%0A                    %7D%0A                %7D
 %0A   

Allow parsing both date formats returned by server