package com.expidev.gcmapp.http;

import android.content.Context;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.expidev.gcmapp.utils.Constants;

import org.apache.http.HttpStatus;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

import me.thekey.android.TheKey;
import me.thekey.android.lib.TheKeyImpl;

/**
 * Created by matthewfrederick on 1/23/15.
 */
public class GmaApiClient
{
    private final String TAG = getClass().getSimpleName();

    private static final String BASE_URL_STAGE = "https://stage.sbr.global-registry.org/api";
    private static final String BASE_URL_PROD = "https://sbr.global-registry.org/api";
    private static final String MEASUREMENTS = "/measurements";
    private static final String TOKEN = "/token";
    
    private final TheKey theKey;
    
    private String ticket;
    private Context context;
    private LocalBroadcastManager broadcastManager;
    
    public GmaApiClient(final Context context)
    {
        theKey = TheKeyImpl.getInstance(context, Constants.THEKEY_CLIENTID);
        this.context = context;
        broadcastManager = LocalBroadcastManager.getInstance(context);
    }
    
    public JSONObject authorizeUser()
    {
        try
        {
            ticket = theKey.getTicket(BASE_URL_STAGE + MEASUREMENTS + TOKEN);
            Log.i(TAG, "Ticket: " + ticket);

            if (ticket == null) return null;

            String urlString = BASE_URL_STAGE + MEASUREMENTS + TOKEN + "?st=" + ticket + "&refresh=true";
            Log.i(TAG, "URL: " + urlString);
            
            URL url = new URL(urlString);

            HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();

            connection.setReadTimeout(10000);
            connection.setConnectTimeout(10000);
            connection.setRequestMethod("GET");
            connection.setDoInput(true);

            connection.connect();

            if (connection.getResponseCode() == HttpStatus.SC_OK)
            {
                InputStream inputStream = connection.getInputStream();

                if (inputStream != null)
                {
                    String jsonAsString = readFully(inputStream, "UTF-8");
                    Log.i(TAG, jsonAsString);

                    return new JSONObject(jsonAsString);
                }
            }
        }
        catch (Exception e)
        {
            Log.e(TAG, e.getMessage(), e);
        }
        
        return null;
    }

    private String readFully(InputStream inputStream, String encoding) throws IOException
    {
        return new String(readFully(inputStream), encoding);
    }

    private byte[] readFully(InputStream inputStream) throws IOException
    {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length = 0;
        while ((length = inputStream.read(buffer)) != -1)
        {
            byteArrayOutputStream.write(buffer, 0, length);
        }
        return byteArrayOutputStream.toByteArray();
    }
}

@@ -221,16 +221,47 @@
 Status;%0A
+import org.json.JSONException;%0A
 import o
@@ -1787,32 +1787,285 @@
 rlString);%0A%0A    
+        return httpGet(url);  %0A        %7D%0A        catch (Exception e)%0A        %7B%0A            Log.e(TAG, e.getMessage(), e);%0A        %7D%0A        %0A        return null;%0A    %7D%0A    %0A    private JSONObject httpGet(URL url) throws IOException, JSONException%0A    %7B%0A
         HttpsURL
@@ -2132,36 +2132,32 @@
 ion();%0A%0A        
-    
 connection.setRe
@@ -2174,36 +2174,32 @@
 10000);%0A        
-  
-  
 connection.setCo
@@ -2219,36 +2219,32 @@
 10000);%0A        
-    
 connection.setRe
@@ -2263,36 +2263,32 @@
 %22GET%22);%0A        
- 
-   
 connection.setDo
@@ -2305,28 +2305,24 @@
 );%0A%0A        
-   
- 
 connection.c
@@ -2328,28 +2328,24 @@
 connect();%0A%0A
-    
         if (
@@ -2398,34 +2398,26 @@
 OK)%0A        
-    %7B%0A    
+%7B%0A
             
@@ -2480,28 +2480,24 @@
             
-    
 if (inputStr
@@ -2517,34 +2517,26 @@
             
-    %7B%0A    
+%7B%0A
             
@@ -2602,36 +2602,32 @@
                 
- 
-   
 Log.i(TAG, jsonA
@@ -2629,36 +2629,32 @@
 jsonAsString);%0A%0A
-    
                 
@@ -2706,118 +2706,9 @@
     
-    %7D%0A            %7D%0A        %7D%0A        catch (Exception e)%0A        %7B%0A            Log.e(TAG, e.getMessage(), e);
+%7D
 %0A   

make http get available for reuse.