<?php
namespace JExtstore\Component\JRealtimeAnalytics\Administrator\Framework;
/**
 *
 * @package JREALTIMEANALYTICS::FRAMEWORK::administrator::components::com_jrealtime
 * @subpackage framework
 * @subpackage view
 * @author Joomla! Extensions Store
 * @copyright (C) 2015 - Joomla! Extensions Store
 * @license GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html
 */
defined ( '_JEXEC' ) or die ( 'Restricted access' );
use Joomla\CMS\MVC\View\HtmlView;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\Filesystem\File;

/**
 * Base view for all display core
 *
 * @package JREALTIMEANALYTICS::FRAMEWORK::administrator::components::com_jrealtime
 * @subpackage framework
 * @subpackage view
 * @since 2.0
 */
class View extends HtmlView {
	/**
	 * User object for ACL authorise check
	 *
	 * @access protected
	 * @var Object
	 */
	protected $user;
	
	/**
	 * Reference to application
	 *
	 * @access public
	 * @var Object
	 */
	public $app;
	
	/**
	 * Document object, needed by views to inject
	 * CSS/JS tags into document output
	 *
	 * @access public
	 * @var Object
	 */
	public $document;
	
	/**
	 * Reference to option executed
	 *
	 * @access public
	 * @var string
	 */
	public $option;
	
	/**
	 * Inject language constant into JS Domain maintaining same name mapping
	 *
	 * @access protected
	 * @param $translations array        	
	 * @param $document Object&        	
	 * @return void
	 */
	protected function injectJsTranslations($translations, $document): void {
		$jsInject = null;
		// Do translations
		foreach ( $translations as $translation ) {
			$jsTranslation = strtoupper ( $translation );
			$translated = Text::_ ( $jsTranslation, true );
			$jsInject .= <<<JS
				var $translation = '{$translated}'; 
JS;
		}
		$document->getWebAssetManager()->addInlineScript ( $jsInject );
	}
	
	/**
	 * Manage injecting jQuery framework into document with class inheritance support
	 *
	 * @access protected
	 * @param Object& $doc
	 * @param boolean $fullStack         	
	 * @return void
	 */
	protected function loadJQuery($document, $fullStack = true): void {
		$wa = $document->getWebAssetManager();
		if($fullStack) {
			$wa->useScript('jquery');
			$wa->useScript('jquery-noconflict');
			array_map ( function ($script) use ($wa) {
				$wa->useScript ( 'bootstrap.' . $script );
			}, [
					'collapse',
					'modal',
					'popover',
					'tab'
			] );
		} else {
			$wa->useScript('jquery');
			$wa->useScript('jquery-noconflict');
		}
		
		// jQuery foundation framework
		$wa->registerAndUseScript('jrealtime.jstorage', 'administrator/components/com_jrealtimeanalytics/js/jstorage.min.js', [], [], ['jquery']);
		$base = Uri::root ();
		$document->getWebAssetManager()->addInlineScript ( "var jrealtimeBaseURI='$base';" );
	}
	
	/**
	 * Manage injecting Bootstrap framework into document
	 *
	 * @access protected
	 * @param Object& $doc        	
	 * @param string $application
	 * @return void
	 */
	protected function loadBootstrap($document, $application = 'administrator/'): void {
		// Main styles for admin interface
		$document->getWebAssetManager()->registerAndUseStyle ( 'jrealtime.boostrap-interface', $application . 'components/com_jrealtimeanalytics/css/bootstrap-interface.css');
		
		if(version_compare(JVERSION, '5', '>=') && $this->app->isClient('administrator')) {
			$document->getWebAssetManager()->registerAndUseStyle ( 'jrealtime.dark-theme', 'administrator/components/com_jrealtimeanalytics/css/dark-theme.css');
		}
		
		// Main JS file for admin interface
		$document->getWebAssetManager()->registerAndUseScript ( 'jrealtime.boostrap-interface', $application . 'components/com_jrealtimeanalytics/js/bootstrap-interface.js', [], [], ['jquery']);
	}
	
	/**
	 * Manage injecting jQuery Widgets script into document
	 *
	 * @access protected
	 * @param Object& $doc        	
	 * @return void
	 */
	protected function loadJQWidgets($document): void {
		// Main JS file for admin interface
		$document->getWebAssetManager()->registerAndUseScript ( 'jrealtime.jqxcore', 'administrator/components/com_jrealtimeanalytics/js/libraries/jqwidgets/jqxcore.js', [], [], ['jquery']);
		
		$document->getWebAssetManager()->registerAndUseScript ( 'jrealtime.jqxchart', 'administrator/components/com_jrealtimeanalytics/js/libraries/jqwidgets/jqxchart.js', [], [], ['jquery', 'jrealtime.jqxcore']);
		
		$document->getWebAssetManager()->registerAndUseScript ( 'jrealtime.jqxgauge', 'administrator/components/com_jrealtimeanalytics/js/libraries/jqwidgets/jqxgauge.js', [], [], ['jquery', 'jrealtime.jqxcore', 'jrealtime.jqxchart']);
		
		$document->getWebAssetManager()->registerAndUseScript ( 'jrealtime.jquery.raty', 'administrator/components/com_jrealtimeanalytics/js/libraries/raty/jquery.raty.js', [], [], ['jquery']);
	}
	
	/**
	 * Manage injecting jQuery Fancybox
	 *
	 * @access protected
	 * @param Object& $doc
	 * @return void
	 */
	protected function loadJQFancybox($document): void {
		// Main JS file for admin interface
		$document->getWebAssetManager()->registerAndUseStyle ( 'jrealtime.jquery.fancybox', 'administrator/components/com_jrealtimeanalytics/js/libraries/fancybox/jquery.fancybox-1.3.4.css');
		
		$document->getWebAssetManager()->registerAndUseScript ( 'jrealtime.jquery.fancybox', 'administrator/components/com_jrealtimeanalytics/js/libraries/fancybox/jquery.fancybox-1.3.4.pack.js', [], [], ['jquery']);
	}
	
	/**
	 * Manage injecting valildation plugin into document
	 *
	 * @access protected
	 * @param Object& $doc        	
	 * @return void
	 */
	protected function loadValidation($document): void {
		$document->getWebAssetManager()->registerAndUseStyle ( 'jrealtime.simplevalidation', 'administrator/components/com_jrealtimeanalytics/css/simplevalidation.css');
		
		$document->getWebAssetManager()->registerAndUseScript ( 'jrealtime.simplevalidation', 'administrator/components/com_jrealtimeanalytics/js/jquery.simplevalidation.js', [], [], ['jquery']);
	}
	
	/**
	 * Manage injecting jQuery UI framework into document
	 *
	 * @access protected
	 * @param Object& $doc        	
	 * @return void
	 */
	protected function loadJQueryUI($document): void {
		$document->getWebAssetManager()->registerAndUseStyle ( 'jrealtime.jquery-ui', 'administrator/components/com_jrealtimeanalytics/css/jqueryui/jquery.ui.all.css');
		
		$document->getWebAssetManager()->registerAndUseScript ( 'jrealtime.jquery-ui', 'administrator/components/com_jrealtimeanalytics/js/jquery.ui.js', [], [], ['jquery']);
		
		if(ComponentHelper::getParams('com_jrealtimeanalytics')->get('stats_calendar_type', 'date') == 'datetime') {
			$document->getWebAssetManager()->registerAndUseStyle ( 'jrealtime.timepicker', 'administrator/components/com_jrealtimeanalytics/css/jqueryui/jquery.ui-timepicker-addon.css');
			$document->getWebAssetManager()->registerAndUseScript ( 'jrealtime.timepicker', 'administrator/components/com_jrealtimeanalytics/js/jquery.ui-timepicker-addon.js', [], [], ['jquery', 'jrealtime.jquery-ui']);
			
			// Load the language object and the translation accordingly
			$language = Factory::getApplication()->getLanguage ();
			$langTag = $language->getTag ();
			
			$explodedLangTag = explode ( '-', $langTag );
			$langCode = array_shift ( $explodedLangTag );
			
			// Security safe
			if (! file_exists ( JPATH_SITE . '/administrator/components/com_jrealtimeanalytics/js/i18n/datepicker/datepicker-' . $langCode . '.js' )) {
				$langCode = 'en';
			}
			
			$document->getWebAssetManager()->registerAndUseScript ( 'jrealtime.i18n.datepicker', 'administrator/components/com_jrealtimeanalytics/js/i18n/datepicker/datepicker-' . $langCode . '.js', [], [], ['jrealtime.jquery-ui'] );
			
			// Security safe
			if (! file_exists ( JPATH_SITE . '/administrator/components/com_jrealtimeanalytics/js/i18n/timepicker/timepicker-' . $langCode . '.js' )) {
				$langCode = 'en';
			}
			
			$document->getWebAssetManager()->registerAndUseScript ( 'jrealtime.i18n.timepicker', 'administrator/components/com_jrealtimeanalytics/js/i18n/timepicker/timepicker-' . $langCode . '.js', [], [], ['jrealtime.timepicker'] );
		}
	}
	
	/**
	 * Manage injecting JQVMap plugin
	 *
	 * @access protected
	 * @param Object& $doc        	
	 * @return void
	 */
	protected function loadJQVMap($document): void {
		$document->getWebAssetManager()->registerAndUseStyle ( 'jrealtime.jqvmap', 'administrator/components/com_jrealtimeanalytics/js/libraries/jqvmap/jqvmap.css');
		
		$document->getWebAssetManager()->registerAndUseScript ( 'jrealtime.jquery.vmap', 'administrator/components/com_jrealtimeanalytics/js/libraries/jqvmap/jquery.vmap.js', [], [], ['jquery']);
		$document->getWebAssetManager()->registerAndUseScript ( 'jrealtime.jquery.vmap.world', 'administrator/components/com_jrealtimeanalytics/js/libraries/jqvmap/jquery.vmap.world.js', [], [], ['jquery', 'jrealtime.jquery.vmap']);
	}
	
	/**
	 * Class constructor
	 *
	 * @param array $config
	 *        	return Object
	 */
	public function __construct($config = array()) {
		parent::__construct ( $config );
		
		$this->app = Factory::getApplication ();
		$this->user = $this->app->getIdentity ();
		$this->option = $this->app->getInput()->get ( 'option' );
		$this->document = $this->app->getDocument();
		if (method_exists($this, 'setDocument')) {
			$this->setDocument($this->document);
		}
	}
}