package com.ikea.shared.browse.service;

import android.content.Context;
import com.ikea.shared.AppCache;
import com.ikea.shared.BaseService;
import com.ikea.shared.BaseService.NetworkCaller;
import com.ikea.shared.browse.model.CatalogElement;
import com.ikea.shared.browse.model.CatalogElementListArray;
import com.ikea.shared.browse.model.CatalogElementSubCatagoryTree;
import com.ikea.shared.browse.model.CatalogElementTree;
import com.ikea.shared.browse.model.CatalogListBaseResponse;
import com.ikea.shared.browse.model.CatalogTopElementListing;
import com.ikea.shared.browse.model.ProductListing;
import com.ikea.shared.filter.model.AppliedFilters;
import com.ikea.shared.network.KompisRequest;
import com.ikea.shared.network.RequestUtil;
import com.ikea.shared.util.LocationUtil;
import com.ikea.shared.util.ServiceCallback;
import java.net.URLEncoder;
import java.text.Collator;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutorService;

public final class CatalogService extends BaseService
{
  public static String FUNCTIONAL_CATEGORY_ID = "functional";
  private static CatalogService instance;
  private CatalogListBaseResponse mCatalog;
  private final Comparator<CatalogElement> mCatalogElementComparator = new Comparator()
  {
    public int compare(CatalogElement paramAnonymousCatalogElement1, CatalogElement paramAnonymousCatalogElement2)
    {
      return Collator.getInstance().compare(paramAnonymousCatalogElement1.getName(), paramAnonymousCatalogElement2.getName());
    }
  };

  private CatalogService(Context paramContext)
  {
    super(paramContext);
  }

  private ProductListing getCatalogElementsNew(String paramString1, String paramString2, String paramString3, AppliedFilters paramAppliedFilters)
    throws Exception
  {
    KompisRequest localKompisRequest = RequestUtil.getProductListRequest(this.mContext, paramString1, paramString2, paramString3, paramAppliedFilters);
    return (ProductListing)new BaseService.NetworkCaller(this, ProductListing.class, this.mContext).execute(localKompisRequest);
  }

  private CatalogListBaseResponse getTopElementsNew(String paramString)
    throws Exception
  {
    KompisRequest localKompisRequest = RequestUtil.getGetCatAlogRequest(this.mContext, paramString);
    CatalogListBaseResponse localCatalogListBaseResponse = (CatalogListBaseResponse)new BaseService.NetworkCaller(this, CatalogListBaseResponse.class, this.mContext).execute(localKompisRequest);
    sortCatalog(localCatalogListBaseResponse);
    return localCatalogListBaseResponse;
  }

  public static CatalogService i(Context paramContext)
  {
    try
    {
      if (instance == null)
        instance = new CatalogService(paramContext);
      CatalogService localCatalogService = instance;
      return localCatalogService;
    }
    finally
    {
    }
  }

  private void sortCatalog(CatalogListBaseResponse paramCatalogListBaseResponse)
  {
    try
    {
      if (paramCatalogListBaseResponse.getCatalogList() != null)
      {
        Iterator localIterator = paramCatalogListBaseResponse.getCatalogList().iterator();
        while (localIterator.hasNext())
        {
          CatalogTopElementListing localCatalogTopElementListing = (CatalogTopElementListing)localIterator.next();
          if ((localCatalogTopElementListing != null) && (localCatalogTopElementListing.getCatalogElementListNew() != null) && (localCatalogTopElementListing.getCatalogElementListNew().getCatalogElementList() != null))
            sortCatalog(localCatalogTopElementListing.getCatalogElementListNew().getCatalogElementList());
        }
      }
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }

  private void sortCatalog(List<CatalogElement> paramList)
  {
    Collections.sort(paramList, this.mCatalogElementComparator);
    Iterator localIterator = paramList.iterator();
    while (localIterator.hasNext())
    {
      CatalogElement localCatalogElement = (CatalogElement)localIterator.next();
      if (localCatalogElement.getCatalogElementSubCatagoryTree() != null)
        sortCatalog(localCatalogElement.getCatalogElementSubCatagoryTree().getmCatalogElementree().getCatalogElementList());
    }
  }

  public CatalogListBaseResponse getCachedCatalogInfo()
  {
    return this.mCatalog;
  }

  public void getCatalogElementsNewAsync(final String paramString1, final String paramString2, final String paramString3, final AppliedFilters paramAppliedFilters, final ServiceCallback<ProductListing> paramServiceCallback)
  {
    this.mExecutorService.execute(new Runnable()
    {
      public void run()
      {
        try
        {
          ProductListing localProductListing = CatalogService.this.getCatalogElementsNew(paramString1, paramString2, paramString3, paramAppliedFilters);
          paramServiceCallback.callbackDone(localProductListing, null, null);
          return;
        }
        catch (Exception localException)
        {
          localException.printStackTrace();
          paramServiceCallback.callbackDone(null, null, localException);
        }
      }
    });
  }

  public void getTopElementsAsyncNew(final String paramString, final ServiceCallback<CatalogListBaseResponse> paramServiceCallback)
  {
    this.mExecutorService.execute(new Runnable()
    {
      public void run()
      {
        try
        {
          CatalogService.access$002(CatalogService.this, AppCache.i(CatalogService.this.mContext).getCatalogListBaseResponse());
          if (CatalogService.this.mCatalog == null)
          {
            CatalogService.access$002(CatalogService.this, CatalogService.this.getTopElementsNew(paramString));
            if ((CatalogService.this.mCatalog != null) && (CatalogService.this.mCatalog.isSuccessful()) && (CatalogService.this.mCatalog.getCatalogList() != null) && (!CatalogService.this.mCatalog.getCatalogList().isEmpty()))
            {
              CatalogService.this.mCatalog.setLastUpdatedTime(System.currentTimeMillis());
              CatalogService.this.mCatalog.setCountryCode(LocationUtil.getRetailCode(CatalogService.this.mContext));
              CatalogService.this.mCatalog.setLanguageCode(LocationUtil.getLanguageCode(CatalogService.this.mContext));
              AppCache.i(CatalogService.this.mContext).updateTopCatalogInfo(CatalogService.this.mCatalog);
            }
          }
          paramServiceCallback.callbackDone(CatalogService.this.mCatalog, null, null);
          return;
        }
        catch (Exception localException)
        {
          localException.printStackTrace();
          paramServiceCallback.callbackDone(null, null, localException);
        }
      }
    });
  }

  public void removeCatalogFromCache()
  {
    this.mCatalog = null;
    AppCache.i(this.mContext).resetAppChace();
  }

  public void resetCachedCatalogInfo()
  {
    this.mCatalog = null;
  }

  public ProductListing searchItem(String paramString, AppliedFilters paramAppliedFilters)
    throws Exception
  {
    String str = URLEncoder.encode(paramString, "UTF-8");
    KompisRequest localKompisRequest = RequestUtil.getTextSearchRequest(this.mContext, str, paramAppliedFilters);
    return (ProductListing)new BaseService.NetworkCaller(this, ProductListing.class, this.mContext).execute(localKompisRequest);
  }

  public void searchItemAsync(final String paramString, final AppliedFilters paramAppliedFilters, final ServiceCallback<ProductListing> paramServiceCallback)
  {
    this.mExecutorService.execute(new Runnable()
    {
      public void run()
      {
        try
        {
          ProductListing localProductListing = CatalogService.this.searchItem(paramString, paramAppliedFilters);
          paramServiceCallback.callbackDone(localProductListing, null, null);
          return;
        }
        catch (Exception localException)
        {
          localException.printStackTrace();
          paramServiceCallback.callbackDone(null, null, localException);
        }
      }
    });
  }

  public void updateCachedCatalogInfo(CatalogListBaseResponse paramCatalogListBaseResponse)
  {
    this.mCatalog = paramCatalogListBaseResponse;
  }
}

/* Location:           C:\Documents and Settings\Cesar Cabello Cea\Mis documentos\Downloads\apk-downloader\com.ikea.kompis-6_dex2jar.jar
 * Qualified Name:     com.ikea.shared.browse.service.CatalogService
 * JD-Core Version:    0.6.2
 */