package com.kickstarter.services;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.kickstarter.BuildConfig;
import com.kickstarter.libs.DateTimeTypeConverter;
import com.kickstarter.models.CurrentUser;
import com.kickstarter.models.DiscoveryParams;
import com.kickstarter.models.Project;
import com.kickstarter.services.ApiResponses.AccessTokenEnvelope;
import com.kickstarter.services.ApiResponses.DiscoverEnvelope;

import org.joda.time.DateTime;

import retrofit.RequestInterceptor;
import retrofit.RestAdapter;
import retrofit.converter.GsonConverter;
import rx.Observable;

public class KickstarterClient {
  private final KickstarterService service;
  private final CurrentUser currentUser;

  public KickstarterClient(final CurrentUser currentUser) {
    this.currentUser = currentUser;

    RequestInterceptor requestInterceptor = request -> {
      request.addHeader("Accept", "application/json");
      request.addHeader("Kickstarter-Android-App", "1"); // TODO: Kickstarter app side
      // TODO: Look at Retrofit user agent
      // TODO: extract this so that it's easy to swap client_id for different HQ envs.
      request.addQueryParam("client_id", "***REMOVED***");
      if (currentUser.exists()) {
        request.addQueryParam("oauth_token", currentUser.getToken());
      }
    };

    Gson gson = new GsonBuilder()
      .registerTypeAdapter(DateTime.class, new DateTimeTypeConverter())
      .create();

    RestAdapter restAdapter = new RestAdapter.Builder()
      .setConverter(new GsonConverter(gson))
        // TODO: extract this so we can switch HQ envs within the app. It's very useful.
      .setEndpoint("https://***REMOVED***")
      .setRequestInterceptor(requestInterceptor)
      .setLogLevel(BuildConfig.DEBUG ? RestAdapter.LogLevel.FULL : RestAdapter.LogLevel.NONE)
      .build();

    service = restAdapter.create(KickstarterService.class);
  }

  public Observable<DiscoverEnvelope> fetchProjects(final DiscoveryParams params) {
    return service.fetchProjects(params.queryParams())
      .retry(3);
  }

  public Observable<Project> fetchProject(final Project project) {
    return Observable.just(project).mergeWith(service.fetchProject(project.id()));
  }

  public Observable<AccessTokenEnvelope> login(final String email, final String password) {
    return login(email, password, "");
  }

  public Observable<AccessTokenEnvelope> login(final String email, final String password, final String code) {
    return service.login(email, password, code);
  }
}

@@ -824,639 +824,170 @@
     
-RequestInterceptor requestInterceptor = request -%3E %7B%0A      request.addHeader(%22Accept%22, %22application/json%22);%0A      request.addHeader(%22Kickstarter-Android-App%22, %221%22); // TODO: Kickstarter app side%0A      // TODO: Look at Retrofit user agent%0A      // TODO: extract this so that it's easy to swap client_id for different HQ envs.%0A      request.addQueryParam(%22client_id%22, %22***REMOVED***%22);%0A      if (currentUser.exists
+service = kickstarterService();%0A  %7D%0A%0A  private KickstarterService kickstarterService
 ()
-)
  %7B%0A    
- 
-   request.addQueryParam(%22oauth_token%22, currentUser.getToken());%0A      %7D%0A    %7D;%0A%0A    Gson gson = new GsonBuilder()%0A      .registerTypeAdapter(DateTime.class, new DateTimeTypeConverter())%0A      .create();%0A%0A   
+return restAdapter().create(KickstarterService.class);%0A  %7D%0A%0A  private
  Res
@@ -1006,18 +1006,31 @@
 tAdapter
- =
+() %7B%0A    return
  new Res
@@ -1072,13 +1072,9 @@
 ter(
-new G
+g
 sonC
@@ -1082,20 +1082,16 @@
 nverter(
-gson
 ))%0A     
@@ -1265,16 +1265,18 @@
 erceptor
+()
 )%0A      
@@ -1383,67 +1383,754 @@
 ();%0A
-%0A
   
-  service = restAdapter.create(KickstarterService.class)
+%7D%0A%0A  private GsonConverter gsonConverter() %7B%0A    Gson gson = new GsonBuilder()%0A      .registerTypeAdapter(DateTime.class, new DateTimeTypeConverter())%0A      .create();%0A%0A    return new GsonConverter(gson);%0A  %7D%0A%0A  private RequestInterceptor requestInterceptor() %7B%0A    return request -%3E %7B%0A      request.addHeader(%22Accept%22, %22application/json%22);%0A      request.addHeader(%22Kickstarter-Android-App%22, %221%22); // TODO: Kickstarter app side%0A      // TODO: Look at Retrofit user agent%0A      // TODO: extract this so that it's easy to swap client_id for different HQ envs.%0A      request.addQueryParam(%22client_id%22, %22***REMOVED***%22);%0A      if (this.currentUser.exists()) %7B%0A        request.addQueryParam(%22oauth_token%22, this.currentUser.getToken());%0A      %7D%0A    %7D
 ;%0A  

Split up client constructor into smaller methods