package com.ikea.kompis.util;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;
import android.view.View;
import com.ikea.kompis.fte.event.CountryUpdateEvent;
import com.squareup.otto.Bus;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.json.JSONArray;
import org.json.JSONObject;

public class ReverseGeocoderHelper
{
  private static final AndroidHttpClient ANDROID_HTTP_CLIENT = AndroidHttpClient.newInstance(ReverseGeocoderHelper.class.getName());
  public boolean running = false;

  public void fetchCountryName(final Context paramContext, final Location paramLocation, final View paramView, final Bus paramBus, boolean paramBoolean)
  {
    this.running = paramBoolean;
    if (this.running)
      return;
    new AsyncTask()
    {
      private List<String> fetchCityNameUsingGoogleMap()
      {
        String str1 = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + paramLocation.getLatitude() + "," + paramLocation.getLongitude() + "&sensor=false";
        while (true)
        {
          int k;
          try
          {
            AndroidHttpClient localAndroidHttpClient = ReverseGeocoderHelper.ANDROID_HTTP_CLIENT;
            HttpGet localHttpGet = new HttpGet(str1);
            JSONArray localJSONArray1 = (JSONArray)new JSONObject((String)localAndroidHttpClient.execute(localHttpGet, new BasicResponseHandler())).get("results");
            int i = 0;
            if (i < localJSONArray1.length())
            {
              JSONObject localJSONObject1 = localJSONArray1.getJSONObject(i);
              if (localJSONObject1.has("address_components"))
              {
                JSONArray localJSONArray2 = localJSONObject1.getJSONArray("address_components");
                int j = 0;
                if (j < localJSONArray2.length())
                {
                  JSONObject localJSONObject2 = localJSONArray2.getJSONObject(j);
                  if (localJSONObject1.has("types"))
                  {
                    JSONArray localJSONArray3 = localJSONObject2.getJSONArray("types");
                    String str2 = "";
                    String str3 = "";
                    k = 0;
                    if (k < localJSONArray3.length())
                    {
                      if ((!"country".equals(localJSONArray3.getString(k))) || (str2 != null))
                        break label380;
                      if (localJSONObject2.has("long_name"))
                        str2 = localJSONObject2.getString("long_name");
                      if (!localJSONObject2.has("short_name"))
                        break label380;
                      str3 = localJSONObject2.getString("short_name");
                      break label380;
                    }
                    if ((str2 != null) && (!str2.isEmpty()))
                    {
                      this.val$list.add(str2);
                      this.val$list.add(str3);
                      List localList = this.val$list;
                      return localList;
                    }
                  }
                  j++;
                  continue;
                }
              }
              i++;
              continue;
            }
          }
          catch (Exception localException)
          {
            localException.printStackTrace();
            this.val$list.add("");
            this.val$list.add("");
            return this.val$list;
          }
          this.val$list.add("");
          this.val$list.add("");
          return this.val$list;
          label380: k++;
        }
      }

      protected List<String> doInBackground(Void[] paramAnonymousArrayOfVoid)
      {
        String str1 = "";
        Object localObject = "";
        if (Geocoder.isPresent());
        try
        {
          List localList = new Geocoder(paramContext, Locale.getDefault()).getFromLocation(paramLocation.getLatitude(), paramLocation.getLongitude(), 1);
          if (!localList.isEmpty())
          {
            str1 = ((Address)localList.get(0)).getCountryName();
            String str2 = ((Address)localList.get(0)).getCountryCode();
            localObject = str2;
          }
          if ((str1 != null) && (!str1.isEmpty()))
          {
            this.val$list.add(str1);
            this.val$list.add(localObject);
            return this.val$list;
          }
        }
        catch (Exception localException)
        {
          while (true)
            localException.printStackTrace();
        }
        return fetchCityNameUsingGoogleMap();
      }

      protected void onPostExecute(List<String> paramAnonymousList)
      {
        ReverseGeocoderHelper.this.running = false;
        paramView.setVisibility(8);
        paramBus.post(new CountryUpdateEvent((String)paramAnonymousList.get(0), (String)paramAnonymousList.get(1)));
      }

      protected void onPreExecute()
      {
        ReverseGeocoderHelper.this.running = true;
      }
    }
    .execute(new Void[0]);
  }
}

/* Location:           C:\Documents and Settings\Cesar Cabello Cea\Mis documentos\Downloads\apk-downloader\com.ikea.kompis-6_dex2jar.jar
 * Qualified Name:     com.ikea.kompis.util.ReverseGeocoderHelper
 * JD-Core Version:    0.6.2
 */