package com.locovna.wmappy.controller;

import android.util.Log;

import com.locovna.wmappy.model.Country;

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

import java.util.ArrayList;
import java.util.Iterator;

/**
 * Helper methods related to requesting and receiving country data from JSON
 */

public class QueryUtils {

  public static final String LOG_TAG = QueryUtils.class.getSimpleName();
  //  public static final String REQUEST_URL = "https://raw.githubusercontent.com/David-Haim/CountriesToCitiesJSON/master/countriesToCities.json";
  public static final String TEST_JSON_RESPONCE = "{\n" +
      "  \"China\": [\"fendou\", \"hetao\"],\n" +
      "  \"Japan:\" : [\"tokyo\", \"hiroshima\"]\n" +
      "}";

  /**
   * No one should create {@link QueryUtils},
   * This class is only meant to hold static variables and methods
   */
  private QueryUtils() {
  }

  public static ArrayList<Country> extractCountries() {
    ArrayList<Country> countries = new ArrayList<>();

    try {
      JSONObject obj = new JSONObject(TEST_JSON_RESPONCE);

      for (int i = 0; i < obj.length(); i++) {

        // parse every key in JSONObject
        Iterator<String> keys = obj.keys();
        while (keys.hasNext()) {
          String countryName = (String) keys.next();
          JSONArray cities = obj.getJSONArray(countryName);

          // convert JSONArray to ArrayList<String>
          ArrayList<String> citiesArray = new ArrayList<String>();
          if (cities != null) {
            int len = cities.length();
            for (int x = 0; x < len; x++) {
              citiesArray.add(cities.get(i).toString());
            }
          }

          Country country = new Country(countryName, citiesArray);
          countries.add(country);
          Log.v(LOG_TAG, country.getCountry() + country.getCities());
        }
      }

    } catch (JSONException e) {
      Log.e(LOG_TAG, "Problem parsing the countries JSON results", e);
    }

    return countries;
  }
}



@@ -701,17 +701,16 @@
  %5C%22Japan
-:
 %5C%22 : %5B%5C%22
@@ -1090,58 +1090,8 @@
 );%0A%0A
-      for (int i = 0; i %3C obj.length(); i++) %7B%0A%0A  
     
@@ -1131,18 +1131,16 @@
 t%0A      
- 
- 
 Iterator
@@ -1173,18 +1173,16 @@
 ;%0A      
-  
 while (k
@@ -1196,24 +1196,63 @@
 Next()) %7B%0A  
+      Log.v(LOG_TAG, %22keys: %22 + keys);%0A
         Stri
@@ -1286,26 +1286,24 @@
 eys.next();%0A
-  
         JSON
@@ -1357,18 +1357,16 @@
         
- 
- 
 // conve
@@ -1399,18 +1399,16 @@
 String%3E%0A
-  
         
@@ -1472,18 +1472,16 @@
         
- 
- 
 if (citi
@@ -1490,26 +1490,24 @@
  != null) %7B%0A
-  
           in
@@ -1541,18 +1541,16 @@
         
-  
 for (int
@@ -1554,24 +1554,24 @@
 int 
-x
+i
  = 0; 
-x
+i
  %3C len; 
 x++)
@@ -1570,17 +1570,15 @@
 en; 
-x
+i
 ++) %7B%0A
-  
     
@@ -1634,30 +1634,26 @@
 ;%0A          
-  %7D%0A  
+%7D%0A
         %7D%0A%0A 
@@ -1659,18 +1659,16 @@
         
-  
 Country 
@@ -1712,26 +1712,24 @@
 tiesArray);%0A
-  
         coun
@@ -1752,26 +1752,24 @@
 y);%0A        
-  
 Log.v(LOG_TA
@@ -1816,26 +1816,16 @@
 ies());%0A
-        %7D%0A
       %7D%0A

delete secondary unnecessary loop