/*
   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 com.google.android.gms.maps.model.LatLng;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

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

/**
 * Created by loc on 1/17/15.
 */
public class JSONParser {
//    public HashMap<Integer, List<LatLng>> parse(JSONArray jsArray) {
//        HashMap<Integer, List<LatLng>> segment = new HashMap<>();
//
//        try {
//            for (int i = 0; i < jsArray.length(); i++) {
//                JSONObject jsonObject = (JSONObject) jsArray.get(i);
//
//                int stationId = (int) jsonObject.get("stationid");
//
//                if (!jsonObject.isNull("geojson_raw")) {
//                    JSONObject segment_raw = (JSONObject) jsonObject.get("geojson_raw");
//
//                    JSONArray jsonArray = segment_raw.getJSONArray("coordinates");
//
//                    List<LatLng> listLatLong = new ArrayList<LatLng>();
//
//                    for (int j = 0; j < jsonArray.length(); j++) {
//                        double latitude = Double.parseDouble(((JSONArray) jsonArray.get(j)).get(1).toString());
//                        double longtitude = Double.parseDouble(((JSONArray) jsonArray.get(j)).get(0).toString());
//                        listLatLong.add(new LatLng(latitude, longtitude));
//                    }
//
//                    segment.put(stationId, listLatLong);
//                }
//            }
//        } catch (JSONException e) {
//            e.printStackTrace();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return segment;
//    }

    public List<Highway> parse(JSONArray jsonArray){
        List<Highway> highwayList =  new ArrayList<>();
        try{
            for (int i = 0; i <jsonArray.length() ; i++) {
                JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                int highwayid = jsonObject.getInt("highwayid");
                String highwayname = jsonObject.getString("highwayname");
                highwayList.add(i, new Highway(highwayname, highwayid));
                JSONObject fullGeoJson = (JSONObject) jsonObject.get("fullGeoJson");
                JSONArray coordinates = (JSONArray) fullGeoJson.get("coordinates");
                for (int j = 0; j <coordinates.length() ; j++) {
                    double latitude = Double.parseDouble(((JSONArray) coordinates.get(j)).get(1).toString());
                    double longtitude = Double.parseDouble(((JSONArray) coordinates.get(j)).get(0).toString());
                    highwayList.get(i).addLatLng(new LatLng(latitude, longtitude));
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return highwayList;
    }
}

@@ -849,555 +849,85 @@
 r %7B%0A
-//
     
-public HashMap%3CInteger, List%3CLatLng%3E%3E parse(JSONArray jsArray) %7B%0A//        HashMap%3CInteger, List%3CLatLng%3E%3E segment = new HashMap%3C%3E();%0A//%0A//        try %7B%0A//            for (int i = 0; i %3C jsArray.length(); i++) %7B%0A//                JSONObject jsonObject = (JSONObject) jsArray.get(i);%0A//%0A//                int stationId = (int) jsonObject.get(%22stationid%22);%0A//%0A//                if (!jsonObject.isNull(%22geojson_raw%22)) %7B%0A//                    JSONObject segment_raw = (JSONObject) jsonObject.get(%22geojson_raw%22);%0A//%0A//                    JSONArray
+/**%0A     * Parse the json array which download from the web%0A     * @param
  jso
@@ -936,818 +936,137 @@
 rray
- = segment_raw.getJSONArray(%22coordinates%22);%0A//%0A//                    List%3CLatLng%3E listLatLong = new ArrayList%3CLatLng%3E();%0A//%0A//                    for (int j = 0; j %3C jsonArray.length(); j++) %7B%0A//                        double latitude = Double.parseDouble(((JSONArray) jsonArray.get(j)).get(1).toString());%0A//                        double longtitude = Double.parseDouble(((JSONArray) jsonArray.get(j)).get(0).toString());%0A//                        listLatLong.add(new LatLng(latitude, longtitude));%0A//                    %7D%0A//%0A//                    segment.put(stationId, listLatLong);%0A//                %7D%0A//            %7D%0A//        %7D catch (JSONException e) %7B%0A//            e.printStackTrace();%0A//        %7D catch (Exception e) %7B%0A//            e.printStackTrace();%0A//        %7D%0A//        return segment;%0A//    %7D%0A
+%0A     * @return the list of highways which contain the latlng%0A     * so the Mapsactivity can draw freeway line on the map%0A     */
 %0A   
@@ -1239,24 +1239,82 @@
 () ; i++) %7B%0A
+                //Create JSON Object for each array index%0A
             
@@ -1371,16 +1371,65 @@
 .get(i);
+%0A%0A                //Get highwayid from the object
 %0A       
@@ -1484,16 +1484,66 @@
 wayid%22);
+%0A%0A                //Get highway name in the object
 %0A       
@@ -1604,24 +1604,79 @@
 ghwayname%22);
+%0A%0A                //add the highway to the list highway
 %0A           
@@ -1733,24 +1733,25 @@
 ighwayid));%0A
+%0A
             
@@ -1822,16 +1822,70 @@
 oJson%22);
+%0A%0A                //Create json array from coordinates
 %0A       
@@ -1960,16 +1960,145 @@
 nates%22);
+%0A%0A                //for each json array coordinates, create latlng and%0A                //add it to the list latlng of its highway
 %0A       
@@ -2485,32 +2485,33 @@
 %7D%0A            %7D%0A
+%0A
         %7D catch 
@@ -2559,24 +2559,24 @@
 ackTrace();%0A
-
         %7D%0A  
@@ -2565,32 +2565,33 @@
 ce();%0A        %7D%0A
+%0A
         return h

clean up and add java doc