package edu.pdx.its.portal.routelandia;

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

import com.google.android.gms.maps.model.LatLng;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Created by loc on 1/30/15.
 */
public class HttpAsyncTask extends AsyncTask<String, Void, JSONObject>{

    //private LatLng startPoint = new LatLng(45.509534, -122.681081);
    //private LatLng endPoint =  new LatLng(45.509534, -122.681081);
    private LatLng startPoint = MapsActivity.startPoint;
    private LatLng endPoint =  MapsActivity.endPoint;
    String midpoint = "17:30";
    String weekday = DayPickSelectedListener.weekDay;

    String url = "http://capstoneaa.cs.pdx.edu/api/trafficstats";

    @Override
    protected JSONObject doInBackground(String... params) {
        return postJsonObject(url, makingJson());
    }
    protected void onPostExecute(JSONObject result) {
        super.onPostExecute(result);

    }

    public JSONObject makingJson() {

        JSONObject jsonObject = new JSONObject();
        try {
            JSONObject startJsonObject = new JSONObject();
            startJsonObject.put("lng", startPoint.longitude);
            startJsonObject.put("lat", startPoint.latitude);

            JSONObject endJsonObject = new JSONObject();
            endJsonObject.put("lng", endPoint.longitude);
            endJsonObject.put("lat", endPoint.latitude);

            JSONObject time = new JSONObject();
            time.put("midpoint", midpoint);
            time.put("weekday", weekday);

            jsonObject.put("startpt", startJsonObject);
            jsonObject.put("endpt", endJsonObject);
            jsonObject.put("time", time);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

    public JSONObject postJsonObject(String url, JSONObject jsonObject){
        InputStream inputStream = null;
        String result = "";
        try {

            // 1. create HttpClient
            HttpClient httpclient = new DefaultHttpClient();

            // 2. make POST request to the given URL

            //http://capstoneaa.cs.pdx.edu/api/trafficstats
            HttpPost httpPost = new HttpPost(url);

            System.out.println("url: " + url);

            // 4. convert JSONObject to JSON to String

             String json = jsonObject.toString();

            System.out.println("my json : " + json);
            // 5. set json to StringEntity
            StringEntity se = new StringEntity(json);

            // 6. set httpPost Entity
            httpPost.setEntity(se);

            // 7. Set some headers to inform server about the type of the content
            httpPost.setHeader("Accept", "application/json");
            httpPost.setHeader("Content-type", "application/json");

            // 8. Execute POST request to the given URL
            HttpResponse httpResponse = httpclient.execute(httpPost);

            // 9. receive response as inputStream
            inputStream = httpResponse.getEntity().getContent();

            // 10. convert inputstream to string
            if(inputStream != null)
                result = convertInputStreamToString(inputStream);
            else
                result = "Did not work!";

        } catch (Exception e) {
            Log.d("InputStream", e.getLocalizedMessage());
        }

        JSONObject json = null;
        try {
            JSONArray jsonArray = new JSONArray(result);
            json = jsonArray.getJSONObject(0);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        // 11. return result
        System.out.println("json from the response: " + json);
        return json;
    }

    private String convertInputStreamToString(InputStream inputStream) throws IOException {
        BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
        String line = "";
        String result = "";
        while((line = bufferedReader.readLine()) != null)
            result += line;

        inputStream.close();
        return result;
    }
}

@@ -684,33 +684,33 @@
 ject%3E%7B%0A%0A    
-//private
+protected
  LatLng star
@@ -720,59 +720,23 @@
 int 
-= new LatLng(45.509534, -122.681081);%0A    //priva
+;%0A    protec
 te
+d
  Lat
@@ -752,192 +752,93 @@
 int 
-=  new LatLng(45.509534, -122.681081);%0A    private LatLng startPoint = MapsActivity.startPoint;%0A    private LatLng endPoint =  MapsActivity.endPoint;%0A    String midpoint = %2217:30%22;%0A   
+;%0A    protected String midpoint ; //backend call time field is midpoint%0A    protected
  Str
@@ -849,17 +849,19 @@
 weekday 
-=
+;//
  DayPick
@@ -887,24 +887,30 @@
 ekDay;%0A%0A    
+final 
 String url =
@@ -956,24 +956,265 @@
 ficstats%22;%0A%0A
+    public HttpAsyncTask(LatLng startPoint, LatLng endPoint, String midpoint, String weekday) %7B%0A        this.startPoint = startPoint;%0A        this.endPoint = endPoint;%0A        this.midpoint = midpoint;%0A        this.weekday = weekday;%0A    %7D%0A%0A
     @Overrid
@@ -1271,24 +1271,83 @@
 . params) %7B%0A
+        System.out.println(%22jsoncreated %22 + makingJson());%0A
         retu
@@ -1440,24 +1440,79 @@
 t result) %7B%0A
+        System.out.println(%22json download %22 + result);%0A
         supe

add constructor with argument with send via date pick up class