package com.nbusy.app;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentSender;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.Scopes;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.Scope;
import com.google.android.gms.plus.Plus;

public class LoginActivity extends Activity implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        View.OnClickListener {

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

    /* Request code used to invoke sign in user interactions. */
    private static final int RC_SIGN_IN = 0;

    /* Client used to interact with Google APIs. */
    private GoogleApiClient mGoogleApiClient;

    /* Is there a ConnectionResult resolution in progress? */
    private boolean mIsResolving = false;

    /* Should we automatically resolve ConnectionResults when possible? */
    private boolean mShouldResolve = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(Plus.API)
                .addScope(new Scope(Scopes.PROFILE))
                .addScope(new Scope(Scopes.EMAIL))
                .build();
        findViewById(R.id.sign_in_button).setOnClickListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        mGoogleApiClient.connect();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mGoogleApiClient.disconnect();
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        // Could not connect to Google Play Services.  The user needs to select an account,
        // grant permissions or resolve an error in order to sign in. Refer to the javadoc for
        // ConnectionResult to see possible error codes.
        Log.d(TAG, "onConnectionFailed:" + connectionResult);

        if (!mIsResolving && mShouldResolve) {
            if (connectionResult.hasResolution()) {
                try {
                    connectionResult.startResolutionForResult(this, RC_SIGN_IN);
                    mIsResolving = true;
                } catch (IntentSender.SendIntentException e) {
                    Log.e(TAG, "Could not resolve ConnectionResult.", e);
                    mIsResolving = false;
                    mGoogleApiClient.connect();
                }
            } else {
                // Could not resolve the connection result, show the user an
                // error dialog.
//                showErrorDialog(connectionResult);
            }
        } else {
            // Show the signed-out UI
//            showSignedOutUI();
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.sign_in_button) {
            onSignInClicked();
        }

//        if (v.getId() == R.id.sign_out_button) {
//            onSignOutClicked();
//        }
    }

    private void onSignInClicked() {
        // User clicked the sign-in button, so begin the sign-in process and automatically
        // attempt to resolve any errors that occur.
        mShouldResolve = true;
        mGoogleApiClient.connect();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d(TAG, "onActivityResult:" + requestCode + ":" + resultCode + ":" + data);

        if (requestCode == RC_SIGN_IN) {
            // If the error resolution was not successful we should not resolve further.
            if (resultCode != RESULT_OK) {
                mShouldResolve = false;
            }

            mIsResolving = false;
            mGoogleApiClient.connect();
        }
    }

    @Override
    public void onConnected(Bundle bundle) {
        // onConnected indicates that an account was selected on the device, that the selected
        // account has granted any requested permissions to our app and that we were able to
        // establish a service connection to Google Play services.
        Log.d(TAG, "onConnected:" + bundle);
        mShouldResolve = false;

        // Show the signed-in UI
//        showSignedInUI();

        // ToDo: get online only token
        // https://developers.google.com/android/guides/http-auth
        // https://developers.google.com/identity/protocols/CrossClientAuth
        // https://gist.github.com/ianbarber/9607551
        // https://developers.google.com/identity/sign-in/android/backend-auth
        // http://stackoverflow.com/questions/21389267/getting-google-one-time-authorization-code
    }

    @Override
    public void onConnectionSuspended(int i) {
        // The connection to Google Play services was lost. The GoogleApiClient will automatically
        // attempt to re-connect. Any UI elements that depend on connection to Google APIs should
        // be hidden or disabled until onConnected is called again.
        Log.w(TAG, "onConnectionSuspended:" + i);
    }
}

@@ -190,16 +190,45 @@
 ew.View;
+%0Aimport android.widget.Toast;
 %0A%0Aimport
@@ -2867,33 +2867,33 @@
              // 
-C
+c
 ould not resolve
@@ -2932,35 +2932,16 @@
  user an
-%0A                //
  error d
@@ -2949,12 +2949,9 @@
 alog
-.%0A//
+%0A
     
@@ -2966,40 +2966,102 @@
     
-showErrorDialog(connectionResult
+Toast.makeText(getApplicationContext(), %22Error: %22 + connectionResult, Toast.LENGTH_LONG).show(
 );%0A 
@@ -3124,26 +3124,24 @@
 gned-out UI%0A
-//
             
@@ -3144,23 +3144,86 @@
     
-showSignedOutUI
+Toast.makeText(getApplicationContext(), %22Signed out!%22, Toast.LENGTH_LONG).show
 ();%0A
@@ -3379,106 +3379,8 @@
    %7D
-%0A%0A//        if (v.getId() == R.id.sign_out_button) %7B%0A//            onSignOutClicked();%0A//        %7D
 %0A   
@@ -4599,70 +4599,8 @@
 e;%0A%0A
-        // Show the signed-in UI%0A//        showSignedInUI();%0A%0A
     
@@ -5005,16 +5005,139 @@
 ion-code
+%0A%0A        // Show the signed-in UI%0A        Toast.makeText(getApplicationContext(), %22Signed in!%22, Toast.LENGTH_LONG).show();
 %0A    %7D%0A%0A

add some toast dialogs around