/*
   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.graphics.Color;
import android.os.AsyncTask;

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

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by loc on 1/24/15.
 */
public class ParserTask extends AsyncTask<String, Integer, List<Highway>> {

    protected GoogleMap mMap;
    protected PolylineOptions globalPoly;

    public ParserTask(GoogleMap mMap, PolylineOptions globalPoly) {
        this.mMap = mMap;
        this.globalPoly = globalPoly;
    }

    // Parsing the data in non-ui thread
    @Override
    protected List<Highway> doInBackground(String... jsonData) {

//        HashMap<Integer, List<LatLng>> routes = new HashMap<>();
        List<Highway> routes =  new ArrayList<>();
        try {
            JSONArray jObject = new JSONArray(jsonData[0]);
            JSONParser parser = new JSONParser();

            // Starts parsing data
            routes = parser.parse(jObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return routes;
    }

    // Executes in UI thread, after the parsing process
    @Override
    protected void onPostExecute(List<Highway> result) {
        PolylineOptions line = new PolylineOptions();
        for (int i = 0; i < result.size(); i++) {
            List<LatLng> points = result.get(i).getLatLngList();
            if (points != null) {
                PolylineOptions lineOptions = new PolylineOptions();

                globalPoly.addAll(points);
                line.addAll(points);
                lineOptions.addAll(points);
                lineOptions.width(10);
                lineOptions.color(Color.GREEN);
                lineOptions.geodesic(true);

                mMap.addPolyline(lineOptions);
            }
        }

    }
}


@@ -1223,43 +1223,522 @@
    /
-/ Parsing the data in non-ui thread
+**%0A     * Override this method to perform a computation on a background thread. The%0A     * specified parameters are the parameters passed to %7B@link #execute%7D%0A     * by the caller of this task.%0A     * %3Cp/%3E%0A     * This method can call %7B@link #publishProgress%7D to publish updates%0A     * on the UI thread.%0A     *%0A     * @param jsonData The parameters of the task.%0A     * @return A result, defined by the subclass of this task.%0A     * @see #onPreExecute()%0A     * @see #onPostExecute%0A     * @see #publishProgress%0A     */
 %0A   
@@ -1818,75 +1818,8 @@
  %7B%0A%0A
-//        HashMap%3CInteger, List%3CLatLng%3E%3E routes = new HashMap%3C%3E();%0A
     
@@ -1861,24 +1861,25 @@
 ayList%3C%3E();%0A
+%0A
         try 
@@ -2184,21 +2184,32 @@
    /
-/ Executes in
+**%0A     * %3Cp%3ERuns on the
  UI 
@@ -2218,35 +2218,396 @@
 read
-,
  after 
-the parsing process
+%7B@link #doInBackground%7D. The%0A     * specified result is the value returned by %7B@link #doInBackground%7D.%3C/p%3E%0A     * %3Cp/%3E%0A     * %3Cp%3EThis method won't be invoked if the task was cancelled.%3C/p%3E%0A     *%0A     * @param highwayList The result of the operation computed by %7B@link #doInBackground%7D.%0A     * @see #onPreExecute%0A     * @see #doInBackground%0A     * @see #onCancelled(Object)%0A     */
 %0A   
@@ -2664,21 +2664,26 @@
 ighway%3E 
-resul
+highwayLis
 t) %7B%0A   
@@ -2761,21 +2761,26 @@
  0; i %3C 
-resul
+highwayLis
 t.size()
@@ -2822,21 +2822,26 @@
 oints = 
-resul
+highwayLis
 t.get(i)

clean up and add java doc