package com.groupr.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.widget.TextView;

import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
import com.groupr.controller.LoginController;
import com.groupr.util.GetUserEventsAsyncTask;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

public class LoginActivity extends Activity {

    private LoginController mLoginController;
    private CallbackManager mCallbackManager;

    private TextView mTitleView;
    private LoginButton mFacebookButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        setContentView(R.layout.activity_login);

        mLoginController = new LoginController(getApplicationContext());
        AccessToken accessToken = AccessToken.getCurrentAccessToken();

        if (accessToken != null) {
            onFacebookLoginSuccess(accessToken.getUserId());
        } else {
            mCallbackManager = CallbackManager.Factory.create();

            mTitleView = (TextView) findViewById(R.id.app_title);
            mFacebookButton = (LoginButton) findViewById(R.id.login_button);
            mFacebookButton.setReadPermissions(Arrays.asList("public_profile",
                    "user_location", "user_friends"));

            mTitleView.setText(getString(R.string.app_name).toUpperCase());
            showHashKey(this);
            // use AccessToken.getCurrentAccessToken() and Profile.getCurrentProfile()
            mFacebookButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    onFacebookLoginSuccess(loginResult.getAccessToken().getUserId());
                }

                @Override
                public void onCancel() {
                }

                @Override
                public void onError(FacebookException facebookException) {
                    mLoginController.onFacebookError(facebookException);
                }
            });
        }
    }

    private void onFacebookLoginSuccess(String userId) {
        mLoginController.onFacebookSuccess(userId);
        finish();
    }

    public static void showHashKey(Context context) {
        try {
            PackageInfo info = context.getPackageManager().getPackageInfo(
                    "com.groupr", PackageManager.GET_SIGNATURES); //Your package name here
            for (Signature signature : info.signatures) {
                MessageDigest md = MessageDigest.getInstance("SHA");
                md.update(signature.toByteArray());
                Log.v("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
            }
        } catch (PackageManager.NameNotFoundException e) {
        } catch (NoSuchAlgorithmException e) {
        }
    }

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