<?php

class vB5_Template_Stylesheet
{

	protected static $instance;
	protected $pending = array();

	public static function instance()
	{
		if (!isset(self::$instance))
		{
			$c = __CLASS__;
			self::$instance = new $c;
		}

		return self::$instance;
	}

	public function register($args)
	{
		$this->pending = array_unique(array_merge($this->pending, $args));
	}

	public function resetPending()
	{
		$this->pending = array();
	}

	public function insertCss(&$content)
	{
		$options = vB5_Template_Options::instance();
		$storecssasfile = $options->get('options.storecssasfile');
		$cssdate = intval($options->get('miscoptions.cssdate'));

		if (!$cssdate)
		{
			$cssdate = time(); // fallback so we get the latest css
		}

		$user = vB5_User::instance();
		$textdirection = ($user['lang_options']['direction'] ? 'ltr' : 'rtl');
		$styleid = vB5_Template_Stylevar::instance()->getPreferredStyleId();
		$vbcsspath = $this->getCssPath($storecssasfile, $textdirection, $styleid);

		$replace = '';
		//if user style customization is enabled we need to hand css_profile specially. It can never come from disk
		//regardless of the option setting.
		$userprofilecss = '';
		$options = vB::getDatastore()->getValue('options');
		$bf_misc_socnet = vB::getDatastore()->getValue('bf_misc_socnet');

		if ($options['enable_profile_styling'])
		{
			//we look for the css_profile.css file
			foreach ($this->pending as $key => $css)
			{
				if (substr($css, 0, 15) == 'css_profile.css')
				{
					$joinChar = (strpos($vbcsspath . $css, '?') === false) ? '?' : '&amp;';
					$userprofilecss = '<link rel="stylesheet" type="text/css" href="' .
						htmlspecialchars($vbcsspath . $css) . "{$joinChar}ts=$cssdate \" />\n";
					unset ($this->pending[$key]);
				}
			}
		}

		if ($storecssasfile)
		{
			foreach($this->pending as $css)
			{
				$replace .= '<link rel="stylesheet" type="text/css" href="' . htmlspecialchars($vbcsspath . $css) . "\" />\n";
			}
		}
		else
		{
			$joinChar = (strpos($vbcsspath, '?') === false) ? '?' : '&amp;';
			$replace .= '<link rel="stylesheet" type="text/css" href="' .
				htmlspecialchars($vbcsspath . implode(',', $this->pending)) . "{$joinChar}ts=$cssdate \" />\n";
		}

		$replace .= $userprofilecss . "\n";

		// insert the css before the first <script> tag in head element
		// if there is no script tag in <head>, then insert it at the
		// end of <head>
		$scriptPos = stripos($content, '<script');
		$headPos = stripos($content, '</head>');
		if ($scriptPos !== false && $headPos !== false && $scriptPos < $headPos)
		{
			$top = substr($content, 0, $scriptPos);
			$bottom = substr($content, $scriptPos);
			$content = $top . $replace . $bottom;
		}
		else
		{
			$replace .= '</head>';
			$content = str_replace('</head>', $replace, $content);
		}
	}


	private function getCssPath($storecssasfile, $textdirection, $styleid)
	{
		$csspath = "";
		if ($storecssasfile)
		{
			$csspath = 'clientscript/vbulletin_css/style' . str_pad($styleid, 5, '0', STR_PAD_LEFT) . $textdirection[0] . '/';
		}
		else
		{
/*
 * Not sure what to make of this....
 This looks like its for when forums have an override style set (Paul).
			$forumid = intval(key_exists('forumid', $this->cache['forumcache']));
			$forumstyleid = intval(key_exists('styleid', $this->cache['forumcache']));
			if (!$this->cache['options']['allowchangestyles'] AND $forumid > 0 AND $forumstyleid > 0)
			{
				$add_forumid = '&amp;forumid = ' . $forumid;
			}
			else
			{
				$add_forumid = '';
			}
*/
				$csspath = 'css.php?styleid=' . $styleid . '&td=' . $textdirection . '&sheet=';
		}

		$config = vB5_Config::instance();
		$csspath = $config->baseurl_core . '/' . $csspath;

		return $csspath;
	}
}
