﻿var FacebookClient = function (app_id)
{
    var app_id = app_id;
    var access_token = "";
    var user_id = "";

    init = function ()
    {
		// console.log("FB.init");
        FB.init(
        {
            appId: app_id,
            // status: true,
            cookie: true,
			oauth: true
        });
		// console.log("After FB.init");
    },

	// Performs authentication.
	// - redirect_url_path - the path to redirect to after authentication
	// - app_perms - a string containing the Facebook permissions to request
	// - response_callback - a function to call upon successful authentication. Is passed the authenticated user's id.
    authenticate = function (redirect_url_path, app_perms, response_callback)
    {
		// console.log("authenticating with permissions: " + app_perms);
		FB.getLoginStatus(function(response) {
			if(response.authResponse) { // already authenticated
				user_id = response.authResponse.userID;
                access_token = response.authResponse.accessToken;
                if (getPagePath().toLowerCase() != redirect_url_path.toLowerCase()) {
                    location.href = getDomain() + redirect_url_path;
                }
                else {
                    response_callback(user_id);
                }
			}
			else {
		        FB.login(function (response) {
					// console.log("FB.login response");
					// console.log(response);
		            if (response.authResponse) {
		                user_id = response.authResponse.userID;
		                access_token = response.authResponse.accessToken;
		                if (getPagePath().toLowerCase() != redirect_url_path.toLowerCase()) {
		                    location.href = getDomain() + redirect_url_path;
		                }
		                else {
		                    response_callback(user_id);
		                }
		            }
		            else {
						// console.log("User cancelled login or did not fully authorize.");
		            }
		        }, { scope: app_perms });
			}
		});
    },

	// Performs login without first checking if the user is already logged in.
	// - redirect_url_path - the path to redirect to after authentication
	// - app_perms - a string containing the Facebook permissions to request
	// - response_callback - a function to call upon successful authentication. Is passed the authenticated user's id.
    login = function (redirect_url_path, app_perms, response_callback)
	{
	    FB.login(function (response) {
			// console.log("FB.login response");
			// console.log(response);
	        if (response.authResponse) {
	            user_id = response.authResponse.userID;
	            access_token = response.authResponse.accessToken;
	            if (getPagePath().toLowerCase() != redirect_url_path.toLowerCase()) {
	                location.href = getDomain() + redirect_url_path;
	            }
	            else {
	                response_callback(user_id);
	            }
	        }
	        else {
				// console.log("User cancelled login or did not fully authorize.");
	        }
	    }, { scope: app_perms });
	},
	
	// Checks whether or not the user is logged in and calls either the
	// specified logged_in_callback function with the user_id if they are
	// logged in or the not_logged_in_callback function otherwise.
	checkLoginStatus = function(logged_in_callback, not_logged_in_callback){
		FB.getLoginStatus(function(response) {
			if(response.authResponse) { // already authenticated
				user_id = response.authResponse.userID;
				logged_in_callback(user_id);
			}
			else {
		        not_logged_in_callback();
			}
		});
	},
	
	// Queries the Facebook API.
	// - q - the query to perform
	// - response_callback - the function to call with the response data. Is passed the response data returned by Facebook.
    query = function (q, response_callback)
    {
		// console.log("FacebookClient query: " + q);
        FB.api({ method: 'fql.query', query: q }, function (response)
        {
            response_callback(response);
        });
    },

	// Queries the Facebook API using multiquery.
	// - q - a dictionary of the queries to perform
	// - response_callback - the function to call with the response data. Is passed the response data returned by Facebook.
    multiquery = function (q, response_callback)
    {
		// console.log("FacebookClient multiquery: " + q);
        FB.api({ method: 'fql.multiquery', queries: q }, function (response)
        {
            response_callback(response);
        });
    }

    return { init: init, authenticate: authenticate, query: query, multiquery: multiquery, checkLoginStatus: checkLoginStatus, login: login };
};
