package com.quickblox.sample.groupchatwebrtc.util;

import android.os.Bundle;
import android.util.Log;

import com.quickblox.auth.QBAuth;
import com.quickblox.auth.model.QBSession;
import com.quickblox.core.QBEntityCallback;
import com.quickblox.core.exception.QBResponseException;
import com.quickblox.core.request.QBPagedRequestBuilder;
import com.quickblox.sample.core.utils.SharedPrefsHelper;
import com.quickblox.sample.groupchatwebrtc.utils.QBEntityCallbackImpl;
import com.quickblox.sample.groupchatwebrtc.utils.TokenUtils;
import com.quickblox.users.QBUsers;
import com.quickblox.users.model.QBUser;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by tereha on 26.04.16.
 */
public class QBResRequestExecutor {
    private String TAG = QBResRequestExecutor.class.getSimpleName();

    public void createSession(QBEntityCallback<QBSession> callback) {
        QBAuth.createSession(callback);
    }

    public void createSessionWithUser(final QBUser qbUser, final QBEntityCallback<QBSession> callback) {
        QBAuth.createSession(qbUser, callback);
    }

    public void signUpNewUser(QBUser newQbUser, QBEntityCallback<QBUser> callback) {
        QBUsers.signUp(newQbUser, callback);
    }

    public void signInUser(final QBUser currentQbUser, final QBEntityCallback<QBUser> callback) {
        QBUsers.signIn(currentQbUser, callback);
    }

    public void deleteCurrentUser(int currentQbUserID, QBEntityCallback<Void> callback) {
        QBUsers.deleteUser(currentQbUserID, callback);
    }

    public void loadUsersByTag(final String tag, final QBEntityCallback<ArrayList<QBUser>> callback) {
        restoreOrCreateSession(new QBEntityCallbackImpl<QBSession>() {
            @Override
            public void onSuccess(QBSession result, Bundle params) {
                QBPagedRequestBuilder requestBuilder = new QBPagedRequestBuilder();
                List<String> tags = new LinkedList<>();
                tags.add(tag);

                QBUsers.getUsersByTags(tags, requestBuilder, callback);
            }
        });
    }

    public void loadUsersByIds(final Collection<Integer> usersIDs, final QBEntityCallback<ArrayList<QBUser>> callback) {
        restoreOrCreateSession(new QBEntityCallbackImpl<QBSession>() {
            @Override
            public void onSuccess(QBSession result, Bundle params) {
                QBUsers.getUsersByIDs(usersIDs, null, callback);
            }
        });
    }

    private void restoreOrCreateSession(final QBEntityCallbackImpl<QBSession> creatingSessionCallback) {
        if (TokenUtils.isTokenValid()) {
            if (TokenUtils.restoreExistentQbSessionWithResult()) {
                creatingSessionCallback.onSuccess(null, null);
            } else {
                creatingSessionCallback.onError(null);
            }
        } else if (SharedPrefsHelper.getInstance().hasQbUser()) {
            createSessionWithSavedUser(creatingSessionCallback);
        } else {
            createSessionWithoutUser(creatingSessionCallback);
        }
    }

    private void createSessionWithSavedUser(final QBEntityCallback<QBSession> creatingSessionCallback) {
        createSessionWithUser(SharedPrefsHelper.getInstance().getQbUser(), new QBEntityCallback<QBSession>() {
            @Override
            public void onSuccess(QBSession result, Bundle params) {
                TokenUtils.saveTokenData();
                creatingSessionCallback.onSuccess(result, params);
            }

            @Override
            public void onError(QBResponseException responseException) {
                creatingSessionCallback.onError(responseException);
                Log.d(TAG, "Error creating session with user");
            }
        });
    }

    private void createSessionWithoutUser(final QBEntityCallback<QBSession> creatingSessionCallback) {
        createSession(new QBEntityCallback<QBSession>() {
            @Override
            public void onSuccess(QBSession result, Bundle params) {
                creatingSessionCallback.onSuccess(result, params);
            }

            @Override
            public void onError(QBResponseException responseException) {
                creatingSessionCallback.onError(responseException);
                Log.d(TAG, "Error creating session");
            }
        });
    }
}
@@ -1167,16 +1167,22 @@
 NewUser(
+final 
 QBUser n
@@ -1190,16 +1190,22 @@
 wQbUser,
+ final
  QBEntit
@@ -1246,42 +1246,370 @@
     
-QBUsers.signUp(newQbUser, callback
+createSessionWithoutUser(new QBEntityCallback%3CQBSession%3E() %7B%0A            @Override%0A            public void onSuccess(QBSession qbSession, Bundle bundle) %7B%0A                QBUsers.signUp(newQbUser, callback);%0A            %7D%0A%0A            @Override%0A            public void onError(QBResponseException e) %7B%0A                callback.onError(e);%0A            %7D%0A        %7D
 );%0A 

add createSessionWithoutUser when signUpUser bug 231