package com.armandgray.seeme.services;

import android.app.IntentService;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.armandgray.seeme.models.User;
import com.armandgray.seeme.network.HttpHelper;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.IOException;

public class HttpService extends IntentService {

    public static final String TAG = "HttpService";
    public static final String HTTP_SERVICE_MESSAGE = "HTTP Service Message";
    public static final String HTTP_SERVICE_JSON_PAYLOAD = "HTTP Service JSON Payload";
    public static final String HTTP_SERVICE_STRING_PAYLOAD = "HTTP Service STRING Payload";
    public static final String JSON_BODY = "JSON_BODY";

    public HttpService() { super("HttpService"); }

    @Override
    protected void onHandleIntent(Intent intent) {
        Uri uri = intent.getData();
        Log.i(TAG, "onHandleIntent: " + uri.toString());



        User[] userArray = null;
        Intent messageIntent = new Intent(HTTP_SERVICE_MESSAGE);
        String response = getResponse(uri);
        if (response != null) {
            if (response.charAt(0) != '[') {
                messageIntent.putExtra(HTTP_SERVICE_STRING_PAYLOAD, response);
            } else {
                Gson gson = new GsonBuilder()
                        .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
                        .create();
                userArray = gson.fromJson(response, User[].class);
                messageIntent.putExtra(HTTP_SERVICE_JSON_PAYLOAD, userArray);
            }
        }

        LocalBroadcastManager broadcastManager =
                LocalBroadcastManager.getInstance(getApplicationContext());
        broadcastManager.sendBroadcast(messageIntent);
    }

    private String getResponse(Uri uri) {
        String response;
        try {
            response = HttpHelper.downloadUrl(uri.toString());
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return response;
    }
}

@@ -1059,26 +1059,24 @@
 ng());%0A%0A
-%0A%0A
         
 User%5B%5D u
@@ -1071,32 +1071,44 @@
     
-User%5B%5D userArray = null;
+String response = getResponse(uri);%0A
 %0A   
@@ -1181,42 +1181,559 @@
     
-String response = getResponse(uri)
+putMessageIntentExtra(messageIntent, response);%0A        LocalBroadcastManager.getInstance(getApplicationContext())%0A                .sendBroadcast(messageIntent);%0A    %7D%0A%0A    private String getResponse(Uri uri) %7B%0A        String response;%0A        try %7B%0A            response = HttpHelper.downloadUrl(uri.toString());%0A        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A            return null;%0A        %7D%0A        return response;%0A    %7D%0A%0A    private void putMessageIntentExtra(Intent messageIntent, String response) %7B%0A        User%5B%5D userArray
 ;%0A  
@@ -2242,467 +2242,8 @@
    %7D
-%0A%0A        LocalBroadcastManager broadcastManager =%0A                LocalBroadcastManager.getInstance(getApplicationContext());%0A        broadcastManager.sendBroadcast(messageIntent);%0A    %7D%0A%0A    private String getResponse(Uri uri) %7B%0A        String response;%0A        try %7B%0A            response = HttpHelper.downloadUrl(uri.toString());%0A        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A            return null;%0A        %7D%0A        return response;
 %0A   

refactored messageIntent code into putMessageIntentExtra