package com.ikea.shared.store.service;

import android.content.Context;
import android.location.Location;
import android.text.TextUtils;
import com.ikea.shared.AppConfigData;
import com.ikea.shared.AppConfigManager;
import com.ikea.shared.BaseService;
import com.ikea.shared.BaseService.NetworkCaller;
import com.ikea.shared.network.KompisRequest;
import com.ikea.shared.network.RequestUtil;
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.LocationUtil;
import com.ikea.shared.util.ServiceCallback;
import com.ikea.shared.util.Util;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutorService;

public final class StoreService extends BaseService
{
  private static StoreService instance;
  private Object obj = new Object();

  private StoreService(Context paramContext)
  {
    super(paramContext);
  }

  public static StoreService i(Context paramContext)
  {
    try
    {
      if (instance == null)
        instance = new StoreService(paramContext);
      StoreService localStoreService = instance;
      return localStoreService;
    }
    finally
    {
    }
  }

  private void updateFavStore(StoreList paramStoreList)
  {
    if ((AppConfigManager.i(this.mContext).getAppConfigData() != null) && (AppConfigManager.i(this.mContext).getAppConfigData().getFavStore() != null))
    {
      StoreRef localStoreRef = AppConfigManager.i(this.mContext).getAppConfigData().getFavStore();
      int i = paramStoreList.getStoreRefList().getStoreRef().size();
      for (int j = 0; j < i; j++)
        if (((StoreRef)paramStoreList.getStoreRefList().getStoreRef().get(j)).getStoreNo().equals(localStoreRef.getStoreNo()))
          AppConfigManager.i(this.mContext).getAppConfigData().setFavStore((StoreRef)paramStoreList.getStoreRefList().getStoreRef().get(j));
    }
  }

  public StoreRef getNearestStore(StoreRefList paramStoreRefList, Location paramLocation, boolean paramBoolean)
  {
    if ((paramStoreRefList != null) && (paramStoreRefList.getStoreRef() != null) && (!paramStoreRefList.getStoreRef().isEmpty()))
    {
      ArrayList localArrayList;
      synchronized (this.obj)
      {
        float[] arrayOfFloat = { 0.0F, 0.0F, 0.0F };
        localArrayList = new ArrayList(paramStoreRefList.getStoreRef());
        if ((paramLocation == null) || (paramLocation.getLatitude() <= -2147483648.0D) || (paramLocation.getLongitude() <= -2147483648.0D))
          break label345;
        Iterator localIterator2 = localArrayList.iterator();
        while (localIterator2.hasNext())
        {
          StoreRef localStoreRef3 = (StoreRef)localIterator2.next();
          StoreLocation localStoreLocation = localStoreRef3.getStoreLocation();
          if ((localStoreLocation != null) && (!TextUtils.isEmpty(localStoreLocation.getLat())) && (!TextUtils.isEmpty(localStoreLocation.getLong())))
          {
            Location.distanceBetween(paramLocation.getLatitude(), paramLocation.getLongitude(), Double.parseDouble(localStoreLocation.getLat()), Double.parseDouble(localStoreLocation.getLong()), arrayOfFloat);
            if ((arrayOfFloat != null) && (arrayOfFloat.length > 0))
              localStoreRef3.setStoreDistance(String.valueOf(arrayOfFloat[0] / 1000.0F));
          }
        }
      }
      Collections.sort(localArrayList, Util.mStoreComparator);
      while (true)
      {
        paramStoreRefList.setStoreRef(localArrayList);
        StoreRef localStoreRef1 = AppConfigManager.i(this.mContext).getAppConfigData().getFavStore();
        Object localObject3 = null;
        if (paramBoolean)
        {
          localObject3 = null;
          if (localStoreRef1 != null)
          {
            Iterator localIterator1 = localArrayList.iterator();
            StoreRef localStoreRef2;
            do
            {
              boolean bool = localIterator1.hasNext();
              localObject3 = null;
              if (!bool)
                break;
              localStoreRef2 = (StoreRef)localIterator1.next();
            }
            while (!localStoreRef2.getStoreNo().equalsIgnoreCase(localStoreRef1.getStoreNo()));
            localObject3 = localStoreRef2;
          }
        }
        if (localObject3 == null)
          localObject3 = (StoreRef)localArrayList.get(0);
        return localObject3;
        label345: Collections.sort(localArrayList, Util.mStoreNameComparator);
      }
    }
    return null;
  }

  public StoreList getStores(KompisRequest paramKompisRequest)
    throws Exception
  {
    StoreList localStoreList = (StoreList)new BaseService.NetworkCaller(this, StoreList.class, this.mContext).execute(paramKompisRequest);
    if (localStoreList != null)
      try
      {
        if (localStoreList.getStoreRefList() != null)
        {
          List localList1 = localStoreList.getStoreRefList().getStoreRef();
          List localList2 = Collections.synchronizedList(new ArrayList());
          Iterator localIterator = localList1.iterator();
          while (localIterator.hasNext())
          {
            StoreRef localStoreRef = (StoreRef)localIterator.next();
            if (localStoreRef.getStoreInformation() != null)
            {
              String str = localStoreRef.getStoreName();
              if (str != null)
                str = str.replaceAll("(?i)ikea", "").trim();
              localStoreRef.setStoreName(str);
              localList2.add(localStoreRef);
            }
            localStoreList.getStoreRefList().setStoreRef(localList2);
          }
        }
      }
      catch (Exception localException)
      {
        localException.printStackTrace();
      }
    return localStoreList;
  }

  public void getStoresAsync(final ServiceCallback<StoreList> paramServiceCallback)
  {
    this.mExecutorService.execute(new Runnable()
    {
      public void run()
      {
        try
        {
          Object localObject = StoreCache.i(StoreService.this.mContext).getCachedStoreList();
          if ((localObject == null) || (((StoreList)localObject).getStoreRefList() == null))
          {
            String str = LocationUtil.getRetailCode(StoreService.this.mContext);
            KompisRequest localKompisRequest = RequestUtil.getStoresRequest(StoreService.this.mContext);
            StoreList localStoreList = StoreService.this.getStores(localKompisRequest);
            if ((localStoreList != null) && (localStoreList.isSuccessful()) && (LocationUtil.getRetailCode(StoreService.this.mContext).equals(str)))
            {
              localObject = localStoreList;
              StoreService.this.updateFavStore(localStoreList);
              StoreCache.i(StoreService.this.mContext).updateStoreList((StoreList)localObject, System.currentTimeMillis(), LocationUtil.getRetailCode(StoreService.this.mContext), LocationUtil.getLanguageCode(StoreService.this.mContext));
            }
          }
          paramServiceCallback.callbackDone(localObject, null, null);
          return;
        }
        catch (Exception localException)
        {
          localException.printStackTrace();
          paramServiceCallback.callbackDone(null, null, localException);
        }
      }
    });
  }

  public void getStoresAsync(final ServiceCallback<StoreList> paramServiceCallback, final String paramString1, final String paramString2)
  {
    this.mExecutorService.execute(new Runnable()
    {
      public void run()
      {
        try
        {
          KompisRequest localKompisRequest = RequestUtil.getStoresRequest(StoreService.this.mContext, paramString1, paramString2);
          StoreList localStoreList = StoreService.this.getStores(localKompisRequest);
          paramServiceCallback.callbackDone(localStoreList, null, null);
          return;
        }
        catch (Exception localException)
        {
          localException.printStackTrace();
          paramServiceCallback.callbackDone(null, null, localException);
        }
      }
    });
  }
}

/* Location:           C:\Documents and Settings\Cesar Cabello Cea\Mis documentos\Downloads\apk-downloader\com.ikea.kompis-6_dex2jar.jar
 * Qualified Name:     com.ikea.shared.store.service.StoreService
 * JD-Core Version:    0.6.2
 */