package com.example.theodhor.fbaccountkit;

import android.content.Intent;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.facebook.accountkit.AccessToken;
import com.facebook.accountkit.Account;
import com.facebook.accountkit.AccountKit;
import com.facebook.accountkit.AccountKitCallback;
import com.facebook.accountkit.AccountKitError;
import com.facebook.accountkit.AccountKitLoginResult;
import com.facebook.accountkit.AccountPreferences;
import com.facebook.accountkit.PhoneNumber;
import com.facebook.accountkit.ui.AccountKitActivity;
import com.facebook.accountkit.ui.AccountKitConfiguration;
import com.facebook.accountkit.ui.LoginType;
import com.facebook.accountkit.ui.SkinManager;
import com.facebook.accountkit.ui.UIManager;

public class MainActivity extends AppCompatActivity {

    public static int APP_REQUEST_CODE = 99;
    public static final String TAG = "MainActivity";

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

        AccessToken accessToken = AccountKit.getCurrentAccessToken();

        if (accessToken != null) {
            //Handle Returning User
                    AccountKit.getCurrentAccount(new AccountKitCallback<Account>() {
                @Override
                public void onSuccess(final Account account) {
                    // Get Account Kit ID
                    String accountKitId = account.getId();

                    // Get phone number
                    PhoneNumber phoneNumber = account.getPhoneNumber();
                    String phoneNumberString = phoneNumber.toString();

                    // Get email
                    String email = account.getEmail();
                    Log.e(TAG,""+accountKitId);
                    Log.e(TAG,""+phoneNumberString);
                    Log.e(TAG,""+email);
                }

                @Override
                public void onError(final AccountKitError error) {
                    // Handle Error
                    Log.e(TAG,error.toString());
                }
            });
        } else {
            //Handle new or logged out user
            Log.e(TAG,"Logged Out");
        }
    }

    public void phoneLogin(@Nullable View view) {
        final Intent intent = new Intent(this, AccountKitActivity.class);
        AccountKitConfiguration.AccountKitConfigurationBuilder configurationBuilder = new AccountKitConfiguration.AccountKitConfigurationBuilder(
                LoginType.PHONE,AccountKitActivity.ResponseType.TOKEN); // or .ResponseType.TOKEN
        // ... perform additional configuration ...
        UIManager uiManager = new SkinManager(
                LoginType.PHONE,
                SkinManager.Skin.TRANSLUCENT,
                (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? getResources().getColor(R.color.colorPrimary,null):getResources().getColor(R.color.colorPrimary)),
                R.drawable.background,
                SkinManager.Tint.WHITE,
                0.55
        );
        configurationBuilder.setUIManager(uiManager);
        intent.putExtra(AccountKitActivity.ACCOUNT_KIT_ACTIVITY_CONFIGURATION,configurationBuilder.build());
        startActivityForResult(intent, APP_REQUEST_CODE);
    }

    public void emailLogin(@Nullable View view) {
        final Intent intent = new Intent(this, AccountKitActivity.class);
        AccountKitConfiguration.AccountKitConfigurationBuilder configurationBuilder = new AccountKitConfiguration.AccountKitConfigurationBuilder(
                LoginType.EMAIL,AccountKitActivity.ResponseType.TOKEN); // or .ResponseType.TOKEN
        // ... perform additional configuration ...
        UIManager uiManager = new SkinManager(
                LoginType.PHONE,
                SkinManager.Skin.CLASSIC,
                (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? getResources().getColor(R.color.colorPrimary,null):getResources().getColor(R.color.colorPrimary)),
                R.drawable.background,
                SkinManager.Tint.WHITE,
                0.55
        );
        configurationBuilder.setUIManager(uiManager);
        intent.putExtra(AccountKitActivity.ACCOUNT_KIT_ACTIVITY_CONFIGURATION,configurationBuilder.build());
        startActivityForResult(intent, APP_REQUEST_CODE);
    }

    public void logout(@Nullable View view){
        AccountKit.logOut();
        AccessToken accessToken = AccountKit.getCurrentAccessToken();
        if(accessToken!=null)
            Log.e(TAG,"Still Logged in...");
        else
            Toast.makeText(this,"Logged Out",Toast.LENGTH_SHORT).show();
    }

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

            AccountKit.getCurrentAccount(new AccountKitCallback<Account>() {
                @Override
                public void onSuccess(final Account account) {
                    // Get Account Kit ID
                    String accountKitId = account.getId();
                    Log.e(TAG, accountKitId);

                    if(account.getPhoneNumber()!=null) {
                        Log.e(TAG, accountKitId + "-" + account.getPhoneNumber().getCountryCode());
                        Log.e(TAG, accountKitId + "-" + account.getPhoneNumber().getPhoneNumber());

                        // Get phone number
                        PhoneNumber phoneNumber = account.getPhoneNumber();
                        String phoneNumberString = phoneNumber.toString();
                        Log.e(TAG, phoneNumberString);
                    }

                    if(account.getEmail()!=null)
                        Log.e(TAG,account.getEmail());


                }

                @Override
                public void onError(final AccountKitError error) {
                    // Handle Error
                    Log.e(TAG, error.toString());
                }
            });
        }
    }
}


@@ -1650,25 +1650,24 @@
 nt.getId();%0A
-%0A
             
@@ -1678,73 +1678,147 @@
     
-// Get phone number%0A                    PhoneNumber phoneNumber =
+Log.e(TAG, accountKitId);%0A%0A                    if(account.getPhoneNumber()!=null) %7B%0A                        Log.e(TAG, accountKitId + %22-%22 +
  acc
@@ -1830,32 +1830,50 @@
 getPhoneNumber()
+.getCountryCode())
 ;%0A              
@@ -1882,36 +1882,73 @@
     
-String phoneNumberString = p
+    Log.e(TAG, accountKitId + %22-%22 + account.getPhoneNumber().getP
 hone
@@ -1957,22 +1957,18 @@
 mber
-.toString
 ()
+)
 ;%0A%0A
+    
     
@@ -1990,21 +1990,28 @@
  // Get 
-email
+phone number
 %0A       
@@ -2027,20 +2027,35 @@
     
-String email
+    PhoneNumber phoneNumber
  = a
@@ -2064,21 +2064,27 @@
 ount.get
-Email
+PhoneNumber
 ();%0A    
@@ -2103,36 +2103,67 @@
     
-Log.e(TAG,%22%22+accountKitId);%0A
+    String phoneNumberString = phoneNumber.toString();%0A    
     
@@ -2188,19 +2188,17 @@
 g.e(TAG,
-%22%22+
+ 
 phoneNum
@@ -2233,26 +2233,112 @@
     
-Log.e(TAG,%22%22+e
+%7D%0A%0A                    if(account.getEmail()!=null)%0A                        Log.e(TAG,account.getE
 mail
+()
 );%0A 

Clean up