<?php
namespace JExtstore\Component\JRealtimeAnalytics\Administrator\View\Webmasters;
/**
 * @package JREALTIME::GOOGLE::administrator::components::com_jrealtimeanalytics
 * @subpackage views
 * @subpackage google
 * @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\Language\Text;
use Joomla\CMS\Uri\Uri;
use JExtstore\Component\JRealtimeAnalytics\Administrator\Framework\Helpers\Toolbars as ToolbarHelper;
use JExtstore\Component\JRealtimeAnalytics\Administrator\Framework\View as JRealtimeView;

/**
 * @package JREALTIME::GOOGLE::administrator::components::com_jrealtimeanalytics
 * @subpackage views
 * @subpackage google
 * @since 2.6
 */
class HtmlView extends JRealtimeView {
	// Template view variables
	protected $dates;
	protected $globalConfig;
	protected $timeZoneObject;
	protected $lists;
	protected $googleData;
	protected $isLoggedIn;
	protected $statsDomain;
	protected $errorsDomain;
	protected $hasOwnCredentials;
	
	/**
	 * Add the page title and toolbar.
	 *
	 * @since	1.6
	 */
	protected function addDisplayToolbar() {
		$user = $this->app->getIdentity();
		ToolbarHelper::title( Text::_( 'COM_JREALTIME_GOOGLE_WEBMASTERS_TOOLS' ), 'jrealtimeanalytics' );
		
		// Store logged in status in session
		if($this->isLoggedIn) {
			ToolbarHelper::custom('webmasters.displayxls', 'download', 'download', 'COM_JREALTIME_EXPORTXLS', false);
			ToolbarHelper::custom('webmasters.deleteEntity', 'lock', 'lock', 'COM_JREALTIME_GOOGLE_LOGOUT', false);
		}
		
		ToolbarHelper::custom('cpanel.display', 'home', 'home', 'COM_JREALTIME_CPANEL', false);
	}
	
	/**
	 * Default display listEntities
	 *        	
	 * @access public
	 * @param string $tpl
	 * @return void
	 */
	public function display($tpl = null) {
		$model = $this->getModel();
		
		// Get main records
		$lists = $model->getLists();
		
		// Check the Google stats type and retrieve stats data accordingly, supported types are 'analytics' and 'webmasters'
		$googleData = $model->getDataWebmasters();
		if(!$this->getModel()->getState('loggedout')) {
			$tpl = 'webmasters';
		}
		
		$this->loadJQuery($this->document);
		$this->loadJQueryUI($this->document); // Required for calendar feature
		$this->loadBootstrap($this->document);
		$this->document->getWebAssetManager()->registerAndUseScript ( 'jrealtime.tablesorter', 'administrator/components/com_jrealtimeanalytics/js/libraries/tablesorter/jquery.tablesorter.js', [], [], ['jquery']);
		$this->document->getWebAssetManager()->registerAndUseScript ( 'jrealtime.google', 'administrator/components/com_jrealtimeanalytics/js/google.js', [], [], ['jquery']);
		$this->document->getWebAssetManager()->registerAndUseScript ('jrealtime.chartjs', 'administrator/components/com_jrealtimeanalytics/js/chartjs.min.js' );
		
		// Inject js translations
		$translations = array(
				'COM_JREALTIME_GOOGLE_WEBMASTERS_CHARTS_KEYWORD',
				'COM_JREALTIME_GOOGLE_WEBMASTERS_CHARTS_PAGE',
				'COM_JREALTIME_GOOGLE_WEBMASTERS_CLICKS',
				'COM_JREALTIME_GOOGLE_WEBMASTERS_IMPRESSION',
				'COM_JREALTIME_GOOGLE_WEBMASTERS_CTR_PERCENTAGE',
				'COM_JREALTIME_GOOGLE_WEBMASTERS_POSITION'
		);
		$this->injectJsTranslations($translations, $this->document);
		
		$dates = array('start'=>$this->getModel()->getState('fromPeriod'), 'to'=>$this->getModel()->getState('toPeriod'));
		$this->dates = $dates;
		$this->globalConfig = $this->app->getConfig();
		$this->timeZoneObject = new \DateTimeZone($this->globalConfig->get('offset'));
		$this->document->getWebAssetManager()->addInlineScript("var jrealtime_baseURI='" . Uri::root() . "';");
		$this->lists = $lists;
		$this->googleData = $googleData;
		$this->isLoggedIn = $this->getModel()->getToken();
		$this->statsDomain = $this->getModel()->getState('stats_domain', Uri::root());
		$this->errorsDomain = preg_match('/^http/i', $this->statsDomain) ? $this->statsDomain . '/' : Uri::getInstance()->getScheme() . '://' . $this->statsDomain . '/';
		$this->hasOwnCredentials = $this->getModel()->getState('has_own_credentials', false);
		$this->option = $this->getModel ()->getState ( 'option' );
		
		// Aggiunta toolbar
		$this->addDisplayToolbar();
		
		parent::display ($tpl);
	}
}