package com.parseapp.vthokiefinder;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.widget.Toast;

import com.facebook.AccessToken;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.parse.ParseException;
import com.parse.ParseFacebookUtils;
import com.parse.ParseUser;
import com.parse.SaveCallback;
import com.parse.ui.ParseLoginBuilder;

import org.json.JSONObject;

/**
 * Activity that allows users to login to the application
 *
 * @author Steven Briggs
 * @version 2015.10.18
 */
public class LoginActivity extends AppCompatActivity implements GraphRequest.GraphJSONObjectCallback {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        setSupportActionBar((Toolbar) findViewById(R.id.toolbar));

        // Skip login procedures if a current user already exists
        if (ParseUser.getCurrentUser() != null) {
            startActivity(new Intent(this, HomeActivity.class));
            finish();
        }

        // Start login procedures using the ParseUI library
        else {
            ParseLoginBuilder builder = new ParseLoginBuilder(this);
            startActivityForResult(builder.build(), 0);
        }
    }

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

        // If the user successfully logged in, then transition to main screen
        if (resultCode == RESULT_OK) {
            handleNewFacebookUser();
        }
    }

    /**
     * Handle the case when a new user signs up through their Facebook account
     */
    private void handleNewFacebookUser() {
        // Determine if the current user just signed up through Facebook
        if (ParseUser.getCurrentUser().isNew() && ParseFacebookUtils.isLinked(ParseUser.getCurrentUser())) {
            GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), this);
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name,email");
            request.setParameters(parameters);
            request.executeAsync();
        }
    }

    @Override
    public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
        // Configure the user's account to have their username and email correspond to their
        // Facebook name and email, respectively
        ParseUser.getCurrentUser().setUsername(jsonObject.optString("name"));

        if (jsonObject.has("email")) {
            ParseUser.getCurrentUser().setEmail(jsonObject.optString("email"));
        }

        if (jsonObject.has("id")) {
            ParseUser.getCurrentUser().put("facebookId", jsonObject.optString("id"));
        }

        ParseUser.getCurrentUser().saveInBackground(new SaveCallback() {
            @Override
            public void done(ParseException e) {
                if (e == null) {
                    startActivity(new Intent(LoginActivity.this, HomeActivity.class));
                    finish();
                }

                else {
                    Toast.makeText(LoginActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}

@@ -1668,24 +1668,25 @@
 ESULT_OK) %7B%0A
+%0A
             
@@ -1685,33 +1685,68 @@
         
-h
+// H
 andle
-New
+ users that just signed up through 
 Facebook
 User();%0A
@@ -1741,15 +1741,8 @@
 book
-User();
 %0A   
@@ -1750,128 +1750,128 @@
     
-%7D%0A
     
-%7D%0A%0A    /**%0A     * Handle the case when a new user signs up through their Facebook account%0A
+if (ParseUser.getCurrentUser().isNew() && ParseFacebookUtils.isLinked(ParseUser.getCurrentUser())) %7B%0A    
      
-*/%0A
     
-private void
+  
  han
@@ -1894,63 +1894,188 @@
 er()
- %7B%0A        // Determine if the current
+;%0A            %7D%0A%0A            // Handle normal users%0A            else %7B%0A                finish();%0A            %7D%0A        %7D%0A    %7D%0A%0A    /**%0A     * Handle the case when a new
  user 
-just 
 sign
-ed
+s
  up 
@@ -2078,32 +2078,38 @@
  up through 
+their 
 Facebook
 %0A        if 
@@ -2100,122 +2100,68 @@
 book
-%0A        if (ParseUser.getCurrentUser().isNew() && ParseFacebookUtils.isLinked(ParseUser.getCurrent
+ account%0A     */%0A    private void handleNewFacebook
 User()
-))
  %7B%0A
-    
     
@@ -2265,20 +2265,16 @@
         
-   
- 
 Bundle p
@@ -2295,28 +2295,24 @@
 w Bundle();%0A
-    
         para
@@ -2356,36 +2356,32 @@
 mail%22);%0A        
-   
- 
 request.setParam
@@ -2403,28 +2403,24 @@
 s);%0A        
-    
 request.exec
@@ -2431,26 +2431,16 @@
 sync();%0A
-        %7D%0A
     %7D%0A%0A 

Fix bug where normal users were not redirected to HomeActivity upon successful sign in