<?php
/**
 * @version   $Id$
 * @author    RocketTheme http://www.rockettheme.com
 * @copyright Copyright (C) 2007 - 2012 RocketTheme, LLC
 * @license   http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
 */

class RokSprocket
{
	protected static $globalHeadersRendered = false;

	/** @var \RokCommon_Registry */
	protected $params;

	/**
	 * @var \RokCommon_Service_Container
	 */
	protected $container;
	/**
	 * @var \RokCommon_Logger
	 */
	protected $logger;

	const BASE_PACKAGE_NAME = 'roksprocket_base';

	/**
	 * @var
	 */
	protected $context_base;

	/**
	 * @param RokCommon_Registry $params
	 */
	public function __construct(RokCommon_Registry $params)
	{
		$this->params = $params;
		/** @var $container RokCommon_Service_Container */
		$this->container = RokCommon_Service::getContainer();
		/** @var $logger RokCommon_Logger */
		$this->logger = $this->container->roksprocket_logger;
	}

	/**
	 * @return RokSprocket_ItemCollection
	 */
	public function getData()
	{
		$items = self::getItemsWithParams($this->params->get('module_id'), $this->params, true);
		$limit = $this->params->get('display_limit', '∞');
		if ($limit != '∞' && (int)$limit > 0) {
			$items = $items->trim($limit);
		}
		return $items;
	}

	/**
	 *
	 * @param \RokSprocket_ItemCollection $items
	 *
	 * @return string the html to be rendered
	 */
	public function render(RokSprocket_ItemCollection $items)
	{


		// get the layout
		$layout_name    = $this->params->get('layout');

		$layout_service = $this->container[sprintf('roksprocket.layouts.%s.service', $layout_name)];
		// add the layout classpath
		$layout_lib_path = $this->container[sprintf('roksprocket.layouts.%s.library.paths', $layout_name)];
		foreach ($layout_lib_path as $lib_path) {
			RokCommon_ClassLoader::addPath($lib_path);

		}
		/** @var $i18n RokCommon_I18N */
		$i18n = $this->container->i18n;
		$layout_lang_paths = $this->container[sprintf('roksprocket.layouts.%s.paths', $layout_name)];
		foreach($layout_lang_paths as $lang_path)
		{
			@$i18n->loadLanguageFiles('roksprocket_layout_'.$layout_name, $lang_path);
		}

		/** @var $layout RokSprocket_Layout */
		$layout = $this->container->$layout_service;

		$layout->initialize($items, $this->params);

		$this->renderGlobalHeaders();
		$layout->renderLayoutHeaders();
		$layout->renderInstanceHeaders();

		ob_start();
		echo $layout->renderBody();
		return ob_get_clean();
	}

	/**
	 * @param \RokCommon_Registry $params
	 */
	public function setParams($params)
	{
		$this->params = $params;
	}

	/**
	 * @return \RokCommon_Registry
	 */
	public function getParams()
	{
		return $this->params;
	}

	/**
	 * @param \RokCommon_Logger $logger
	 */
	public function setLogger($logger)
	{
		$this->logger = $logger;
	}

	/**
	 * @return \RokCommon_Logger
	 */
	public function getLogger()
	{
		return $this->logger;
	}

	/**
	 * @param \RokCommon_Service_Container $container
	 */
	public function setContainer($container)
	{
		$this->container = $container;
	}

	/**
	 * @return \RokCommon_Service_Container
	 */
	public function getContainer()
	{
		return $this->container;
	}


	public static function registerPaths()
	{

	}

	public function renderGlobalHeaders()
	{
		if (!self::$globalHeadersRendered) {
			RokCommon_Header::addScript(RokCommon_Composite::get($this->context_base . '.assets.js')->getUrl('mootools-mobile.js'));
			RokCommon_Header::addScript(RokCommon_Composite::get($this->context_base . '.assets.js')->getUrl('rokmediaqueries.js'));
			RokCommon_Header::addScript(RokCommon_Composite::get($this->context_base . '.assets.js')->getUrl('roksprocket.js'));

			$ns   = array();
			$ns[] = "if (typeof RokSprocket == 'undefined') RokSprocket = {};";
			$ns[] = "Object.merge(RokSprocket, {";
			$ns[] = "	SiteURL: '" . str_replace('&', '&amp;', JRoute::_(JURI::root(true), true)) . "',";
			$ns[] = "	CurrentURL: '" . str_replace('&', '&amp;', JRoute::_(JURI::current(true), true)) . "',";
			$ns[] = "	AjaxURL: '" . str_replace('&', '&amp;', JRoute::_(JURI::root(true) . '/index.php?option=com_roksprocket&task=ajax&format=raw')) . "'";
			$ns[] = "});";

			RokCommon_Header::addInlineScript(implode("\n", $ns) . "\n");

			self::$globalHeadersRendered = true;
		}
	}

	/**
	 * @static
	 *
	 * @param                    $moduleId
	 * @param RokCommon_Registry $parameters
	 * @param bool               $apply_random
	 *
	 * @param bool               $unpublished
	 *
	 * @return RokSprocket_ItemCollection
	 */
	public static function getItemsWithParams($moduleId, RokCommon_Registry $parameters, $apply_random = true, $unpublished = false)
	{
		$container = RokCommon_Service::getContainer();

		/** @var $platformHelper RokSprocket_PlatformHelper */
		$platformHelper = $container->roksprocket_platformhelper;
		$provider_type  = $parameters->get('provider', 'joomla');

		/** @var $provider RokSprocket_IProvider */
		$provider_service = $container['roksprocket.providers.registered.' . $provider_type . '.service'];
		$provider         = $container->$provider_service;

		$provider->setParams($parameters);

		$provider_filters  = $parameters->get($provider_type . '_filters', array());
		$provider_articles = $parameters->get($provider_type . '_articles', array());

		$sort_type    = $parameters->get($provider_type . '_sort', 'automatic');
		$sort_append  = $parameters->get(sprintf('%s_sort_%s_append', $provider_type, $sort_type));
		$sort_filters = RokCommon_Utils_ArrayHelper::fromObject($parameters->get(sprintf('%s_sort_%s_filters', $provider_type, $sort_type), array()));

		$filters = array();
		if (!empty($provider_filters)) {
			$filters = array_merge($filters, RokCommon_Utils_ArrayHelper::fromObject($provider_filters));
		}
		if (!empty($provider_articles)) {
			$filters = array_merge($filters, RokCommon_Utils_ArrayHelper::fromObject($provider_articles));
		}

		return self::getItems($provider, $moduleId, $filters, $sort_filters, $sort_type, $sort_append, $apply_random);
	}

	/**
	 * @static
	 *
	 * @param string             $moduleId
	 * @param string             $provider_type
	 * @param array              $provider_filters
	 * @param array              $provider_articles
	 * @param array              $sort_filters
	 * @param string             $sort_type
	 * @param string             $sort_append
	 * @param RokCommon_Registry $extra_parameters
	 * @param bool               $apply_random
	 *
	 * @param bool               $unpublished
	 *
	 * @return RokSprocket_ItemCollection
	 */
	public static function getItemsWithFilters($moduleId, $provider_type, $provider_filters, $provider_articles, $sort_filters, $sort_type, $sort_append, &$extra_parameters, $apply_random = false, $unpublished = false)
	{
		$container = RokCommon_Service::getContainer();

		/** @var $provider RokSprocket_IProvider */
		$provider_service = $container['roksprocket.providers.registered.' . $provider_type . '.service'];
		$provider         = $container->$provider_service;

		$provider->setParams($extra_parameters);

		$filters = array();
		if (!empty($provider_filters)) {
			$filters = array_merge($filters, RokCommon_Utils_ArrayHelper::fromObject($provider_filters));
		}
		if (!empty($provider_articles)) {
			$filters = array_merge($filters, RokCommon_Utils_ArrayHelper::fromObject($provider_articles));
		}

		return self::getItems($provider, $moduleId, $filters, $sort_filters, $sort_type, $sort_append, $apply_random, $unpublished);
	}

	/**
	 * @static
	 *
	 * @param RokSprocket_IProvider $provider
	 * @param                       $moduleId
	 * @param                       $filters
	 * @param                       $sort_filters
	 * @param                       $sort_type
	 * @param                       $sort_append
	 * @param                       $apply_random
	 *
	 * @param bool                  $unpublished
	 *
	 * @return RokSprocket_ItemCollection
	 */
	protected static function getItems(RokSprocket_IProvider &$provider, $moduleId, $filters, $sort_filters, $sort_type, $sort_append, $apply_random, $unpublished = false)
	{
		if (!empty($filters)) {
			$provider->setFilterChoices($filters, $sort_filters);
			$provider->setModuleId($moduleId);
			$provider->setShowUnpublished($unpublished);
			$items                       = $provider->getItems();
			$sort_options                = array();
			$sort_options['append']      = $sort_append;
			$sort_options['applyrandom'] = $apply_random;
			$items->sort($sort_type, $sort_options);
		} else {
			$items = new  RokSprocket_ItemCollection();
		}

		return $items;
	}

}
