package org.tndata.officehours.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;

import org.tndata.officehours.R;
import org.tndata.officehours.databinding.ActivityLauncherBinding;


/**
 * Lets the user sign in.
 *
 * @author Ismael Alonso
 * @version 1.0.0
 */
public class LauncherActivity extends AppCompatActivity implements View.OnClickListener, GoogleApiClient.OnConnectionFailedListener{
    private static final int GOOGLE_SIGN_IN_RC = 5327;


    private ActivityLauncherBinding binding;

    private GoogleApiClient googleApiClient;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_launcher);

        //Set listeners
        binding.launcherGooogleSignIn.setOnClickListener(this);
        binding.launcherProceed.setOnClickListener(this);

        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build();

        googleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this, this)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();
    }

    @Override
    public void onClick(View view){
        if (view.getId() == R.id.launcher_gooogle_sign_in){
            signInWithGoogle();
        }
        else if (view.getId() == R.id.launcher_proceed){
            startActivity(new Intent(this, ScheduleActivity.class));
            finish();
        }
    }

    private void signInWithGoogle(){
        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient);
        startActivityForResult(signInIntent, GOOGLE_SIGN_IN_RC);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if (requestCode == GOOGLE_SIGN_IN_RC){
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            handleGoogleSignInResult(result);
        }
        else{
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    private void handleGoogleSignInResult(GoogleSignInResult result){
        if (result.isSuccess()){
            Log.d("LauncherFragment", "Sign in success");
            //Signed in successfully, show authenticated UI.
            GoogleSignInAccount acct = result.getSignInAccount();
            acct.
            //updateUI(true);
        }
        else{
            //Signed out, show unauthenticated UI.
            //updateUI(false);
        }
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult){
        binding.launcherGooogleSignIn.setEnabled(false);
    }
}

@@ -322,16 +322,45 @@
 ew.View;
+%0Aimport android.widget.Toast;
 %0A%0Aimport
@@ -402,75 +402,8 @@
 th;%0A
-import com.google.android.gms.auth.api.signin.GoogleSignInAccount;%0A
 impo
@@ -745,16 +745,58 @@
 inding;%0A
+import org.tndata.officehours.model.User;%0A
 %0A%0A/**%0A *
@@ -869,16 +869,16 @@
 0.0%0A */%0A
-
 public c
@@ -1002,16 +1002,75 @@
 stener%7B%0A
+    private static final String TAG = %22LauncherActivity%22;%0A%0A
     priv
@@ -3023,46 +3023,43 @@
 Log.
-d(%22LauncherFragment%22, %22Sign in
+i(TAG, %22Sign in with google
  success
 %22);%0A
@@ -3054,16 +3054,19 @@
  success
+ful
 %22);%0A    
@@ -3079,94 +3079,98 @@
   //
-Signed in successfully, show authenticated UI.%0A            GoogleSignInAccount acct = 
+TODO check if this account already exists in the backend%0A            User user = new User(
 resu
@@ -3190,16 +3190,17 @@
 ccount()
+)
 ;%0A      
@@ -3205,47 +3205,23 @@
     
-    acct.%0A            //updateUI(true);
+%7D%0A        else%7B
 %0A   
@@ -3229,103 +3229,168 @@
     
-%7D%0A
     
-    else%7B%0A            //Signed out, show unauthenticated UI.%0A            //updateUI(false
+//Why would this happen?%0A            Log.e(TAG, %22Sign in with Google failed%22);%0A            Toast.makeText(this, %22Couldn't sign in%22, Toast.LENGTH_LONG).show(
 );%0A 
@@ -3491,32 +3491,91 @@
 nectionResult)%7B%0A
+        Log.e(TAG, %22Couldn't connect to GoogleApiClient%22);%0A
         binding.

Add some logs and comments