package com.ikea.shared;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ikea.shared.event.NewVersionAvailableEvent;
import com.ikea.shared.network.WLRequest;
import com.ikea.shared.util.L;
import com.ikea.shared.util.ServiceCallback;
import com.ikea.shared.util.UiUtil;
import com.ikea.shared.util.Util;
import com.squareup.otto.Bus;
import com.worklight.wlclient.api.WLClient;
import com.worklight.wlclient.api.WLErrorCode;
import com.worklight.wlclient.api.WLFailResponse;
import com.worklight.wlclient.api.WLProcedureInvocationData;
import com.worklight.wlclient.api.WLRequestOptions;
import com.worklight.wlclient.api.WLResponse;
import com.worklight.wlclient.api.WLResponseListener;
import com.worklight.wlclient.api.challengehandler.WLChallengeHandler;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import org.json.JSONObject;

public class WLEngine extends BroadcastReceiver
{
  private static final String HEADER_ADAPTER_NAME = "adapter-name";
  private static final String HEADER_PROCEDURE_NAME = "procedure-name";
  protected static final String TAG = WLEngine.class.getSimpleName();
  private static WLEngine sInstance;
  private volatile boolean connected;
  private volatile boolean connecting;
  private boolean mAppUpdateAvailable = false;
  private final Bus mBus;
  private final Context mContext;
  private final Handler mHandler;
  private WLResponseListener mPendingListener;
  private WLRequest mPendingReq;
  private boolean mRegisterReceiver;
  private final WLClient mWLClient;
  private final WLResponseListener mWlInitResponseListener = new WLResponseListener()
  {
    public void onFailure(WLFailResponse paramAnonymousWLFailResponse)
    {
      L.D(WLEngine.TAG, "WL connecttion fail " + paramAnonymousWLFailResponse);
      WLEngine.access$502(WLEngine.this, false);
      WLEngine.access$602(WLEngine.this, false);
      if (WLEngine.this.mPendingListener != null)
      {
        WLEngine.this.mPendingListener.onFailure(paramAnonymousWLFailResponse);
        WLEngine.access$702(WLEngine.this, null);
        WLEngine.access$802(WLEngine.this, null);
      }
    }

    public void onSuccess(WLResponse paramAnonymousWLResponse)
    {
      L.D(WLEngine.TAG, "WL connected");
      WLEngine.access$502(WLEngine.this, true);
      WLEngine.access$602(WLEngine.this, false);
      if ((WLEngine.this.mPendingReq != null) && (WLEngine.this.mPendingListener != null))
      {
        WLEngine.i(WLEngine.this.mContext).invoke(WLEngine.this.mPendingReq, WLEngine.this.mPendingListener);
        WLEngine.access$702(WLEngine.this, null);
        WLEngine.access$802(WLEngine.this, null);
      }
    }
  };

  private WLEngine(Context paramContext)
  {
    this.mContext = paramContext;
    this.mWLClient = WLClient.createInstance(paramContext);
    this.mBus = LibConfig.i().bus();
    this.mHandler = LibConfig.i().getHandler();
    if (this.mWLClient != null)
      this.mWLClient.registerChallengeHandler(new RemoteDisableChallengeHandler("wl_remoteDisableRealm", paramContext));
  }

  public static WLEngine i(Context paramContext)
  {
    try
    {
      WLEngine localWLEngine1 = sInstance;
      if (localWLEngine1 == null);
      try
      {
        sInstance = new WLEngine(paramContext);
        WLEngine localWLEngine2 = sInstance;
        return localWLEngine2;
      }
      catch (Exception localException)
      {
        while (true)
          localException.printStackTrace();
      }
    }
    finally
    {
    }
  }

  public void init()
  {
    this.mRegisterReceiver = false;
    if (this.mWLClient != null)
    {
      if ((!this.connected) && (!this.connecting))
      {
        this.connecting = true;
        this.mRegisterReceiver = true;
        this.mWLClient.connect(this.mWlInitResponseListener);
      }
      this.mHandler.post(new Runnable()
      {
        public void run()
        {
          if (WLEngine.this.mRegisterReceiver)
            WLEngine.this.mContext.registerReceiver(WLEngine.this, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
          WLEngine.this.mBus.register(WLEngine.this);
        }
      });
    }
  }

  public boolean invoke(WLRequest paramWLRequest, WLResponseListener paramWLResponseListener)
  {
    if ((this.mWLClient != null) && (UiUtil.isConnectionAvailable(this.mContext)))
    {
      if (this.connected)
      {
        this.mPendingReq = null;
        this.mPendingListener = null;
        L.D(TAG, "calling adapter " + paramWLRequest.getAdapterName() + " procedure " + paramWLRequest.getProcedureName());
        WLProcedureInvocationData localWLProcedureInvocationData = new WLProcedureInvocationData(paramWLRequest.getAdapterName(), paramWLRequest.getProcedureName(), true);
        localWLProcedureInvocationData.setParameters(new String[] { "junk", "param" });
        localWLProcedureInvocationData.setCompressResponse(true);
        this.mWLClient.addGlobalHeader("procedure-name", paramWLRequest.getProcedureName());
        this.mWLClient.addGlobalHeader("adapter-name", paramWLRequest.getAdapterName());
        HashMap localHashMap = paramWLRequest.getHeader();
        if ((localHashMap != null) && (!localHashMap.isEmpty()))
        {
          Iterator localIterator = localHashMap.keySet().iterator();
          while (localIterator.hasNext())
          {
            String str = (String)localIterator.next();
            L.D(TAG, "adding header " + str + " " + (String)localHashMap.get(str));
            this.mWLClient.addGlobalHeader(str, (String)localHashMap.get(str));
          }
        }
        WLRequestOptions localWLRequestOptions = new WLRequestOptions();
        localWLRequestOptions.setTimeout(120000);
        this.mWLClient.invokeProcedure(localWLProcedureInvocationData, paramWLResponseListener, localWLRequestOptions);
        return true;
      }
      this.mPendingReq = paramWLRequest;
      this.mPendingListener = paramWLResponseListener;
      if (!this.connecting)
        init();
      L.D(TAG, "WL client not connected");
      return true;
    }
    return false;
  }

  public void onReceive(Context paramContext, Intent paramIntent)
  {
    if (UiUtil.isConnectionAvailable(paramContext))
    {
      if (!this.connected)
        init();
      return;
    }
    this.connected = false;
  }

  public void shutdown()
  {
    this.mHandler.post(new Runnable()
    {
      public void run()
      {
        WLEngine.this.mContext.unregisterReceiver(WLEngine.this);
        try
        {
          WLEngine.this.mBus.unregister(WLEngine.this);
          return;
        }
        catch (Exception localException)
        {
          L.W("No event handler to unregister");
        }
      }
    });
  }

  public static class GenericInvokeRequestWlResponseListener<T>
    implements WLResponseListener
  {
    protected ServiceCallback<T> mCallback;
    protected T mResponse;
    private final Class<T> mResponseType;

    public GenericInvokeRequestWlResponseListener(ServiceCallback<T> paramServiceCallback, Class<T> paramClass)
    {
      this.mCallback = paramServiceCallback;
      this.mResponseType = paramClass;
    }

    public T getResponse()
    {
      return this.mResponse;
    }

    public void onFailure(WLFailResponse paramWLFailResponse)
    {
      Log.d("KOMPIS", "WL call fail " + paramWLFailResponse.getStatus());
      if ((WLErrorCode.UNRESPONSIVE_HOST != paramWLFailResponse.getErrorCode()) || (this.mCallback != null))
        this.mCallback.callbackDone(null, null, new WLEngine.WlException(paramWLFailResponse));
    }

    public void onSuccess(WLResponse paramWLResponse)
    {
      try
      {
        L.D("WL call success " + paramWLResponse);
        this.mResponse = parseResponse(paramWLResponse);
        if (this.mCallback != null)
          this.mCallback.callbackDone(this.mResponse, null, null);
        return;
      }
      catch (Exception localException)
      {
        do
          localException.printStackTrace();
        while (this.mCallback == null);
        this.mCallback.callbackDone(null, null, localException);
      }
    }

    protected T parseResponse(WLResponse paramWLResponse)
    {
      return Util.getGSONBuilderObj().create().fromJson(paramWLResponse.getResponseText(), this.mResponseType);
    }
  }

  class RemoteDisableChallengeHandler extends WLChallengeHandler
  {
    public RemoteDisableChallengeHandler(String paramContext, Context arg3)
    {
      super();
    }

    public void handleChallenge(JSONObject paramJSONObject)
    {
    }

    public void handleFailure(JSONObject paramJSONObject)
    {
      L.D(WLEngine.TAG, "WL handleFailure");
      try
      {
        WLEngine.access$002(WLEngine.this, true);
        String str1 = paramJSONObject.getString("message");
        L.D(WLEngine.TAG, "message :: " + str1);
        String str2 = paramJSONObject.getString("downloadLink");
        L.D(WLEngine.TAG, "downloadLink :: " + str2);
        final AppUpdateInfo localAppUpdateInfo = new AppUpdateInfo(str1, str2, WLEngine.this.mAppUpdateAvailable);
        WLEngine.this.mHandler.post(new Runnable()
        {
          public void run()
          {
            WLEngine.this.mBus.post(new NewVersionAvailableEvent(localAppUpdateInfo));
          }
        });
        UiUtil.saveVersionUpdateInfoInDisk(WLEngine.this.mContext, localAppUpdateInfo);
        return;
      }
      catch (Exception localException)
      {
        L.D(WLEngine.TAG, "Exception :: " + localException.getMessage());
        localException.printStackTrace();
      }
    }

    public void handleSuccess(JSONObject paramJSONObject)
    {
    }
  }

  public static class WlException extends Exception
  {
    private static final long serialVersionUID = -7755886740119850640L;
    private final WLFailResponse mWLFailResponse;

    public WlException()
    {
      this(null);
    }

    public WlException(WLFailResponse paramWLFailResponse)
    {
      this.mWLFailResponse = paramWLFailResponse;
    }

    public WLFailResponse getWLFailResponse()
    {
      return this.mWLFailResponse;
    }
  }
}

/* Location:           C:\Documents and Settings\Cesar Cabello Cea\Mis documentos\Downloads\apk-downloader\com.ikea.kompis-6_dex2jar.jar
 * Qualified Name:     com.ikea.shared.WLEngine
 * JD-Core Version:    0.6.2
 */