package com.myo.stroll;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.json.JSONObject;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.AsyncTask;
import android.os.StrictMode;
import android.util.Log;

public class RequestBuilder extends AsyncTask<String, Void, JSONObject> {
	// API call parameter list
    private static final String HEADER = "https://maps.googleapis.com/maps/api/directions/json?";
    private static final String ORIGIN = "origin=";
    private static final String DEST = "destination=";
    private static final String KEY = "key=";
    private static final String DEPARTURE_TIME = "departure_time=";
    private static final String MODE = "mode=";
    // Value of api parameters
    private static final String UNITS = "units";
    private static final String METRIC = "metric";
    private static final String ALTROUTE = "alternatives";
    private static final String TRUE = "true";
    private static final String WALK = "walking";
    private static final String API_KEY = "AIzaSyA3bq0V_IMiwMGL4MR3M-1LmdbVB8kB-0k";
    
    private static String getCurrentTime() {
        return Integer.toString((int)(System.currentTimeMillis() / 1000));
    } 
    
    // Returns json string of direction api call results
    private String getDirectionString(String origin, String destination) {
    	Log.w("debugger", "Initialized");
        HttpClient client = new DefaultHttpClient();
        HttpResponse response;
        
        try {
        	// Use http get to get directions
        	String url = RequestBuilder.buildRequestURL(origin, destination);
        	Log.w("debugger", url);
            response = client.execute(new HttpGet(url));
            
            StatusLine status = response.getStatusLine();
            
            if (status.getStatusCode() == HttpStatus.SC_OK) {
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                
                out.close();
                return out.toString();
            }
            
            response.getEntity().getContent().close();
        }
        catch (ClientProtocolException e) {
            return "Client Exception";
        }
        catch (IOException e) {
            return "IO Exception";
        }
        catch (Exception e) {
            return e.toString();
        }
        
        return null;
    }
    
    // Parses json string returned from directions api into json object
    private JSONObject getDirections(String origin, String destination) {
        String directionString = this.getDirectionString(origin, destination);
        
        if (directionString != null && !directionString.isEmpty()) {
            try {
            	Log.w("debugger", "Got direction string");
                return new JSONObject(directionString);
            }
            catch(Exception e) {
                // whatever
            }
        }

        return null;
    }
    
    public static String buildRequestURL(String origin, String destination) {
        return RequestBuilder.HEADER + RequestBuilder.ORIGIN + origin + "&"
                + RequestBuilder.DEST + destination + "&"
                + RequestBuilder.KEY + RequestBuilder.API_KEY + "&"
                + RequestBuilder.ALTROUTE + RequestBuilder.TRUE + "&"
                + RequestBuilder.UNITS + RequestBuilder.METRIC + "&"
                + RequestBuilder.DEPARTURE_TIME + RequestBuilder.getCurrentTime() + "&"
                + RequestBuilder.MODE + RequestBuilder.WALK;
    }

    @Override
    protected JSONObject doInBackground(String... place) {
    	
        if (place.length != 2)
            return null;
        
        Log.w("debugger", "Starting...");
        // place[0] - origin
        // place[1] - destination
        return this.getDirections(place[0], place[1]);
    }
}

@@ -83,16 +83,89 @@
 ception;
+%0Aimport java.io.UnsupportedEncodingException;%0Aimport java.net.URLEncoder;
 %0A%0Aimport
@@ -1043,24 +1043,76 @@
  = %22mode=%22;%0A
+    private static final String REGION = %22region=%22;%0A
     // Value
@@ -1336,16 +1336,59 @@
 %22true%22;%0A
+    private static final String CA = %22ca%22;%0A
     priv
@@ -3848,24 +3848,90 @@
 ETRIC + %22&%22%0A
+                + RequestBuilder.REGION + RequestBuilder.CA + %22&%22%0A
             
@@ -4182,48 +4182,20 @@
 gth 
-!
+=
 = 2)
+ %7B
 %0A    
-        return null;%0A        %0A
     
@@ -4228,24 +4228,28 @@
 rting...%22);%0A
+    
         // p
@@ -4265,32 +4265,36 @@
  origin%0A        
+    
 // place%5B1%5D - de
@@ -4307,24 +4307,41 @@
 ion%0A        
+    try %7B%0A    %09%09%09
 return this.
@@ -4358,27 +4358,251 @@
 ons(
-place%5B0%5D, place%5B1%5D)
+URLEncoder.encode(place%5B0%5D, %22UTF-8%22), URLEncoder.encode(place%5B1%5D, %22UTF-8%22));%0A    %09%09%7D catch (UnsupportedEncodingException e) %7B%0A    %09%09%09// TODO Auto-generated catch block%0A    %09%09%09e.printStackTrace();%0A    %09%09%7D%09%0A        %7D%0A        %0A        return null
 ;%0A  

Add region biased and url encoding for directions api call