<?php
/**
 * @file       clock/syntax.php
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Luis Machuca Bezzaza <luis.machuca [at] gulix.cl>
 * @version    2.0
 *
 */

if(!defined('DW_LF')) define('DW_LF',"\n");
 
if(!defined('DOKU_INC')) 
define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) 
define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
 
class syntax_plugin_clock extends DokuWiki_Syntax_Plugin {
function getType(){
	return 'substition';
}

function getAllowedTypes() { 
	return array(); 
}

function getSort(){
	return 290;
}

function getPType(){ 
	return 'block'; 
}

function connectTo($mode) {
	$this->Lexer->addSpecialPattern (
	'^\{\{clock\}\}$', $mode, 'plugin_clock');
}
 
function handle($match, $state, $pos, Doku_Handler $handler){
	$data= array();
	$theJS= $this->getConf('nojs_fallback');
	// if 'nojs_fallback' is set, we get the time from the server
	// if 'clock_infopage' contains a link, we convert it

	/* compose the data array */
	$data['type']    = $this->getConf('clock_type');
	$data['style']   = $this->getConf('clock_style');
	$data['text']    = $theJS ? date('H:i:s') : 'clock';

	/* Are we ready yet? */
	return $data;
}  

function render($mode, Doku_Renderer $renderer, $data) {
	static $wasrendered= false;
	if ($wasrendered===true) {return true; }
	if ($mode == 'xhtml') {
		$nl   = DW_LF;
		$cty  = $this->getConf('clock_type');
		$cid  = $this->getConf('clock_id');
		$html = $this->_clock_createblock_html($data, $cty);
		$hbar = ($this->getConf('helpbar')) ? $this->_get_clock_helpbar() : '';
		$renderer->doc .= <<<EOF
<div id="clock_wrapper">$html 
$hbar </div><!-- end clock-->
EOF;
		$wasrendered= true;
	} else if ($mode == 'odt') {
		return false; // may be implemented in the future
	}
	else if ($mode == 'text') {
		if ($wasrendered) return true;
		$text= ' ['. $c. ' '. date('H:i'). '] ';
		$renderer->doc .= $text;
		$wasrendered= true;
		return true;
	}
	/* That's all about rendering that needs to be done, at the moment */
	return false;
}

/**
 * From this point onwards, local (helper) functions are implemented.
 */

	/**
     * @fn          dw_clock_helpbar
     * @brief       Returns the contents of the help bar
     * The helpbar is displayed only when $conf['helpbar'] is set.
     */
private function _get_clock_helpbar () {
	$p.= '<p class="helpbar" >';
	$link= $this->getConf('clock_infopage');
	if (empty($link) ) { // point to plugin page by default
		$info= '[[doku>plugin:clock|Clock Plugin]]';
	} else {
		$info= "[[$link|Info]]";
	}
	$info= p_render('xhtml', p_get_instructions($info), $info);
	$info= trim(substr($info, 4, -5)); // remove <p>, </p>
	$p.= $info;
	$p.= '</p>';       
	return $p;
}

private function _clock_createblock_html($data, $cty='text') {
	$theType    = $data['type'] ?? 'text';
	$theStyle   = $data['style'] ?? 'plain';
	$theText    = $data['text'];
	$id= $this->getConf('clock_id');
	$codetext= <<<EOF
<div class="${theType}"><div id="dw_clock_object" class="${theStyle}"> {1} </div>
</div>
EOF;
	if ($cty=== 'text' || $cty=== 'beats') {
		$s1= 'default';
	} else if ($cty === 'binary') {
		$s1= <<<EOF
<span class="hh"> 
<span></span><span></span><span></span><span></span><span></span>
</span>
<span class="mm">
<span></span><span></span><span></span><span></span><span></span><span></span>
</span>
<span class="ss">
<span></span><span></span><span></span><span></span><span></span><span></span>
</span>
EOF;
	}
	$codetext= str_replace('{1}', $s1, $codetext);
	return $codetext;
}

}
