<?php 
namespace JExtstore\Component\JChat\Site\View\Livestreaming;

/**
 * @author Joomla! Extensions Store
 * @package JCHAT::LIVESTREAMING::components::com_jchat
 * @subpackage views
 * @subpackage livestreaming
 * @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\Factory;
use Joomla\CMS\Uri\Uri;
use Joomla\Registry\Registry;
use Joomla\CMS\Router\Route;
use JExtstore\Component\JChat\Administrator\Framework\View as JChatView;

/**
 * View for JS client
 *
 * @package JCHAT::LIVESTREAMING::components::com_jchat
 * @subpackage views
 * @subpackage livestreaming
 * @since 2.48
 */
class HtmlView extends JChatView {
	// Template view variables
	protected $menuTitle;
	protected $activeMenu;
	protected $cparams;
	protected $myUsername;
	protected $params;
	protected $livestreamingUniqueIdentifier;
	protected $livestreamingExists;
	protected $recordingAvailable;
	
	/**
	 * Return application/json response to JS client APP
	 * Replace $tpl optional param with $userData contents to inject
	 *        	
	 * @access public
	 * @param string $userData
	 * @return void
	 */
	public function display($tpl = null) {
		$this->menuTitle = null;
		$this->activeMenu = $this->app->getMenu()->getActive ();
	
		$this->cparams = $this->getModel ()->getComponentParams();
		if (isset ( $this->activeMenu )) {
			$this->menuTitle = $this->activeMenu->title;
		}

		$this->loadJQuery($this->doc, false);
		
		// Inject js vars
		$this->doc->getWebAssetManager()->addInlineScript("var jchat_recorded_media_chunk_duration=" . $this->cparams->get('recorded_media_chunk_duration', 30) . ";" .
														  "var jchat_retry_interval=" . $this->cparams->get('retry_interval', 5) . ";" .
														  "var jchat_hide_sidebar=" . $this->cparams->get('hide_sidebar', 0) . ";" .
														  "var jchat_livestreaming_access_guest=" . $this->cparams->get('livestreaming_access_guest', 0) . ";" .
														  "var jchat_conference_access_guest=" . $this->cparams->get('livestreaming_access_guest', 0) . ";" .
														  "var jchat_livestreaming_iswatcher=" . ($this->getModel ()->getState('livestreaming_iswatcher') ? 1 : 0) . ";" . 
														  "var jchat_livestreaming_broadcaster_sessionid='" . $this->app->input->getString('livestreamingid', '') . "';" .
														  "var jchat_livestreaming_share_link='" . Route::_('index.php?option=com_jchat&view=livestreaming&livestreamingid=' . $this->getModel ()->getState('livestreamingid'), true, Route::TLS_IGNORE, true) . "';" .
														  "var jchat_livestreaming_broadcaster_name='" . addcslashes($this->getModel()->getBroadcasterName(), "'") . "';" .
														  "var jchat_livestreaming_stale=" . ($this->getModel ()->checkIfStreamingStale() ? 0 : 1) . ";" .
														  "var jchat_livestreaming_ismine=" . ($this->getModel ()->getState('livestreaming_ismine') ? 1 : 0) . ";" .
														  "var jchat_current_itemid=" . $this->activeMenu->id . ";");
		
		$userFieldName = $this->cparams->get('usefullname', 'username');
		$this->myUsername =  $this->user->{$userFieldName};
		$this->livestreamingUniqueIdentifier = $this->app->input->getString('livestreamingid', $this->getModel ()->getState('livestreamingid'));
		$this->livestreamingExists = $this->getModel()->getState('livestreaming_exists');
		$this->recordingAvailable = $this->getModel()->checkIfRecordingAvailable();
		
		$defer = $this->cparams->get('scripts_loading', null) == 'defer' ? true : false;
		$async = $this->cparams->get('scripts_loading', null) == 'async' ? true : false;
		$this->doc->getWebAssetManager()->registerAndUseScript ( 'jchat.conference', 'components/com_jchat/js/conference.js', [], ['defer'=>$defer, 'async'=>$async], ['jquery']);
		$this->doc->getWebAssetManager()->registerAndUseScript ( 'jchat.livestreaming', 'components/com_jchat/js/livestreaming.js', [], ['defer'=>$defer, 'async'=>$async], ['jquery', 'jchat.conference']);
		$this->doc->getWebAssetManager()->registerAndUseStyle ( 'jchat.conference', 'components/com_jchat/css/conference.css');
		
		// Inject js translations
		$translations = array('COM_JCHAT_ENTER_LIVESTREAMING',
							  'COM_JCHAT_EXIT_LIVESTREAMING',
							  'COM_JCHAT_SHARE_LIVESTREAMING',
							  'COM_JCHAT_NOACCESS_LIVESTREAMING',
							  'COM_JCHAT_SAFARI_WEBRTC_SHORT_SUPPORT',
							  'COM_JCHAT_END_VIDEOCONFERENCE',
							  'COM_JCHAT_NOACCESS_VIDEOCONFERENCE',
							  'COM_JCHAT_NOACCESS_LIVESTREAMING',
							  'COM_JCHAT_START_LIVESTREAMING',
							  'COM_JCHAT_END_LIVESTREAMING',
							  'COM_JCHAT_WATCH_LIVESTREAMING',
							  'COM_JCHAT_STOP_WATCHING_LIVESTREAMING',
							  'COM_JCHAT_PAUSE_WATCHING_LIVESTREAMING',
							  'COM_JCHAT_LIVESTREAMING_IS_ENDED',
							  'COM_JCHAT_LIVESTREAMING_TERMINATE_BEFORE_CLOSING',
							  'COM_JCHAT_LIVESTREAMING_PREVENT_OTHER_TABS',
							  'COM_JCHAT_LIVESTREAMING_STILL_NOT_AVAILABLE',
							  'COM_JCHAT_WATCH_RECORDED_STREAMING',
							  'COM_JCHAT_RECORDEDSTREAMING_IS_ENDED',
							  'COM_JCHAT_LIVE_STREAMING_STALE'
		);
		$this->injectJsTranslations($translations, $this->doc);
		
		// Add meta info
		$this->_prepareDocument();
		
		parent::display($tpl);
	}
	
	/**
	 * Prepares the document
	 */
	protected function _prepareDocument() {
		$document = $this->app->getDocument();
		$menus = $this->app->getMenu();
		$title = null;
	
		// Because the application sets a default page title,
		// we need to get it from the menu item itself
		$menu = $menus->getActive();
		if(is_null($menu)) {
			return;
		}
	
		$this->params = new Registry();
		$menuParams = $menu->getParams()->toString();
		$this->params->loadString($menuParams);
	
		$title = $this->params->get('page_title', Text::_('COM_JCHAT_LIVESTREAMING_VIEW'));
		
		$this->setDocumentTitle($title);
	
		if ($this->params->get('menu-meta_description')) {
			$document->setDescription($this->params->get('menu-meta_description'));
		}
		
		if ($this->params->get('robots')) {
			$document->setMetadata('robots', $this->params->get('robots'));
		}
	}
}