package net.squanchy.signin;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.widget.Toast;

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 net.squanchy.R;
import net.squanchy.fonts.TypefaceStyleableActivity;

import io.reactivex.android.schedulers.AndroidSchedulers;

public class SignInActivity extends TypefaceStyleableActivity implements GoogleApiClient.OnConnectionFailedListener {

    private static final int RC_SIGN_IN = 9001;

    private SignInService service;

    private GoogleApiClient googleApiClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signin);

        // Button listeners
        findViewById(R.id.sign_in_button).setOnClickListener(view -> signIn());

        // Configure Google Sign In
        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 /* FragmentActivity */, this /* OnConnectionFailedListener */)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();

        SignInComponent component = SignInInjector.obtain(this);
        service = component.service();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            if (result.isSuccess()) {
                // Google Sign In was successful, authenticate with Firebase
                GoogleSignInAccount account = result.getSignInAccount();
                firebaseAuthWithGoogle(account);
            } else {
                // Google Sign In failed, update UI appropriately
                showSignInFailedError();
            }
        }
    }

    private void firebaseAuthWithGoogle(GoogleSignInAccount account) {
        showProgressDialog();

        service.signInWithGoogle(account)
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(this::finish);
    }

    private void showProgressDialog() {
        // TODO
    }

    private void hideProgressDialog() {
        // TODO
    }

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

    private void showSignInFailedError() {
        hideProgressDialog();
        Toast.makeText(this, "Google Play Services error.", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        showSignInFailedError();
    }
}

@@ -84,51 +84,8 @@
 le;%0A
-import android.support.annotation.NonNull;%0A
 impo
@@ -359,63 +359,8 @@
 lt;%0A
-import com.google.android.gms.common.ConnectionResult;%0A
 impo
@@ -616,62 +616,8 @@
 ity 
-implements GoogleApiClient.OnConnectionFailedListener 
 %7B%0A%0A 
@@ -916,36 +916,8 @@
 );%0A%0A
-        // Button listeners%0A
     
@@ -1005,35 +1005,212 @@
     
-// Configure Google Sign In
+googleApiClient = connectToGoogleApis();%0A%0A        SignInComponent component = SignInInjector.obtain(this);%0A        service = component.service();%0A    %7D%0A%0A    private GoogleApiClient connectToGoogleApis() %7B
 %0A   
@@ -1234,19 +1234,29 @@
 Options 
-gso
+signInOptions
  = new G
@@ -1461,33 +1461,22 @@
         
-googleApiClient =
+return
  new Goo
@@ -1544,70 +1544,38 @@
 this
- /* FragmentActivity */, this /* OnConnectionFailedListener */
+, e -%3E showSignInFailedError()
 )%0A  
@@ -1625,11 +1625,21 @@
 PI, 
-gso
+signInOptions
 )%0A  
@@ -1665,113 +1665,8 @@
 d();
-%0A%0A        SignInComponent component = SignInInjector.obtain(this);%0A        service = component.service();
 %0A   
@@ -1832,104 +1832,8 @@
 );%0A%0A
-        // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);%0A
     
@@ -2005,85 +2005,8 @@
 ) %7B%0A
-                // Google Sign In was successful, authenticate with Firebase%0A
     
@@ -2148,74 +2148,8 @@
 e %7B%0A
-                // Google Sign In failed, update UI appropriately%0A
     
@@ -2462,32 +2462,200 @@
 finish);%0A    %7D%0A%0A
+    private void showSignInFailedError() %7B%0A        hideProgressDialog();%0A        Toast.makeText(this, %22Google Play Services error.%22, Toast.LENGTH_SHORT).show();%0A    %7D%0A%0A
     private void
@@ -2685,32 +2685,45 @@
 %0A        // TODO
+ implement UI
 %0A    %7D%0A%0A    priv
@@ -2769,16 +2769,29 @@
  // TODO
+ implement UI
 %0A    %7D%0A%0A
@@ -2970,310 +2970,7 @@
    %7D
-%0A%0A    private void showSignInFailedError() %7B%0A        hideProgressDialog();%0A        Toast.makeText(this, %22Google Play Services error.%22, Toast.LENGTH_SHORT).show();%0A    %7D%0A%0A    @Override%0A    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) %7B%0A        showSignInFailedError();%0A    %7D
 %0A%7D%0A

Tidy up the remains of the sample code