<?php
namespace JExtstore\Component\JRealtimeAnalytics\Administrator\View\Cpanel;
/**
 *
 * @package JREALTIMEANALYTICS::CPANEL::administrator::components::com_jrealtimeanalytics
 * @subpackage views
 * @subpackage cpanel
 * @author Joomla! Extensions Store
 * @copyright (C) 2013 - Joomla! Extensions Store
 * @license GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html 
 */
defined ( '_JEXEC' ) or die ( 'Restricted access' );
use Joomla\CMS\Language\Text;
use Joomla\CMS\Factory;
use Joomla\CMS\Uri\Uri;
use JExtstore\Component\JRealtimeAnalytics\Administrator\Framework\Helpers\Toolbars as ToolbarHelper;
use JExtstore\Component\JRealtimeAnalytics\Administrator\Framework\View as JRealtimeView;

/**
 * CPanel view
 *
 * @package JREALTIMEANALYTICS::CPANEL::administrator::components::com_jrealtimeanalytics
 * @subpackage views
 * @subpackage cpanel
 * @since 1.0
 */
class HtmlView extends JRealtimeView {
	// Template view variables
	protected $user;
	protected $icons;
	protected $updatesData;
	protected $infoData;
	protected $cParams;
	protected $currentVersion;
	public $httpClient;
	
	/**
	 * Renderizza l'iconset del cpanel
	 *
	 * @param $link string
	 * @param $image string
	 * @access private
	 * @return string
	 */
	private function getIcon($link, $image, $text, $target = '', $title = null, $class = 'icons') {
		$app = Factory::getApplication ();
		$lang = $app->getLanguage ();
		$option = $this->option;
		?>
<div class="<?php echo $class;?>" style="float:<?php echo ($lang->isRTL()) ? 'right' : 'left'; ?>;" role="button">
	<div class="icon" aria-hidden="true">
		<a <?php echo $title;?> <?php echo $target;?> href="<?php echo $link; ?>"> 
			<div class="task <?php echo $image;?>"></div> 
			<span class="task"><?php echo $text; ?></span>
		</a>
	</div>
</div>
<?php
		}
	
	/**
	 * Add the page title and toolbar.
	 *
	 * @since	1.6
	 */
	protected function addDisplayToolbar() {
		ToolbarHelper::title( Text::_( 'COM_JREALTIME_CPANEL_TOOLBAR' ), 'jrealtimeanalytics' );
		ToolbarHelper::custom('cpanel.display', 'home', 'home', 'COM_JREALTIME_CPANEL', false);
	}
	
	/**
	 * Effettua il rendering del pannello di controllo
	 * @access public
	 * @return void
	 */
	public function display($tpl = null) {
		$doc = $this->app->getDocument ();
		$this->loadJQuery ( $doc );
		$this->loadBootstrap ( $doc );
		$this->loadJQWidgets ( $doc );
		$this->loadJQVMap( $doc );
		$componentParams = $this->getModel()->getState('cparams');

		$wa = $doc->getWebAssetManager();
		$wa->registerAndUseStyle ( 'jrealtime.cpanel', 'administrator/components/com_jrealtimeanalytics/css/cpanel.css');
		$wa->registerAndUseStyle ( 'jrealtime.sitespeed', 'administrator/components/com_jrealtimeanalytics/css/sitespeed.css');
		
		// Add JS Libs
		$wa->registerAndUseScript ( 'jrealtime.chart', 'administrator/components/com_jrealtimeanalytics/js/chart.js');
		
		// Core MVC JS
		$wa->registerAndUseScript ( 'jrealtime.sitespeed.view', 'administrator/components/com_jrealtimeanalytics/js/views/sitespeed.view.js', [], [], ['jquery']);
		$wa->registerAndUseScript ( 'jrealtime.sitespeed.model', 'administrator/components/com_jrealtimeanalytics/js/models/sitespeed.model.js', [], [], ['jquery']);
		$wa->registerAndUseScript ( 'jrealtime.sitespeed.controller', 'administrator/components/com_jrealtimeanalytics/js/controllers/sitespeed.controller.js', [], [], ['jquery']);
		$wa->registerAndUseScript ( 'jrealtime.cpanel', 'administrator/components/com_jrealtimeanalytics/js/cpanel.js', [], [], ['jquery', 'jrealtime.boostrap-interface', 'jrealtime.chart']);
		
		// Inject js translations
		$translations = array (
				'COM_JREALTIME_PAGELOADED_TIME',
				'COM_JREALTIME_TOTALPAGES_CHART',
				'COM_JREALTIME_TOTALUSERS_CHART',
				'COM_JREALTIME_SYSTEMUSERS_CHART',
				'COM_JREALTIME_SYSTEMEVENTS_CHART',
				'COM_JREALTIME_PAGELOADED_TIME',
				'COM_JREALTIME_VERYFAST',
				'COM_JREALTIME_FAST',
				'COM_JREALTIME_AVERAGE',
				'COM_JREALTIME_SLOW',
				'COM_JREALTIME_VERYSLOW',
				'COM_JREALTIME_SLOWER',
				'COM_JREALTIME_FASTER',
				'COM_JREALTIME_SPEEDTEST_ADVICE',
				'COM_JREALTIME_SITESPEED_POPUP_TITLE',
				'COM_JREALTIME_SITESPEED_TEST',
				'COM_JREALTIME_EXPIREON',
				'COM_JREALTIME_CLICKTOUPDATE',
				'COM_JREALTIME_UPDATEPROGRESSTITLE',
				'COM_JREALTIME_DOWNLOADING_UPDATE_SUBTITLE',
				'COM_JREALTIME_INSTALLING_UPDATE_SUBTITLE',
				'COM_JREALTIME_COMPLETED_UPDATE_SUBTITLE',
				'COM_JREALTIME_CPANEL_SITESPEED_EXEC',
				'COM_JREALTIME_PAGESPEED_SCORE',
				'COM_JREALTIME_PAGESPEED_WAITER',
				'COM_JREALTIME_PAGESPEED_LOW',
				'COM_JREALTIME_PAGESPEED_AVERAGE',
				'COM_JREALTIME_PAGESPEED_HIGH',
				'COM_JREALTIME_PAGESPEED_LCP',
				'COM_JREALTIME_PAGESPEED_LCP_DESC',
				'COM_JREALTIME_PAGESPEED_LCP_DESC_XTD',
				'COM_JREALTIME_PAGESPEED_FID',
				'COM_JREALTIME_PAGESPEED_FID_DESC',
				'COM_JREALTIME_PAGESPEED_FID_DESC_XTD',
				'COM_JREALTIME_PAGESPEED_CLS',
				'COM_JREALTIME_PAGESPEED_CLS_DESC',
				'COM_JREALTIME_PAGESPEED_CLS_DESC_XTD'
		);
		$this->injectJsTranslations ( $translations, $doc );
		
		$infoData = $this->getModel ()->getData ();
		$doc->getWebAssetManager()->addInlineScript ( 'var jrealtimeChartData = ' . json_encode ( $infoData ) . ';' );
		$doc->getWebAssetManager()->addInlineScript ( 'var jrealtimeAutoExecuteSiteSpeed = ' . $this->getModel()->getState('cparams')->get('auto_execute_sitespeed', 0) . ';' );
		
		// Buffer delle icons
		ob_start ();
		// Full stats report
		$this->getIcon ( 'index.php?option=com_jrealtimeanalytics&amp;task=serverstats.display&amp;statsreport=full', 'fas fa-chart-area', Text::_ ( 'COM_JREALTIME_SERVERSTATS' ), null, null, 'icons-first' );
		
		// Specific stats report
		$this->getIcon ( 'index.php?option=com_jrealtimeanalytics&amp;task=serverstats.display&amp;statsreport=landingleave', 'fas fa-copy', Text::_ ( 'COM_JREALTIME_LANDINGLEAVE' ), null, null, 'icons-first' );
		$this->getIcon ( 'index.php?option=com_jrealtimeanalytics&amp;task=serverstats.display&amp;statsreport=visitsbypage', 'fas fa-file', Text::_ ( 'COM_JREALTIME_VISITS_BYPAGE' ), null, null, 'icons-first' );
		$this->getIcon ( 'index.php?option=com_jrealtimeanalytics&amp;task=serverstats.display&amp;statsreport=visitsbyuser', 'fas fa-users', Text::_ ( 'COM_JREALTIME_VISITS_BYUSER' ), null, null, 'icons-first' );
		$this->getIcon ( 'index.php?option=com_jrealtimeanalytics&amp;task=serverstats.display&amp;statsreport=visitsbyip', 'fas fa-map-marker-alt', Text::_ ( 'COM_JREALTIME_VISITS_BYIP' ), null, null, 'icons-first' );
		$this->getIcon ( 'index.php?option=com_jrealtimeanalytics&amp;task=serverstats.display&amp;statsreport=referralkeys', 'fas fa-exchange-alt', Text::_ ( 'COM_JREALTIME_VISITS_REFERRAL_KEYWORDS' ), null, null, 'icons-first' );
		
		$this->getIcon ( 'index.php?option=com_jrealtimeanalytics&amp;task=heatmap.display', 'fas fa-map', Text::_ ( 'COM_JREALTIME_HEATMAP' ) );
		$this->getIcon ( 'index.php?option=com_jrealtimeanalytics&amp;task=overlook.display', 'fas fa-chart-bar', Text::_ ( 'COM_JREALTIME_OVERVIEW' ) );
		
		$this->getIcon ( 'index.php?option=com_jrealtimeanalytics&amp;task=realstats.display', 'fas fa-chart-bar', Text::_ ( 'COM_JREALTIME_REALSTATS' ) );
		
		$this->getIcon ( 'index.php?option=com_jrealtimeanalytics&amp;task=eventstats.display', 'fas fa-bolt', Text::_ ( 'COM_JREALTIME_EVENTSTATS' ) );
		$this->getIcon ( 'index.php?option=com_jrealtimeanalytics&amp;task=categories.display', 'fas fa-folder', Text::_ ( 'COM_JREALTIME_EVENTS_CATEGORIES' ) );
		
		// Access check.
		if ($this->user->authorise('jrealtime.google', 'com_jrealtimeanalytics')) {
			$analyticsService = $componentParams->get('analytics_service', 'google');
			$stringtextTitle = 'COM_JREALTIME_ANALYTICS_SERVICE_' . strtoupper($analyticsService);
			$this->getIcon ( 'index.php?option=com_jrealtimeanalytics&amp;task=google.display', 'fas fa-plug', Text::_ ( $stringtextTitle ) );
		}
		
		// Access check.
		if ($this->user->authorise('jrealtime.webmasters', 'com_jrealtimeanalytics')) {
			$this->getIcon ( 'index.php?option=com_jrealtimeanalytics&amp;task=webmasters.display', 'fab fa-google', Text::_ ( 'COM_JREALTIME_GOOGLE_WEBMASTERS' ) );
		}
		
		// Access check.
		if ($this->user->authorise('core.admin', 'com_jrealtimeanalytics')) {
			$this->getIcon ( 'index.php?option=com_jrealtimeanalytics&amp;task=config.display', 'fas fa-cog', Text::_ ( 'COM_JREALTIME_CONFIG_ICON' ) );
		}
		
		$this->getIcon ( 'https://storejextensions.org/jrealtime_analytics_documentation.html', 'fas fa-question-circle', Text::_ ( 'COM_JREALTIME_HELP' ) );
		$contents = ob_get_clean ();
		
		// Assign reference variables
		$this->icons = $contents;
		$this->updatesData = $this->getModel ()->getUpdates ( $this->httpClient );
		$this->infoData = $infoData;
		$this->cParams = $this->getModel()->getState('cparams');
		$this->currentVersion = strval(simplexml_load_file(JPATH_COMPONENT_ADMINISTRATOR . '/jrealtimeanalytics.xml')->version);
		
		// Aggiunta toolbar
		$this->addDisplayToolbar ();
		
		// Output del template
		parent::display ();
	}
}