package com.ikea.shared.user.service;

import android.content.Context;
import android.os.Handler;
import com.ikea.shared.BaseService;
import com.ikea.shared.StickyRequest;
import com.ikea.shared.StickyResponse;
import com.ikea.shared.WLEngine;
import com.ikea.shared.WLEngine.GenericInvokeRequestWlResponseListener;
import com.ikea.shared.WLEngine.WlException;
import com.ikea.shared.cart.ShoppingCart;
import com.ikea.shared.cart.model.SyncEventModel;
import com.ikea.shared.network.RequestUtil;
import com.ikea.shared.network.WLRequest;
import com.ikea.shared.shopping.service.ShoppingCartService;
import com.ikea.shared.user.event.LogoutDoneEvent;
import com.ikea.shared.user.model.AuthResponse;
import com.ikea.shared.user.model.User;
import com.ikea.shared.util.AppExecutors;
import com.ikea.shared.util.DataPersister;
import com.ikea.shared.util.L;
import com.ikea.shared.util.ServiceCallback;
import com.squareup.otto.Bus;
import com.worklight.wlclient.api.WLResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;

public final class UserService extends BaseService
{
  private static final String FOLDER_NAME = "userdata";
  private static UserService INSTANCE;
  private final DataPersister<User> mDataPersister;
  private final ExecutorService mExecutorService = AppExecutors.highPrio();
  private Random mRandomNo = new Random();
  private final List<StickyRequest> mRequestList = Collections.synchronizedList(new ArrayList());
  private final List<StickyResponse> mResponseList = Collections.synchronizedList(new ArrayList());
  private User mUser;

  static
  {
    if (!UserService.class.desiredAssertionStatus());
    for (boolean bool = true; ; bool = false)
    {
      $assertionsDisabled = bool;
      return;
    }
  }

  private UserService(Context paramContext)
  {
    super(paramContext);
    this.mDataPersister = new DataPersister(paramContext, "userdata", User.class, null);
    try
    {
      List localList = this.mDataPersister.load();
      if ((localList != null) && (!localList.isEmpty()))
        this.mUser = ((User)localList.get(0));
      return;
    }
    catch (FileNotFoundException localFileNotFoundException)
    {
      localFileNotFoundException.printStackTrace();
    }
  }

  private long getNextRequestID()
  {
    if (this.mRandomNo != null)
      return this.mRandomNo.nextLong();
    this.mRandomNo = new Random();
    return this.mRandomNo.nextLong();
  }

  private StickyRequest getPendingRequest(long paramLong)
  {
    if ((this.mRequestList != null) && (this.mRequestList.size() > 0))
    {
      Iterator localIterator = this.mRequestList.iterator();
      while (localIterator.hasNext())
      {
        StickyRequest localStickyRequest = (StickyRequest)localIterator.next();
        if (localStickyRequest.getRequestID() == paramLong)
          return localStickyRequest;
      }
    }
    return null;
  }

  private StickyResponse getResponse(long paramLong)
  {
    if ((this.mResponseList != null) && (this.mResponseList.size() > 0))
    {
      Iterator localIterator = this.mResponseList.iterator();
      while (localIterator.hasNext())
      {
        StickyResponse localStickyResponse = (StickyResponse)localIterator.next();
        if (localStickyResponse.getRequestID() == paramLong)
          return localStickyResponse;
      }
    }
    return null;
  }

  public static UserService i(Context paramContext)
  {
    try
    {
      if (INSTANCE == null)
        INSTANCE = new UserService(paramContext);
      UserService localUserService = INSTANCE;
      return localUserService;
    }
    finally
    {
    }
  }

  private void saveRequest(long paramLong, ServiceCallback<AuthResponse> paramServiceCallback)
  {
    if ((this.mRequestList != null) && (this.mRequestList.size() > 5))
      this.mRequestList.remove(0);
    int i = -1;
    assert (this.mRequestList != null);
    Iterator localIterator = this.mRequestList.iterator();
    while (localIterator.hasNext())
    {
      StickyRequest localStickyRequest = (StickyRequest)localIterator.next();
      if (localStickyRequest.getRequestID() == paramLong)
        i = this.mRequestList.indexOf(localStickyRequest);
    }
    if (i != -1)
      this.mRequestList.remove(i);
    this.mRequestList.add(new StickyRequest(paramLong, paramServiceCallback));
  }

  private void saveResponse(StickyResponse paramStickyResponse)
  {
    StickyRequest localStickyRequest = getPendingRequest(paramStickyResponse.getRequestID());
    if ((localStickyRequest != null) && (localStickyRequest.getCallback().isValid()))
    {
      localStickyRequest.getCallback().callbackDone(paramStickyResponse.getResult(), paramStickyResponse.getErrors(), paramStickyResponse.getException());
      return;
    }
    if (localStickyRequest != null)
      this.mRequestList.remove(localStickyRequest);
    if ((this.mResponseList != null) && (this.mResponseList.size() > 5))
      this.mResponseList.remove(0);
    assert (this.mResponseList != null);
    this.mResponseList.add(paramStickyResponse);
  }

  public void attacheRequest(long paramLong, ServiceCallback<AuthResponse> paramServiceCallback)
  {
    StickyResponse localStickyResponse = getResponse(paramLong);
    if (localStickyResponse != null)
    {
      paramServiceCallback.callbackDone(localStickyResponse.getResult(), localStickyResponse.getErrors(), localStickyResponse.getException());
      this.mResponseList.remove(localStickyResponse);
      return;
    }
    saveRequest(paramLong, paramServiceCallback);
  }

  public User getUser()
  {
    if (this.mUser == null)
      this.mUser = new User();
    return this.mUser;
  }

  public long loginAsyncWL(final String paramString1, final String paramString2, final ServiceCallback<AuthResponse> paramServiceCallback)
  {
    final long l = getNextRequestID();
    this.mExecutorService.execute(new Runnable()
    {
      public void run()
      {
        try
        {
          WLRequest localWLRequest = RequestUtil.getLoginRequestWL(UserService.this.mContext, paramString1, paramString2);
          WLEngine.GenericInvokeRequestWlResponseListener local1 = new WLEngine.GenericInvokeRequestWlResponseListener(paramServiceCallback, AuthResponse.class)
          {
            public void onSuccess(WLResponse paramAnonymous2WLResponse)
            {
              L.D("WL call success " + paramAnonymous2WLResponse);
              try
              {
                AuthResponse localAuthResponse = (AuthResponse)super.parseResponse(paramAnonymous2WLResponse);
                if (UserService.1.this.val$callback.isValid())
                {
                  UserService.1.this.val$callback.callbackDone(localAuthResponse, null, null);
                  return;
                }
                UserService.this.saveResponse(new StickyResponse(UserService.1.this.val$requestID, localAuthResponse, null, null));
                return;
              }
              catch (Exception localException)
              {
                localException.printStackTrace();
                if (UserService.1.this.val$callback.isValid())
                {
                  UserService.1.this.val$callback.callbackDone(null, null, localException);
                  return;
                }
                UserService.this.saveResponse(new StickyResponse(UserService.1.this.val$requestID, null, null, localException));
              }
            }
          };
          if (WLEngine.i(UserService.this.mContext).invoke(localWLRequest, local1))
            return;
          if (paramServiceCallback.isValid())
          {
            paramServiceCallback.callbackDone(null, null, new WLEngine.WlException());
            return;
          }
        }
        catch (Exception localException)
        {
          localException.printStackTrace();
          if (paramServiceCallback.isValid())
          {
            paramServiceCallback.callbackDone(null, null, localException);
            return;
            UserService.this.saveResponse(new StickyResponse(l, null, null, new WLEngine.WlException()));
            return;
          }
          UserService.this.saveResponse(new StickyResponse(l, null, null, localException));
        }
      }
    });
    return l;
  }

  public boolean logout()
    throws IOException
  {
    this.mDataPersister.reset();
    this.mUser.resetAuthData();
    this.mDataPersister.save(this.mUser);
    ShoppingCart.i(this.mContext).reset();
    ShoppingCartService.i(this.mContext).reset();
    ShoppingCartService.i(this.mContext).processSyncSL(new SyncEventModel("event_clear"));
    this.mHandler.post(new Runnable()
    {
      public void run()
      {
        UserService.this.mBus.post(new LogoutDoneEvent());
      }
    });
    return true;
  }

  public boolean logout(boolean paramBoolean)
    throws IOException
  {
    this.mDataPersister.reset();
    this.mUser.resetAuthData();
    this.mDataPersister.save(this.mUser);
    if (!paramBoolean)
      ShoppingCart.i(this.mContext).reset();
    this.mHandler.post(new Runnable()
    {
      public void run()
      {
        UserService.this.mBus.post(new LogoutDoneEvent());
      }
    });
    return true;
  }

  public void logoutAsync(final ServiceCallback<Boolean> paramServiceCallback)
  {
    this.mExecutorService.execute(new Runnable()
    {
      public void run()
      {
        try
        {
          boolean bool = UserService.this.logout();
          paramServiceCallback.callbackDone(Boolean.valueOf(bool), null, null);
          return;
        }
        catch (Exception localException)
        {
          localException.printStackTrace();
          paramServiceCallback.callbackDone(null, null, localException);
        }
      }
    });
  }

  public void saveIkeaFamilyNumber(String paramString)
    throws IOException
  {
    try
    {
      if (this.mUser == null)
        this.mUser = new User();
      this.mUser.setIkeaFamilyNumber(paramString);
      this.mDataPersister.reset();
      this.mDataPersister.save(this.mUser);
      return;
    }
    catch (IOException localIOException)
    {
      L.E(this, "exception saving ", localIOException);
      throw localIOException;
    }
  }

  public void saveLocalIkeaFamilyNumber(String paramString)
    throws IOException
  {
    try
    {
      if (this.mUser == null)
        this.mUser = new User();
      this.mUser.setLocalIkeaFamilyNumber(paramString);
      this.mDataPersister.reset();
      this.mDataPersister.save(this.mUser);
      return;
    }
    catch (IOException localIOException)
    {
      L.E(this, "exception saving ", localIOException);
      throw localIOException;
    }
  }

  public void saveUser(User paramUser)
    throws IOException
  {
    this.mUser = paramUser;
    this.mDataPersister.reset();
    this.mDataPersister.save(this.mUser);
  }
}

/* Location:           C:\Documents and Settings\Cesar Cabello Cea\Mis documentos\Downloads\apk-downloader\com.ikea.kompis-6_dex2jar.jar
 * Qualified Name:     com.ikea.shared.user.service.UserService
 * JD-Core Version:    0.6.2
 */