package com.example.shalantor.connect4;


import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;

import java.util.Arrays;

public class AccountFragment extends Fragment{

    public static final int SCREEN_TO_TEXT_SIZE_RATIO = 20;
    public static final String USER_TYPE = "USER_TYPE";
    private CallbackManager callbackManager;
    private Activity activity;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        activity = getActivity();
        FacebookSdk.sdkInitialize(activity.getApplicationContext());
        callbackManager = CallbackManager.Factory.create();

        getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        return inflater.inflate(R.layout.account_fragment, container, false);
    }

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

    }



    /*Method to adjust button/text size*/
    public void adjustButtons(){

        final Activity activity = getActivity();

        /*Get references to buttons and editText*/
        Button loginButton = (Button) activity.findViewById(R.id.login_button);
        Button registerButton = (Button) activity.findViewById(R.id.register_button);
        LoginButton fbButton = (LoginButton) activity.findViewById(R.id.login_fb_button);
        EditText address = (EditText) activity.findViewById(R.id.ip_address);

        /*Get screen dimensions*/
        Display display = activity.getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int displayHeight = size.y;

        /*set dimensions of components according to screen size*/
        loginButton.setTextSize(displayHeight / SCREEN_TO_TEXT_SIZE_RATIO);
        registerButton.setTextSize(displayHeight / SCREEN_TO_TEXT_SIZE_RATIO);
        fbButton.setTextSize(displayHeight / SCREEN_TO_TEXT_SIZE_RATIO);
        address.setTextSize(displayHeight / SCREEN_TO_TEXT_SIZE_RATIO);
        address.setSelected(false);
        address.clearFocus();

        /*Set permissions of login button for facebook*/
        fbButton.setReadPermissions(Arrays.asList("public_profile","email"));
        fbButton.setFragment(this);

        /*Register callback for fb button*/
        LoginManager.getInstance().registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        TextView textView = (TextView) activity.findViewById(R.id.error_messages);
                        String message = "LOGIN OK";
                        textView.setText(message, TextView.BufferType.NORMAL);
                    }

                    @Override
                    public void onCancel() {
                        TextView textView = (TextView) activity.findViewById(R.id.error_messages);
                        String message = "LOGIN NOT OK";
                        textView.setText(message, TextView.BufferType.NORMAL);
                    }

                    @Override
                    public void onError(FacebookException exception) {
                        TextView textView = (TextView) activity.findViewById(R.id.error_messages);
                        String message = "LOGIN ERROR";
                        textView.setText(message, TextView.BufferType.NORMAL);
                    }
                });

    }

}

@@ -228,24 +228,49 @@
 p.Fragment;%0A
+import android.util.Log;%0A
 import andro
@@ -474,16 +474,49 @@
 tView;%0A%0A
+import com.facebook.AccessToken;%0A
 import c
@@ -654,16 +654,128 @@
 ookSdk;%0A
+import com.facebook.GraphRequest;%0Aimport com.facebook.GraphRequestAsyncTask;%0Aimport com.facebook.GraphResponse;%0A
 import c
@@ -903,24 +903,101 @@
 ort 
-java.util.Arrays
+org.json.JSONObject;%0A%0Aimport java.util.Arrays;%0Aimport java.util.concurrent.ExecutionException
 ;%0A%0Ap
@@ -1234,16 +1234,102 @@
 ctivity;
+%0A    private String facebookId;%0A    private String username;%0A    private String email;
 %0A%0A    @O
@@ -1616,24 +1616,69 @@
 Context());%0A
+        LoginManager.getInstance().logOut();%0A
         call
@@ -2322,24 +2322,30 @@
 t*/%0A        
+final 
 Button login
@@ -3868,35 +3868,1136 @@
     
-String message = %22LOGIN OK%22
+AccessToken token = loginResult.getAccessToken();%0A%0A                        /*Asynchronous request task to get user data*/%0A                        GraphRequestAsyncTask request = GraphRequest.newMeRequest(token,new GraphRequest.GraphJSONObjectCallback() %7B%0A                            @Override%0A                            public void onCompleted(JSONObject user,GraphResponse graphResponse)%7B%0A                                facebookId = user.optString(%22id%22);%0A                                username = user.optString(%22name%22);%0A                                email = user.optString(%22email%22);%0A                            %7D%0A                        %7D).executeAsync();%0A%0A                        try%7B%0A                            request.get();%0A                        %7D%0A                        catch (InterruptedException ex)%7B%0A                            Log.d(%22EX%22,%22Interrupted%22);%0A                        %7D%0A                        catch (ExecutionException ex)%7B%0A                            Log.d(%22EX2%22,%22Execute exception%22);%0A                        %7D%0A%0A                        String message = %22LOGIN OK %22 + facebookId + username + email
 ;%0A  
@@ -5322,14 +5322,17 @@
 GIN 
-NOT OK
+Cancelled
 %22;%0A 
@@ -5684,13 +5684,13 @@
 GIN 
-ERROR
+error
 %22;%0A 

Add code to get user name and id, still doesnt get email