<?php

/**
 * Singleton object for accessing information about (the current user's) facebook session
 */
class vB5_Facebook
{
	/**
	 * Singleton instance
	 * @var	vB5_Facebook
	 */
	protected static $instance = null;

	/**
	 * Singleton instance getter
	 *
	 * @return	vB5_User
	 */
	public static function instance()
	{
		if (self::$instance === null)
		{
			$class = __CLASS__;
			self::$instance = new $class;
		}

		return self::$instance;
	}

	public function loadFacebook($dofbredirect)
	{
		$api = Api_InterfaceAbstract::instance();
		$isEnabled = $api->callApi('facebook', 'isFacebookEnabled');

		// check if facebook and session is enabled
		if ($isEnabled)
		{
			$userIsLoggedIn = $api->callApi('facebook', 'userIsLoggedIn');

			// is user is logged into facebook?
			if ($userIsLoggedIn)
			{
//echo "Logged into facebook<br />\n";

				$user = vB5_User::instance();
				$inRegistrationRoute = (vB5_Frontend_Application::instance()->getRouter()->getRouteGuid() == 'vbulletin-4ecbdacd6a6f13.66635711');

				// is user logged into vB?
				if (!empty($user['userid']))
				{
//echo "Logged into vB<br />\n";
					$loggedInFbUserId = $api->callApi('facebook', 'getLoggedInFbUserId');

					// if vb user is not associated with the current facebook account (or no facebook account at all),
					// redirect to the register association page, if doing facebook redirect
					if ($user['fbuserid'] != $loggedInFbUserId)
					{
						if ($dofbredirect) // do_facebook_redirect()
						{
							header('Location: ' . vB5_Config::instance()->baseurl . '/register');
							exit;
						}

						// if not doing facebook redirect and not on the reg page,
						// pretend the user is not logged into facebook at all so user can browse
						else if (!$inRegistrationRoute)
						{
							$show['facebookuser'] = false;
							if ($dofbredirect)
							{
								standard_error(fetch_error('facebook_connect_fail'));
							}
						}
					}
				}

				// user is not logged into vb, but logged into facebook
				else
				{
//echo "Not logged into vB<br />\n";
					// check if there is an associated vb account, if so attempt to log that user in
					if ($vbUserId = $api->callApi('facebook', 'getVbUseridFromFbUserid'))
					{
//echo "FB acct connected to a vB acct<br />\n";
						// make sure user is trying to login
						if ($dofbredirect) // do_facebook_redirect()
						{
							$uname = $api->callApi('user', 'fetchUserName', array($vbUserId));
							$loginInfo = $api->callApi('user', 'login', array($uname, '', '', '', 'fbauto'));

							vB5_Auth::setLoginCookies($loginInfo);
							vB5_Auth::doLoginRedirect();
						}
						// if user is not trying to login with FB connect,
						// pretend like the user is not logged in to FB
						else if (!$inRegistrationRoute)
						{
							$show['facebookuser'] = false;
							if ($dofbredirect)
							{
								standard_error(fetch_error('facebook_connect_fail'));
							}
						}
					}

					// otherwise, fb account is not associated with any vb user
					else
					{
//echo "FB account not associated with any vB acct<br />\n";
						// redirect to the registration page to create a vb account
						if ($dofbredirect && !$inRegistrationRoute) // do_facebook_redirect()
						{
							header('Location: ' . vB5_Config::instance()->baseurl . '/register');
							exit;
						}

						// if not doing redirect and not trying to register,
						// pretend user is not logged into facebook so they can still browse the site
						else if (!$inRegistrationRoute)
						{
							$show['facebookuser'] = false;
							if ($dofbredirect)
							{
								standard_error(fetch_error('facebook_connect_fail'));
							}
						}
					}
				}
			}
		}
	}


	//public function publishToFacebook()
	//{
	//
	//}

}
