<?php
namespace JExtstore\Component\JRealtimeAnalytics\Administrator\View\Google;
/**
 * @package JREALTIMEANALYTICS::GOOGLE::administrator::components::com_jrealtimeanalytics
 * @subpackage views
 * @subpackage google
 * @author Joomla! Extensions Store
 * @copyright (C) 2014 - 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 JExtstore\Component\JRealtimeAnalytics\Administrator\Framework\Helpers\Toolbars as ToolbarHelper;
use JExtstore\Component\JRealtimeAnalytics\Administrator\Framework\View as JRealtimeView;

/**
 * @package JREALTIMEANALYTICS::GOOGLE::administrator::components::com_jrealtimeanalytics
 * @subpackage views
 * @subpackage google
 * @since 2.5
 */
class HtmlView extends JRealtimeView {
	// Template view variables
	protected $lists;
	protected $googleData;
	protected $isLoggedIn;
	
	/**
	 * Add the page title and toolbar.
	 *
	 * @since	1.6
	 */
	protected function addDisplayToolbar() {
		$user = $this->app->getIdentity();
		$analyticsService = $this->getModel()->getComponentParams()->get('analytics_service', 'google');
		$toolbarTitle = $analyticsService == 'matomo' ? Text::_('COM_JREALTIME_MATOMO_ANALYTICS') : Text::_('COM_JREALTIME_GOOGLE_ANALYTICS');
		ToolbarHelper::title($toolbarTitle, 'jrealtimeanalytics');
		
		// Store logged in status in session
		if($this->isLoggedIn && $analyticsService != 'matomo') {
			ToolbarHelper::custom('google.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) {
		// Get main records
		$model = $this->getModel();
		
		$lists = $model->getLists();
		
		$analyticsService = $this->getModel()->getComponentParams()->get('analytics_service', 'google');
		if($analyticsService == 'matomo') {
			$googleData = $model->getDataMatomo();
		} else {
			$gaApi = $this->getModel()->getComponentParams()->get('analytics_api', 'analytics');
			switch($gaApi) {
				// Retrieve data using the Analitics API
				case 'analytics':
					$googleData = $model->getDataAnalytics();
					break;
					
					// Retrieve data using the Reporting API
				case 'reporting':
					$googleData = $model->getDataReporting();
					break;
					
					// Retrieve data using the DATA GA4 API
				case 'data':
				default:
					$googleData = $model->getDataData();
					break;
			}
		}
		
		$this->loadJQuery($this->document);
		$this->loadBootstrap($this->document);
		
		$this->document->getWebAssetManager()->addInlineStyle('@media (max-width: 1480px) { body.admin.com_jrealtimeanalytics { min-width: 1480px; }}');
		
		$dates = array('start'=>$this->getModel()->getState('fromPeriod'), 'to'=>$this->getModel()->getState('toPeriod'));
		$this->lists = $lists;
		$this->googleData = $googleData;
		$this->isLoggedIn = $analyticsService == 'matomo' ? false : $this->getModel()->getToken();
		$this->option = $this->getModel ()->getState ( 'option' );
		
		// Aggiunta toolbar
		$this->addDisplayToolbar();
		
		parent::display ();
	}
}