/*
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package edu.pdx.its.portal.routelandia;

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

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.PolylineOptions;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by loc on 1/24/15.
 */
public class DownloadTask extends AsyncTask<String, Void, String> {

    protected GoogleMap mMap;
    protected PolylineOptions globalPoly;

    /**
     * Constructor for DownloadTask class
     * @param mMap is a field Google Map in MapsActivity class
     * @param globalPoly is field polylineOptions in MapsActivity
     */
    public DownloadTask(GoogleMap mMap, PolylineOptions globalPoly) {
        this.mMap = mMap;
        this.globalPoly = globalPoly;
    }

    /**
     * Override this method to perform a computation on a background thread. The
     * specified parameters are the parameters passed to {@link #execute}
     * by the caller of this task.
     * <p/>
     * This method can call {@link #publishProgress} to publish updates
     * on the UI thread.
     *
     * @param url The parameters of the task.
     * @return A result, defined by the subclass of this task.
     * @see #onPostExecute
     */
    @Override
    protected String doInBackground(String... url) {

        // For storing data from web service
        String data = "";

        try {
            // Fetching the data from web service
            data = downloadUrl(url[0]);
        } catch (Exception e) {
            Log.d("Background Task", e.toString());
        }
        return data;
    }

    /**
     * <p>Runs on the UI thread after {@link #doInBackground}. The
     * specified result is the value returned by {@link #doInBackground}.</p>
     * <p/>
     * <p>This method won't be invoked if the task was cancelled.</p>
     *
     * @param result The result of the operation computed by {@link #doInBackground}.
     * @see #onPreExecute
     * @see #doInBackground
     * @see #onCancelled(Object)
     */
    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        //Create ParserTask class to parse the JSON from the backend
        ParserTask parserTask = new ParserTask(mMap, globalPoly);

        // Invokes the thread for parsing the JSON data
        parserTask.execute(result);

    }

    /**
     * A method to download json data from url
     * @param strUrl is the URL API endpoint to download the highway information
     * @return the string json
     * @throws IOException
     */
    private String downloadUrl(String strUrl) throws IOException {
        String data = "";
        InputStream iStream;
        HttpURLConnection urlConnection;

        try {
            URL url = new URL(strUrl);

            // Creating an http connection to communicate with url
            urlConnection = (HttpURLConnection) url.openConnection();

            // Connecting to url
            urlConnection.connect();

            // Reading data from url
            iStream = urlConnection.getInputStream();

            // Create bufferedReader from input
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(iStream));

            StringBuilder stringBuilder = new StringBuilder();

            String line;

            //append all line from buffered Reader into string builder
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }

            //convert the string builder into string and update its for data
            data = stringBuilder.toString();

            //close the buffered reader
            bufferedReader.close();

        } catch (Exception e) {
            Log.d("Exception while downloading url", e.toString());
        }

        return data;
    }
}

@@ -1033,24 +1033,26 @@
  String%3E %7B%0A%0A
+//
     protecte
@@ -1065,24 +1065,26 @@
 leMap mMap;%0A
+//
     protecte
@@ -1113,25 +1113,29 @@
 alPoly;%0A
-%0A
+//%0A//
     /**%0A
      * C
@@ -1122,24 +1122,26 @@
 /%0A//    /**%0A
+//
      * Const
@@ -1166,24 +1166,26 @@
 dTask class%0A
+//
      * @para
@@ -1235,16 +1235,18 @@
 y class%0A
+//
      * @
@@ -1303,24 +1303,26 @@
 ctivity%0A
+//
      */%0A
     publ
@@ -1309,24 +1309,26 @@
 y%0A//     */%0A
+//
     public D
@@ -1381,24 +1381,26 @@
 obalPoly) %7B%0A
+//
         this
@@ -1413,16 +1413,18 @@
 = mMap;%0A
+//
         
@@ -1453,16 +1453,18 @@
 alPoly;%0A
+//
     %7D%0A%0A 
@@ -2880,16 +2880,18 @@
 backend%0A
+//
         
@@ -2948,17 +2948,21 @@
 lPoly);%0A
-%0A
+//%0A//
         
@@ -3005,24 +3005,26 @@
 e JSON data%0A
+//
         pars

remove unnessary field