package com.example.barte_000.chopin;

import android.app.usage.UsageEvents;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

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

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.Route;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.Path;
import retrofit2.http.POST;

/**
 * Created by barte_000 on 19.11.2016.
 */

public class API {

    private static APIInterface apiInterface;
    private static String url = "http://frelia.org:3001/api/v1/offers/";

    public static APIInterface getClient() {
        if (apiInterface == null) {
            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .addInterceptor(
                            new Interceptor() {
                                @Override
                                public Response intercept(Interceptor.Chain chain) throws IOException {
                                    Request request = chain.request().newBuilder()
                                            .addHeader("Accept", "Application/JSON").build();
                                    return chain.proceed(request);
                                }
                            }).build();

            Gson gson = new GsonBuilder()
                    .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
                    .create();

            Retrofit client = new Retrofit.Builder()
                    .baseUrl(url)
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .client(okHttpClient)
                    .build();
            apiInterface = client.create(APIInterface.class);
        }
        return apiInterface;
    }

    public interface APIInterface {

        // Offers
        @GET("/api/v1/offers/")
        Call<List<Offer>> getAllOffers();

        @GET("/offers/{offer_id}")
        Call<Offer> getOffer(@Path("offer_id") Integer offer_id);

        @POST("/api/v1/offers/")
        Call<Offer> sendOffer(@Body Offer offer);

        @POST("/api/v1/sign_in")
        Call <User> getToken(@Body User user);
    }
}


@@ -340,16 +340,43 @@
 2.Call;%0A
+import retrofit2.Callback;%0A
 import r
@@ -498,19 +498,79 @@
 t2.http.
-GET
+Field;%0Aimport retrofit2.http.GET;%0Aimport retrofit2.http.Headers
 ;%0Aimport
@@ -1946,32 +1946,465 @@
 ass);%0A        %7D%0A
+%0A%0A        User user = new User();%0A        Call%3CUser%3E call = apiInterface.getToken(user.email, user.password);%0A        call.enqueue(new Callback%3CUser%3E() %7B%0A%0A            @Override%0A            public void onResponse(Call%3CUser%3E call, retrofit2.Response%3CUser%3E response) %7B%0A                response.headers();%0A            %7D%0A%0A            @Override%0A            public void onFailure(Call%3CUser%3E call, Throwable t) %7B%0A%0A            %7D%0A        %7D);%0A%0A
         return a
@@ -2651,24 +2651,45 @@
 offer_id);%0A%0A
+        @Headers(%7B%7D)%0A
         @POS
@@ -2827,22 +2827,71 @@
 en(@
-Body User user
+Field(%22email%22) String email, @Field(%22password%22) String password
 );%0A 

add headers