<?php

defined('HOSTCMS') || exit('HostCMS: access denied.');

/**
 * Syntaxhighlighter_Driver_Ace_Handler.
 *
 * @package HostCMS
 * @subpackage Syntaxhighlighter
 * @version 7.x
 * @author Hostmake LLC
 * @copyright © 2005-2025 ООО "Хостмэйк" (Hostmake LLC), http://www.hostcms.ru
 */
class Syntaxhighlighter_Driver_Ace_Handler extends Syntaxhighlighter_Handler
{
	/**
	 * Base path
	 * @var string
	 */
	static protected $_basePath = '/modules/syntaxhighlighter/driver/ace';

	/**
	 * Get driver syntaxhighlighter options config
	 * @return array|NULL
	 */
	public function getConfig()
	{
		return Core_Config::instance()->get('syntaxhighlighter_ace', array());
	}

	/**
	 * Get driver js list
	 * @return array
	 */
	public function getJsList()
	{
		//https://github.com/ajaxorg/ace-builds

		return array(
			self::$_basePath . '/ace.js',
			self::$_basePath . '/theme-github.js',
			self::$_basePath . '/mode-html.js',
			self::$_basePath . '/mode-php.js',
			self::$_basePath . '/mode-css.js',
			self::$_basePath . '/mode-less.js',
			self::$_basePath . '/mode-scss.js',
			self::$_basePath . '/mode-xml.js',
			self::$_basePath . '/mode-sql.js',
			self::$_basePath . '/mode-smarty.js',
			self::$_basePath . '/ext-language_tools.js',
			self::$_basePath . '/ext-searchbox-hostcms.js',
			self::$_basePath . '/ext-prompt.js',
			// self::$_basePath . '/ext-beautify.js',
			self::$_basePath . '/syntaxhighlighter.js'
		);
	}

	/**
	 * Get driver css list
	 * @return array
	 */
	public function getCssList()
	{
		return array();
	}

	/**
	 * Get driver raw js
	 * @return array
	 */
	public function getJs()
	{
		return NULL;
	}

	/**
	 * Init
	 * @param Admin_Form_Entity $oAdmin_Form_Entity_Textarea
	 */
	public function init($oAdmin_Form_Entity_Textarea)
	{
		$aTmp = array();

		$aConfig = $this->getConfig();
		$aConfig['mode'] = '"ace/mode/' . $oAdmin_Form_Entity_Textarea->syntaxHighlighterMode . '"';

		foreach ($aConfig as $key => $value)
		{
			is_string($value) && !is_numeric($value) && $value[0] != '"' && $value = '"' . $value . '"';
			is_bool($value) && $value = $value ? 'true' : 'false';

			$aTmp[] = Core_Str::escapeJavascriptVariable($key) . ": " . (
				is_array($value)
					? json_encode($value)
					: $value
				);
		}

		// $sHeight = ($this->rows * 15) . 'px';

		$Core_Html_Entity_Script = new Core_Html_Entity_Script();
		$Core_Html_Entity_Script
			/*->value("$(function() { var editor = CodeMirror.fromTextArea(document.getElementById('" . Core_Str::escapeJavascriptVariable($this->id) . "'), {
				" . implode(",\n", $aTmp) . "
			});
			editor.setSize(null, '{$sHeight}');
			});")*/
			->value("
				$(function() {
					var textarea = document.getElementById('" . Core_Str::escapeJavascriptVariable($oAdmin_Form_Entity_Textarea->id) . "'),
						div = document.createElement('div'),
						jTextarea = $(textarea);

					$(div).insertAfter(jTextarea.hide()).text(jTextarea.val());

					// var beautify = ace.require('ace/ext/beautify');

					var editor = ace.edit(div, {
						" . implode(",\n							", $aTmp) . "
					});

					// beautify.beautify(editor.session);

					editor.setOptions({
						maxLines: '{$oAdmin_Form_Entity_Textarea->rows}',
						minLines: '{$oAdmin_Form_Entity_Textarea->rows}'
					});
				});
			")
			->execute();
	}
}