package au.com.pactera.pacterapulse.helper;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;

import com.microsoft.aad.adal.AuthenticationCallback;
import com.microsoft.aad.adal.AuthenticationContext;
import com.microsoft.aad.adal.AuthenticationResult;
import com.microsoft.aad.adal.PromptBehavior;

import java.security.NoSuchAlgorithmException;

import javax.crypto.NoSuchPaddingException;

/**
 * Created by kai on 26/05/15.
 */
public class OfficeAuthenticationHelper
{

	// Office 365 resources
	private static final String RESOURCE_ID = "https://graph.windows.net/";
	private static final String CLIENT_ID = "b94cba4d-b100-4c56-8ae7-db8c1f3519aa";
	private static final String REDIRECT_URL = "ppos://pactera.com.au/oauth2";
	private static final String AUTHORITY_URL = "https://login.windows.net/common";

	private static final String TAG = "OfficeAuthHelper";

	private static AuthenticationContext mAuthContext;

	/**
	 * Acquire token from server, callback obj should be passed to get token
	 *
	 * @param activity
	 * @param callback
	 * @return
	 */
	public static AuthenticationContext acquireToken(Activity activity, AuthenticationCallback callback)
	{
		if (getAuthenticationContext(activity) == null)
		{
			return null;
		}
		mAuthContext.acquireToken(activity, RESOURCE_ID, CLIENT_ID,
				REDIRECT_URL, PromptBehavior.Auto, callback);
		return mAuthContext;
	}

	/**
	 * Get authentication context object for further operation
	 *
	 * @param context
	 * @return
	 */
	private static AuthenticationContext getAuthenticationContext(Context context)
	{
		if (mAuthContext != null)
		{
			return mAuthContext;
		}
		try
		{
			mAuthContext = new AuthenticationContext(context.getApplicationContext(),
					AUTHORITY_URL, true); //true = use SharedPreferences for cache
			return mAuthContext;
		}
		catch (NoSuchAlgorithmException e)
		{
			Log.d(TAG, e.toString());
		}
		catch (NoSuchPaddingException e)
		{
			Log.d(TAG, e.toString());
		}
		return null;
	}


	/**
	 * Logout from current context
	 *
	 * @param context
	 * @return
	 */
	public static boolean logout(Context context)
	{
		CookieManager cookieManager = CookieManager.getInstance();
		cookieManager.removeAllCookie();
		CookieSyncManager.getInstance().sync();
		if (getAuthenticationContext(context) != null)
		{
			mAuthContext.getCache().removeAll();
			return false;
		}
		return true;
	}

	public static boolean hasToken(Context context)
	{
		if (getAuthenticationContext(context) != null)
		{
			mAuthContext.acquireTokenSilentSync(RESOURCE_ID, CLIENT_ID,
					REDIRECT_URL);
			return false;
		}
		return true;
	}

	public static AuthenticationContext acquireTokenSilent(Context context, String uid, AuthenticationCallback<AuthenticationResult> callback)
	{
		if (getAuthenticationContext(context) == null)
		{
			return null;
		}
		mAuthContext.acquireTokenSilent(RESOURCE_ID, CLIENT_ID, uid, callback);
		return mAuthContext;
	}
}

@@ -1132,16 +1132,33 @@
 activity
+ activity context
 %0A%09 * @pa
@@ -1169,28 +1169,101 @@
 callback
-%0A%09 * @return
+ callback of token acquire%0A%09 * @return authenticationContext instance you need to use
 %0A%09 */%0A%09p
@@ -1657,32 +1657,40 @@
 * @param context
+ Context
 %0A%09 * @return%0A%09 *
@@ -1677,32 +1677,79 @@
 text%0A%09 * @return
+ authenticationContext instance you need to use
 %0A%09 */%0A%09private s
@@ -2285,24 +2285,32 @@
 aram context
+ Context
 %0A%09 * @return
@@ -2313,64 +2313,284 @@
 turn
-%0A%09 */%0A%09public static boolean logout(Context context)%0A%09%7B%0A
+ logout success or not%0A%09 */%0A%09@SuppressWarnings(%22deprecation%22)%0A%09public static boolean logout(Context context)%0A%09%7B%0A%09%09if (getAuthenticationContext(context) != null)%0A%09%09%7B%0A%09%09%09mAuthContext.getCache().removeAll();%0A%09%09%09CookieSyncManager.createInstance(context.getApplicationContext());%0A%09
 %09%09Co
@@ -2646,16 +2646,57 @@
 ance();%0A
+%09%09%09cookieManager.removeSessionCookie();%0A%09
 %09%09cookie
@@ -2722,16 +2722,17 @@
 okie();%0A
+%09
 %09%09Cookie
@@ -2771,44 +2771,34 @@
 ;%0A%09%09
-if (getAuthenticationContext
+%09Preference.setUID
 (context
 ) !=
@@ -2797,61 +2797,15 @@
 text
-) !=
+,
  null)
-%0A%09%09%7B%0A%09%09%09mAuthContext.getCache().removeAll()
 ;%0A%09%09

Clean cookie after user logout