package ca.etsmtl.applets.etsmobile.http;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.octo.android.robospice.request.springandroid.SpringAndroidSpiceRequest;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.kobjects.base64.Base64;

import java.util.Iterator;

import ca.etsmtl.applets.etsmobile.model.Sponsor;
import ca.etsmtl.applets.etsmobile.model.SponsorList;
import ca.etsmtl.applets.etsmobile2.R;

public class AppletsApiSponsorRequest extends SpringAndroidSpiceRequest<SponsorList> {

    private Context context;

    public AppletsApiSponsorRequest(Context context) {
        super(SponsorList.class);
        this.context = context;
    }

    @Override
    public SponsorList loadDataFromNetwork() throws Exception {

        String address = context.getString(R.string.applets_api_sponsors);

        SponsorList sponsorList = null;

        try {

            // Instantiate the custom HttpClient to call Https request
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet get = new HttpGet(address);

            String apiCredentials = context.getString(R.string.credentials_api);

            String basicAuth = "Basic " + new String(new Base64().encode(apiCredentials.getBytes()));
            get.setHeader("Authorization", basicAuth);
            get.setHeader("Content-Type", "application/json; charset=utf-8");
            String method = get.getMethod();

            HttpResponse getResponse = client.execute(get);
            HttpEntity responseEntity = getResponse.getEntity();

            String result = EntityUtils.toString(responseEntity, "UTF-8");
            JSONObject data = new JSONObject(result);

            /*JSONObject root = new JSONObject(result);
            JSONObject data = root.getJSONObject("data");*/
            ObjectMapper mapper = new ObjectMapper();
            sponsorList = new SponsorList();
            Iterator keys = data.keys();
            while (keys.hasNext()) {

                int imageResource = 0;
                String currentDynamicKey = (String) keys.next();

                //imageResource = assignResource(currentDynamicKey);

                JSONArray arraySponsors = data.getJSONArray(currentDynamicKey);

                for (int i = 0; i < arraySponsors.length(); i++) {
                    Sponsor sponsor = mapper.readValue(arraySponsors.getJSONObject(i).toString(), Sponsor.class);
                    //sponsor.setImageResource(imageResource);
                    sponsorList.add(sponsor);
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return sponsorList;
    }
}

@@ -177,16 +177,45 @@
 Mapper;%0A
+import com.google.gson.Gson;%0A
 import c
@@ -288,16 +288,130 @@
 Request;
+%0Aimport com.squareup.okhttp.OkHttpClient;%0Aimport com.squareup.okhttp.Request;%0Aimport com.squareup.okhttp.Response;
 %0A%0Aimport
@@ -1294,146 +1294,10 @@
     
-SponsorList sponsorList = null;%0A%0A        try %7B%0A%0A            // Instantiate the custom HttpClient to call Https request%0A            Default
+Ok
 Http
@@ -1316,23 +1316,18 @@
 t = new 
-Default
+Ok
 HttpClie
@@ -1328,32 +1328,33 @@
 tpClient();%0A
+%0A
         
     HttpGet 
@@ -1345,406 +1345,122 @@
     
-    HttpGet get = new HttpGet(address);%0A%0A            String apiCredentials = context.getString(R.string.credentials_api);%0A%0A            String basicAuth = %22Basic %22 + new String(new Base64().encode(apiCredentials.getBytes()));%0A            get.setHeader(%22Authorization%22, basicAuth);%0A            get.setHeader(%22Content-Type%22, %22application/json; charset=utf-8%22);%0A            String method = get.getMetho
+Request request = new Request.Builder()%0A                .url(address)%0A                .get()%0A                .buil
 d();
@@ -1469,24 +1469,16 @@
         
-    
-Http
 Response
@@ -1478,20 +1478,17 @@
 esponse 
-getR
+r
 esponse 
@@ -1500,92 +1500,37 @@
 ent.
-execute(get);%0A            HttpEntity responseEntity = getResponse.getEntity
+newCall(request).execute
 ();%0A%0A
-    
     
@@ -1553,163 +1553,31 @@
 t = 
-EntityUtils.toString(responseEntity, %22UTF-8%22);%0A            JSONObject data = new JSONObject(result);%0A%0A            /*JSONObject root = new JSONObject(result
+response.body().string(
 );%0A 
@@ -1587,905 +1587,79 @@
     
- 
-   JSONObject data = root.getJSONObject(%22data%22);*/%0A            ObjectMapper mapper = new ObjectMapper();%0A            sponsorList = new SponsorList();%0A            Iterator keys = data.keys();%0A            while (keys.hasNext()) %7B%0A%0A                int imageResource = 0;%0A                String currentDynamicKey = (String) keys.next();%0A%0A                //imageResource = assignResource(currentDynamicKey);%0A%0A                JSONArray arraySponsors = data.getJSONArray(currentDynamicKey);%0A%0A                for (int i = 0; i %3C arraySponsors.length(); i++) %7B%0A                    Sponsor sponsor = mapper.readValue(arraySponsors.getJSONObject(i).toString(), Sponsor.class);%0A                    //sponsor.setImageResource(imageResource);%0A                    sponsorList.add(sponsor);%0A                %7D%0A            %7D%0A        %7D catch (Exception e) %7B%0A            System.out.println(e.getMessage());%0A        %7D
+SponsorList sponsors = new Gson().fromJson(result, SponsorList.class);%0A
 %0A   
@@ -1677,20 +1677,17 @@
  sponsor
-List
+s
 ;%0A    %7D%0A

Fix partners request