package com.ikea.shared.geofence;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.Geofence.Builder;
import com.ikea.shared.AppError;
import com.ikea.shared.LibConfig;
import com.ikea.shared.location.event.StoreInOutEvent;
import com.ikea.shared.store.service.StoreCache;
import com.ikea.shared.stores.model.StoreList;
import com.ikea.shared.stores.model.StoreLocation;
import com.ikea.shared.stores.model.StoreRef;
import com.ikea.shared.stores.model.StoreRefList;
import com.ikea.shared.util.AppExecutors;
import com.ikea.shared.util.L;
import com.ikea.shared.util.LocationUtil;
import com.ikea.shared.util.ServiceCallback;
import com.squareup.otto.Bus;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutorService;

public class GeofenceService
{
  protected static final float GEOFENCE_RADIUS = 250.0F;
  private static GeofenceService instance;
  private final ServiceCallback<Integer> mAddServiceCallback = new ServiceCallback()
  {
    public void done(Integer paramAnonymousInteger, AppError paramAnonymousAppError, Exception paramAnonymousException)
    {
      if ((paramAnonymousInteger != null) && (paramAnonymousInteger.intValue() == 0))
        try
        {
          StoreCache.i(GeofenceService.this.mContext).updateGeofencesList(GeofenceService.this.mGeofenceIdsAdded);
          L.I(this, "registerAllStore  all success");
          return;
        }
        catch (IOException localIOException)
        {
          L.E("Error occured:-" + localIOException.getMessage());
          return;
        }
      L.E("Geofence Add request failed");
    }
  };
  private final Bus mBus;
  private final Context mContext;
  private final ExecutorService mExecutorService = AppExecutors.stdPrio();
  private final List<String> mGeofenceIdsAdded = new ArrayList();
  private GeofenceRemover mGeofenceRemover;
  private GeofenceRequester mGeofenceRequester;
  private final Handler mHandler = new Handler();
  private final ServiceCallback<Integer> mRemoveServiceCallback = new ServiceCallback()
  {
    public void done(Integer paramAnonymousInteger, AppError paramAnonymousAppError, Exception paramAnonymousException)
    {
      if ((paramAnonymousInteger != null) && (paramAnonymousInteger.intValue() == 0))
      {
        GeofenceService.this.addGeofences();
        return;
      }
      GeofenceService.this.addGeofences();
    }
  };
  private String mStoreID = "";

  private GeofenceService(Context paramContext)
  {
    this.mContext = paramContext;
    this.mBus = LibConfig.i().bus();
    init();
  }

  private void addDummyFences(List<Geofence> paramList)
  {
    paramList.add(new Geofence.Builder().setRequestId("office").setTransitionTypes(3).setCircularRegion(Double.valueOf("28.4997293").doubleValue(), Double.valueOf("77.0682131").doubleValue(), 250.0F).setExpirationDuration(-1L).build());
    this.mGeofenceIdsAdded.add("office");
    Geofence localGeofence1 = new Geofence.Builder().setRequestId("atm").setTransitionTypes(3).setCircularRegion(Double.valueOf("28.499614").doubleValue(), Double.valueOf("77.067830").doubleValue(), 250.0F).setExpirationDuration(-1L).build();
    this.mGeofenceIdsAdded.add("atm");
    paramList.add(localGeofence1);
    Geofence localGeofence2 = new Geofence.Builder().setRequestId("plot5").setTransitionTypes(3).setCircularRegion(Double.valueOf("28.499353").doubleValue(), Double.valueOf("77.068040").doubleValue(), 250.0F).setExpirationDuration(-1L).build();
    this.mGeofenceIdsAdded.add("plot5");
    paramList.add(localGeofence2);
    Geofence localGeofence3 = new Geofence.Builder().setRequestId("cafeteria").setTransitionTypes(3).setCircularRegion(Double.valueOf("28.500418").doubleValue(), Double.valueOf("77.069070").doubleValue(), 250.0F).setExpirationDuration(-1L).build();
    this.mGeofenceIdsAdded.add("cafeteria");
    paramList.add(localGeofence3);
    Geofence localGeofence4 = new Geofence.Builder().setRequestId("tea").setTransitionTypes(3).setCircularRegion(Double.valueOf("28.499315").doubleValue(), Double.valueOf("77.066634").doubleValue(), 250.0F).setExpirationDuration(-1L).build();
    this.mGeofenceIdsAdded.add("tea");
    paramList.add(localGeofence4);
    Geofence localGeofence5 = new Geofence.Builder().setRequestId("home").setTransitionTypes(3).setCircularRegion(Double.valueOf("28.502219").doubleValue(), Double.valueOf("77.025488").doubleValue(), 250.0F).setExpirationDuration(-1L).build();
    this.mGeofenceIdsAdded.add("home");
    paramList.add(localGeofence5);
    Geofence localGeofence6 = new Geofence.Builder().setRequestId("bbq").setTransitionTypes(3).setCircularRegion(28.472415999999999D, 77.093227999999996D, 250.0F).setExpirationDuration(-1L).build();
    this.mGeofenceIdsAdded.add("bbq");
    paramList.add(localGeofence6);
    Geofence localGeofence7 = new Geofence.Builder().setRequestId("sector17").setTransitionTypes(3).setCircularRegion(28.489177000000002D, 77.055925000000002D, 250.0F).setExpirationDuration(-1L).build();
    this.mGeofenceIdsAdded.add("sector17");
    paramList.add(localGeofence7);
    Geofence localGeofence8 = new Geofence.Builder().setRequestId("iffco").setTransitionTypes(3).setCircularRegion(28.479671D, 77.069785999999993D, 250.0F).setExpirationDuration(-1L).build();
    this.mGeofenceIdsAdded.add("iffco");
    paramList.add(localGeofence8);
  }

  public static final GeofenceService i(Context paramContext)
  {
    try
    {
      if (instance == null)
        instance = new GeofenceService(paramContext);
      GeofenceService localGeofenceService = instance;
      return localGeofenceService;
    }
    finally
    {
    }
  }

  private void init()
  {
    this.mGeofenceRequester = new GeofenceRequester(this.mContext);
    this.mGeofenceRemover = new GeofenceRemover(this.mContext);
    addGeofences();
  }

  private boolean servicesConnected()
  {
    int i = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this.mContext);
    if (i == 0)
    {
      Log.d("Geofence Detection", "Location Services is available");
      return true;
    }
    String str = GooglePlayServicesUtil.getErrorString(i);
    L.E("Error occured:-" + str);
    return false;
  }

  protected void addGeofences()
  {
    boolean bool1 = StoreCache.i(this.mContext).isInStore();
    StoreList localStoreList = StoreCache.i(this.mContext).getStoreList();
    int i = 0;
    if (localStoreList != null)
    {
      StoreRefList localStoreRefList = localStoreList.getStoreRefList();
      i = 0;
      if (localStoreRefList != null)
      {
        List localList = localStoreList.getStoreRefList().getStoreRef();
        i = 0;
        if (localList != null)
        {
          int m = localStoreList.getStoreRefList().getStoreRef().size();
          i = 0;
          if (m > 0)
          {
            StoreRef localStoreRef = (StoreRef)localStoreList.getStoreRefList().getStoreRef().get(0);
            String str = localStoreRef.getStoreDistance();
            boolean bool2 = str.equals("-1");
            i = 0;
            if (!bool2)
            {
              Double localDouble = Double.valueOf(Double.parseDouble(str.replace(",", ".")));
              boolean bool3 = localDouble.doubleValue() < 0.0D;
              i = 0;
              if (bool3)
              {
                boolean bool4 = localDouble.doubleValue() < 1.0D;
                i = 0;
                if (bool4)
                {
                  i = 1;
                  this.mStoreID = localStoreRef.getStoreNo();
                }
              }
            }
          }
        }
      }
    }
    if ((bool1) || (i != 0))
    {
      WifiManager localWifiManager = (WifiManager)this.mContext.getSystemService("wifi");
      if ((localWifiManager != null) && (localWifiManager.isWifiEnabled()))
        if (LocationUtil.isIkeaWifiAccessible(this.mContext));
    }
    while (true)
    {
      try
      {
        StoreCache.i(this.mContext).updateInStoreStatus(false, "");
        ArrayList localArrayList1 = new ArrayList();
        this.mGeofenceIdsAdded.clear();
        if (localArrayList1 != null)
          localArrayList1.clear();
        if ((localStoreList == null) || (localStoreList.getStoreRefList() == null))
          break;
        ArrayList localArrayList2 = new ArrayList(localStoreList.getStoreRefList().getStoreRef());
        if ((localArrayList2 == null) || (localArrayList2.isEmpty()))
          break;
        int j = 0;
        int k = localArrayList2.size();
        if (j >= k)
          break;
        if ((((StoreRef)localArrayList2.get(j)).getStoreLocation() != null) && (!TextUtils.isEmpty(((StoreRef)localArrayList2.get(j)).getStoreLocation().getLat())) && (!TextUtils.isEmpty(((StoreRef)localArrayList2.get(j)).getStoreLocation().getLong())))
        {
          this.mGeofenceIdsAdded.add(((StoreRef)localArrayList2.get(j)).getStoreNo());
          localArrayList1.add(new Geofence.Builder().setRequestId(((StoreRef)localArrayList2.get(j)).getStoreNo()).setTransitionTypes(3).setCircularRegion(Double.valueOf(((StoreRef)localArrayList2.get(j)).getStoreLocation().getLat()).doubleValue(), Double.valueOf(((StoreRef)localArrayList2.get(j)).getStoreLocation().getLong()).doubleValue(), 250.0F).setExpirationDuration(-1L).build());
          L.I(this, "register Stroe Id " + ((StoreRef)localArrayList2.get(j)).getStoreNo());
        }
        L.I(this, "registerAllStore  all Stroe");
        if (!localArrayList1.isEmpty())
          this.mGeofenceRequester.addGeofences(localArrayList1, this.mAddServiceCallback);
        j++;
        continue;
      }
      catch (IOException localIOException3)
      {
        localIOException3.printStackTrace();
        continue;
      }
      if (!bool1)
      {
        try
        {
          StoreCache.i(this.mContext).updateInStoreStatus(true, this.mStoreID);
          this.mHandler.post(new Runnable()
          {
            public void run()
            {
              GeofenceService.this.mBus.post(new StoreInOutEvent(true, GeofenceService.this.mStoreID));
            }
          });
        }
        catch (IOException localIOException2)
        {
          localIOException2.printStackTrace();
        }
        continue;
        try
        {
          StoreCache.i(this.mContext).updateInStoreStatus(false, "");
        }
        catch (IOException localIOException1)
        {
          localIOException1.printStackTrace();
        }
        continue;
        try
        {
          StoreCache.i(this.mContext).updateInStoreStatus(false, "");
        }
        catch (IOException localIOException4)
        {
          localIOException4.printStackTrace();
        }
      }
    }
  }

  public void notifyStoreInOut(final int paramInt, final String paramString)
  {
    try
    {
      this.mHandler.post(new Runnable()
      {
        public void run()
        {
          boolean bool1;
          if (paramInt == 1)
            bool1 = true;
          while (true)
          {
            StoreList localStoreList = StoreCache.i(GeofenceService.this.mContext).getStoreList();
            if ((localStoreList != null) && (localStoreList.getStoreRefList() != null) && (localStoreList.getStoreRefList().getStoreRef() != null) && (!localStoreList.getStoreRefList().getStoreRef().isEmpty()))
            {
              Iterator localIterator = new ArrayList(localStoreList.getStoreRefList().getStoreRef()).iterator();
              do
              {
                boolean bool2 = localIterator.hasNext();
                j = 0;
                if (!bool2)
                  break;
              }
              while (!((StoreRef)localIterator.next()).getStoreNo().equalsIgnoreCase(paramString));
              int j = 1;
              if (j == 0);
            }
            try
            {
              StoreCache.i(GeofenceService.this.mContext).updateInStoreStatus(bool1, paramString);
              GeofenceService.this.mBus.post(new StoreInOutEvent(bool1, paramString));
              return;
              int i = paramInt;
              bool1 = false;
              if (i != 2)
                continue;
              bool1 = false;
            }
            catch (IOException localIOException)
            {
              while (true)
                localIOException.printStackTrace();
            }
          }
        }
      });
      return;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }

  public void registerAllStore()
  {
    if (!servicesConnected())
      return;
    this.mExecutorService.execute(new Runnable()
    {
      public void run()
      {
        try
        {
          List localList = StoreCache.i(GeofenceService.this.mContext).getGetGeofencesAddedList();
          if ((localList != null) && (!localList.isEmpty()))
          {
            GeofenceService.this.mGeofenceRemover.removeGeofencesById(localList, GeofenceService.this.mRemoveServiceCallback);
            return;
          }
          GeofenceService.this.addGeofences();
          return;
        }
        catch (Exception localException)
        {
          localException.printStackTrace();
          L.E(this, "Exception registerAllStore " + localException.getMessage());
        }
      }
    });
  }
}

/* Location:           C:\Documents and Settings\Cesar Cabello Cea\Mis documentos\Downloads\apk-downloader\com.ikea.kompis-6_dex2jar.jar
 * Qualified Name:     com.ikea.shared.geofence.GeofenceService
 * JD-Core Version:    0.6.2
 */