package net.rimoto.intlphoneinput;


import android.content.Context;

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

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class CountriesFetcher {
    private static CountryList mCountries;

    private static String getCountriesJson(Context context) {
        String json;
        try {
            InputStream is = context.getResources().openRawResource(R.raw.countries);
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            json = new String(buffer, "UTF-8");
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        return json;
    }
    public static CountryList getCountries(Context context) {
        if(mCountries != null) {
            return mCountries;
        }
        mCountries = new CountryList();
        try {
            JSONArray countries = new JSONArray(getCountriesJson(context));
            for(int i=0; i<countries.length(); i++) {
                try {
                    JSONObject country = (JSONObject) countries.get(i);
                    mCountries.add(new Country(country.getString("name"), country.getString("iso2"), country.getInt("dialCode")));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return mCountries;
    }


    public static class CountryList extends ArrayList<Country> {
        public int indexOfIso(String iso) {
            for(int i=0; i<this.size(); i++) {
                if(this.get(i).getIso().toUpperCase().equals(iso.toUpperCase())) {
                    return i;
                }
            }
            return -1;
        }
        public int indexOfDialCode(int dialCode) {
            for(int i=0; i<this.size(); i++) {
                if(this.get(i).getDialCode() == dialCode) {
                    return i;
                }
            }
            return -1;
        }
    }
}

@@ -237,31 +237,8 @@
 ist;
-%0Aimport java.util.List;
 %0A%0Apu
@@ -311,16 +311,170 @@
 tries;%0A%0A
+    /**%0A     * Fetch JSON from RAW resource%0A     * @param context Context%0A     * @param resource Resource int of the RAW file%0A     * @return JSON%0A     */%0A
     priv
@@ -494,29 +494,27 @@
 ring get
-CountriesJson
+JsonFromRaw
 (Context
@@ -513,32 +513,46 @@
 (Context context
+, int resource
 ) %7B%0A        Stri
@@ -599,17 +599,26 @@
 Stream i
-s
+nputStream
  = conte
@@ -655,23 +655,16 @@
 rce(
-R.raw.countries
+resource
 );%0A 
@@ -686,17 +686,26 @@
 size = i
-s
+nputStream
 .availab
@@ -767,17 +767,26 @@
        i
-s
+nputStream
 .read(bu
@@ -805,17 +805,26 @@
        i
-s
+nputStream
 .close()
@@ -999,24 +999,142 @@
  json;%0A    %7D
+%0A%0A    /**%0A     * Import CountryList from RAW resource%0A     * @param context Context%0A     * @return CountryList%0A     */
 %0A    public 
@@ -1367,29 +1367,44 @@
 (get
-CountriesJson(context
+JsonFromRaw(context, R.raw.countries
 ));%0A
@@ -1978,16 +1978,173 @@
 ntry%3E %7B%0A
+        /**%0A         * Fetch item index on the list by iso%0A         * @param iso Country's iso2%0A         * @return index of the item in the list%0A         */%0A
         
@@ -2395,32 +2395,250 @@
 rn -1;%0A        %7D
+%0A%0A        /**%0A         * Fetch item index on the list by dial coder%0A         * @param dialCode Country's dial code prefix%0A         * @return index of the item in the list%0A         */%0A        @SuppressWarnings(%22unused%22)
 %0A        public 

add more comments