package com.fisheradelakin.bitdate;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import com.parse.LogInCallback;
import com.parse.ParseException;
import com.parse.ParseFacebookUtils;
import com.parse.ParseUser;

import java.util.ArrayList;
import java.util.List;

public class SignInActivity extends AppCompatActivity {

    private static final String TAG = "SignInActivity";

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

        Button loginButton = (Button) findViewById(R.id.login_button);
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<String> permissions = new ArrayList<String>();
                permissions.add("user_birthday");
                ParseFacebookUtils.logInWithReadPermissionsInBackground(SignInActivity.this, permissions, new LogInCallback() {
                    @Override
                    public void done(ParseUser parseUser, ParseException e) {
                        if (parseUser == null) {
                            Log.i(TAG, "User is null. Error creating user.");
                        } else if (parseUser.isNew()) {
                            Log.i(TAG, "User is new. Created new user.");
                        } else {
                            Log.i(TAG, "Logged in existing user.");
                        }
                    }
                });
            }
        });
    }

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_sign_in, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

@@ -274,16 +274,150 @@
 utton;%0A%0A
+import com.facebook.AccessToken;%0Aimport com.facebook.GraphRequest;%0Aimport com.facebook.GraphResponse;%0Aimport com.facebook.HttpMethod;%0A
 import c
@@ -440,16 +440,16 @@
 llback;%0A
-
 import c
@@ -537,16 +537,103 @@
 rseUser;
+%0Aimport com.parse.SaveCallback;%0A%0Aimport org.json.JSONArray;%0Aimport org.json.JSONObject;
 %0A%0Aimport
@@ -1765,32 +1765,79 @@
 ed new user.%22);%0A
+                            getFacebookInfo();%0A
                 
@@ -1913,32 +1913,70 @@
 isting user.%22);%0A
+                            finish();%0A
                 
@@ -2033,32 +2033,32 @@
 ;%0A            %7D%0A
-
         %7D);%0A    
@@ -2052,32 +2052,1117 @@
      %7D);%0A    %7D%0A%0A
+    private void getFacebookInfo() %7B%0A        Bundle parameters = new Bundle();%0A        parameters.putString(%22fields%22, %22picture, first_name%22);%0A        new GraphRequest(AccessToken.getCurrentAccessToken(), %22/me%22, parameters, HttpMethod.GET, new GraphRequest.Callback() %7B%0A            @Override%0A            public void onCompleted(GraphResponse graphResponse) %7B%0A                JSONObject user = graphResponse.getJSONObject();%0A                ParseUser currentUser = ParseUser.getCurrentUser();%0A                currentUser.put(%22firstName%22, user.optString(%22first_name%22));%0A                currentUser.put(%22pictureURL%22, user.optJSONObject(%22picture%22).optJSONObject(%22data%22).optString(%22url%22));%0A                currentUser.saveInBackground(new SaveCallback() %7B%0A                    @Override%0A                    public void done(ParseException e) %7B%0A                        if(e == null) %7B%0A                            Log.i(TAG, %22User saved%22);%0A                            finish();%0A                        %7D%0A                    %7D%0A                %7D);%0A            %7D%0A        %7D).executeAsync();%0A    %7D%0A%0A
     @Override%0A  

update user with facebook info