package com.nbusy.app.worker;

import android.util.Log;

import com.nbusy.app.data.Chat;
import com.nbusy.app.data.DB;
import com.nbusy.app.data.UserProfile;
import com.nbusy.app.data.callbacks.CreateProfileCallback;
import com.nbusy.app.data.callbacks.UpsertChatsCallback;
import com.nbusy.sdk.Client;

import java.util.ArrayList;
import java.util.Date;

import titan.client.callbacks.ConnCallbacks;
import titan.client.callbacks.GoogleAuthCallback;
import titan.client.messages.MsgMessage;
import titan.client.responses.GoogleAuthResponse;

/**
 * Handles first time login/registration using Google auth.
 */
public class GoogleAuthManager implements ConnCallbacks {

    public static final int LOGIN_OK = 9000;
    private static final String TAG = GoogleAuthManager.class.getSimpleName();
    private final Client client;
    private final UserProfileManager profileManager;

    private String googleIDToken;
    private AuthFinishedCallback cb;

    public GoogleAuthManager(Client client, UserProfileManager profileManager) {
        if (client == null) {
            throw new IllegalArgumentException("client cannot be null or empty");
        }
        if (profileManager == null) {
            throw new IllegalArgumentException("profileManager cannot be null or empty");
        }

        this.client = client;
        this.profileManager = profileManager;
    }

    public void login(String googleIDToken, AuthFinishedCallback cb) {
        if (googleIDToken == null || googleIDToken.isEmpty()) {
            throw new IllegalArgumentException("googleIDToken cannot be null or empty");
        }
        if (cb == null) {
            throw new IllegalArgumentException("callback cannot be null");
        }

        this.googleIDToken = googleIDToken;
        this.cb = cb;
        client.connect(this);
    }

    public interface AuthFinishedCallback {
        void success();

        void error();
    }

    /***************************
     * ConnCallbacks Overrides *
     ***************************/

    @Override
    public void messagesReceived(MsgMessage... msgs) {
    }

    @Override
    public void connected(String reason) {
        boolean requestSent = client.googleAuth(googleIDToken, new GoogleAuthCallback() {
            @Override
            public void success(GoogleAuthResponse res) {
                Log.i(TAG, "Authenticated with NBusy server using Google auth.");
                UserProfile profile = new UserProfile(res.id, res.token, res.email, res.name, res.picture);
                profileManager.createUserProfile(profile, new UserProfileManager.CreateUserProfileCallback() {
                    @Override
                    public void success() {
                        client.close();
                        cb.success();
                    }

                    @Override
                    public void error() {
                        cb.error();
                    }
                });
            }

            @Override
            public void fail(int code, String message) {
                Log.i(TAG, "Failed to authenticate with NBusy server using Google auth: " + code + " : " + message);
            }
        });

        if (!requestSent) {
            client.close();
        }
    }

    @Override
    public void disconnected(String reason) {
        cb.error();
    }
}

@@ -43,178 +43,71 @@
 oid.
-util.Log;%0A%0Aimport com.nbusy.app.data.Chat;%0Aimport com.nbusy.app.data.DB;%0Aimport com.nbusy.app.data.UserProfile;%0Aimport com.nbusy.app.data.callbacks.CreateProfileCallback;
+os.Handler;%0Aimport android.os.Looper;%0Aimport android.util.Log;%0A
 %0Aimp
@@ -133,37 +133,19 @@
 ata.
-callbacks.
 U
-p
 ser
-tChatsCallback
+Profile
 ;%0Aim
@@ -176,60 +176,8 @@
 t;%0A%0A
-import java.util.ArrayList;%0Aimport java.util.Date;%0A%0A
 impo
@@ -606,24 +606,129 @@
 mpleName();%0A
+    private final Handler handler = new Handler(Looper.getMainLooper()); // to run callback on UI thread%0A
     private 
@@ -872,16 +872,290 @@
 back cb;
+%0A    private Runnable cbSuccess = new Runnable() %7B%0A        @Override%0A        public void run() %7B%0A            cb.success();%0A        %7D%0A    %7D;%0A    private Runnable cbError = new Runnable() %7B%0A        @Override%0A        public void run() %7B%0A            cb.error();%0A        %7D%0A    %7D;
 %0A%0A    pu
@@ -2959,35 +2959,46 @@
             
-cb.s
+handler.post(cbS
 uccess
-(
 );%0A         
@@ -3103,33 +3103,44 @@
             
-cb.e
+handler.post(cbE
 rror
-(
 );%0A         
@@ -3547,32 +3547,43 @@
 ) %7B%0A        
-cb.e
+handler.post(cbE
 rror
-(
 );%0A    %7D%0A%7D%0A

call callbacks on UI thread