<?php defined('SYSPATH') or die('No direct access allowed.');

/**
 * This class is for rendering system information messages.
 *
 * @package    lnApp
 * @category   lnApp/Helpers
 * @author     Deon George
 * @copyright  (c) 2009-2013 Deon George
 * @license    http://dev.leenooks.net/license.html
 */
abstract class lnApp_SystemMessage extends HTMLRender {
	protected static $_data = array();
	protected static $_spacer = '<table><tr class="spacer"><td>&nbsp;</td></tr></table>';
	protected static $_required_keys = array('title','body','type');

	/**
	 * Add a system message to be rendered
	 *
	 * @param array System Message attributes
	 */
	public static function add($msg,$prepend=FALSE) {
		if ($msgs = Session::instance()->get('sessionmsgs')) {
			static::$_data = $msgs;
		}

		parent::add($msg);

		// Add a gribber popup
		Style::add(array(
			'type'=>'file',
			'data'=>'css/jquery.gritter.css',
			'media'=>'screen',
		));
		Script::add(array(
			'type'=>'file',
			'data'=>'js/jquery.gritter-1.5.js',
		));
		Script::add(array(
			'type'=>'stdin',
			'data'=>sprintf(
'$(document).ready(function() {
	$.extend($.gritter.options, {
		fade_in_speed: "medium",
		fade_out_speed: 2000,
		time: "3000",
		sticky: false,
	});
	$.gritter.add({
	title: "%s",
	text: "%s",
	image: "%s",
});});',$msg['title'],$msg['body'],URL::site().static::image($msg['type'],true))));

		// Save our messages in our session, so that we get them for redirects
		Session::instance()->set('sessionmsgs',static::$_data);
	}

	/**
	 * Return an instance of this class
	 *
	 * @return SystemMessage
	 */
	public static function factory() {
		return new SystemMessage;
	}

	/**
	 * Render an image for the System Message
	 */
	public static function image($type,$raw=false,$big=false,$alt='') {
		$mediapath = Route::get(static::$_media_path);

		switch ($type) {
			case 'error':
				$file = sprintf('img/dialog-error%s.png',$big ? '-big' : '');
				break;
			case 'info':
				$file = sprintf('img/dialog-information%s.png',$big ? '-big' : '');
				break;
			case 'warning':
				$file = sprintf('img/dialog-warning%s.png',$big ? '-big' : '');
				break;
			case 'debug':
				$file = sprintf('img/dialog-question%s.png',$big ? '-big' : '');
				break;
			default:
				throw new Kohana_Exception('Unknown system message type :type',array(':type'=>$value['type']));
		}

		if ($raw)
			return $mediapath->uri(array('file'=>$file));
		else
			return HTML::image($mediapath->uri(array('file'=>$file)),array('alt'=>$alt ? $alt : '','class'=>'sysicon'));
	}

	/**
	 * Render this system message
	 *
	 * @see HTMLRender::render()
	 */
	protected function render() {
		$output = '';
		$mediapath = Route::get(static::$_media_path);

		// Reload our message from the session
		if ($msgs = Session::instance()->get('sessionmsgs')) {
			Session::instance()->delete('sessionmsgs');
			static::$_data = $msgs;
		}

		$i = 0;
		foreach (static::$_data as $value) {
			if ($i++)
				$output .= static::$_spacer;

			$output .= '<table><tr>';
			$output .= sprintf('<td class="icon" rowspan="2">%s</td>',static::image($value['type'],false,false,isset($value['alt']) ? $value['alt'] : ''));
			$output .= sprintf('<td class="head">%s</td>',$value['title']);
			$output .= '</tr><tr>';
			$output .= sprintf('<td class="body">%s</td>',$value['body']);
			$output .= '</tr></table>';
		}

		return $output;
	}
}
?>
