package com.quickblox.sample.chat.ui.activity;

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

import com.quickblox.auth.session.QBSessionManager;
import com.quickblox.core.QBEntityCallback;
import com.quickblox.core.exception.QBResponseException;
import com.quickblox.sample.chat.App;
import com.quickblox.sample.chat.R;
import com.quickblox.sample.chat.utils.Consts;
import com.quickblox.sample.chat.utils.chat.ChatHelper;
import com.quickblox.sample.core.ui.activity.CoreSplashActivity;
import com.quickblox.sample.core.ui.dialog.ProgressDialogFragment;
import com.quickblox.sample.core.utils.SharedPrefsHelper;
import com.quickblox.users.model.QBUser;

public class SplashActivity extends CoreSplashActivity {

    private static final String TAG = SplashActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (checkConfigsWithSnackebarError()){
            proceedToTheNextActivityWithDelay();
        }
    }

    @Override
    protected String getAppName() {
        return getString(R.string.splash_app_title);
    }

    @Override
    protected void proceedToTheNextActivity() {
        if (checkSignIn()) {
            restoreChatSession();
        } else {
            LoginActivity.start(this);
            finish();
        }
    }

    @Override
    protected boolean sampleConfigIsCorrect() {
        boolean result = super.sampleConfigIsCorrect();
        result = result && App.getSampleConfigs() != null;
        return result;
    }

    private void restoreChatSession(){
        if (ChatHelper.getInstance().isLogged()) {
            DialogsActivity.start(this);
            finish();
        } else {
            QBUser currentUser = getUserFromSession();
            loginToChat(currentUser);
        }
    }

    private QBUser getUserFromSession(){
        QBUser user = SharedPrefsHelper.getInstance().getQbUser();
        user.setId(QBSessionManager.getInstance().getSessionParameters().getUserId());
        return user;
    }

    @Override
    protected boolean checkSignIn() {
        return SharedPrefsHelper.getInstance().hasQbUser();
    }

    private void loginToChat(final QBUser user) {
        ProgressDialogFragment.show(getSupportFragmentManager(), R.string.dlg_restoring_chat_session);

        ChatHelper.getInstance().loginToChat(user, new QBEntityCallback<Void>() {
            @Override
            public void onSuccess(Void result, Bundle bundle) {
                Log.v(TAG, "Chat login onSuccess()");

                ProgressDialogFragment.hide(getSupportFragmentManager());
                DialogsActivity.start(SplashActivity.this);
                finish();
            }

            @Override
            public void onError(QBResponseException e) {
                ProgressDialogFragment.hide(getSupportFragmentManager());
                Log.w(TAG, "Chat login onError(): " + e);
                showSnackbarError( findViewById(R.id.layout_root), R.string.error_recreate_session, e,
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                loginToChat(user);
                            }
                        });
            }
        });
    }
}
@@ -350,55 +350,8 @@
 .R;%0A
-import com.quickblox.sample.chat.utils.Consts;%0A
 impo
@@ -930,16 +930,17 @@
 Error())
+ 
 %7B%0A      
@@ -1565,32 +1565,33 @@
 oreChatSession()
+ 
 %7B%0A        if (Ch
@@ -1754,32 +1754,139 @@
 rFromSession();%0A
+            if (currentUser == null) %7B%0A                LoginActivity.start(this);%0A            %7D else %7B%0A    
             logi
@@ -1903,24 +1903,38 @@
 rrentUser);%0A
+            %7D%0A
         %7D%0A  
@@ -1977,16 +1977,17 @@
 ession()
+ 
 %7B%0A      
@@ -2059,49 +2059,263 @@
     
-user.setId(QBSessionManager.getInstance()
+QBSessionManager qbSessionManager = QBSessionManager.getInstance();%0A        if (qbSessionManager.getSessionParameters() == null) %7B%0A            ChatHelper.getInstance().destroy();%0A            return null;%0A        %7D%0A        Integer userId = qbSessionManager
 .get
@@ -2345,16 +2345,43 @@
 UserId()
+;%0A        user.setId(userId
 );%0A     
@@ -3319,17 +3319,16 @@
 arError(
- 
 findView

fix crash in the SplashActivity when user uninstall the application without logout session