<?php 
namespace JExtstore\Component\JChat\Site\View\Conference;

/**
 * @author Joomla! Extensions Store
 * @package JCHAT::FORM::components::com_jchat
 * @subpackage views
 * @subpackage form
 * @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 JExtstore\Component\JChat\Administrator\Framework\View as JChatView;

/**
 * View for JS client
 *
 * @package JCHAT::FORM::components::com_jchat
 * @subpackage views
 * @subpackage form
 * @since 2.5
 */
class HtmlView extends JChatView {
	// Template view variables
	protected $menuTitle;
	protected $activeMenu;
	protected $cparams;
	protected $myUsername;
	protected $params;
	
	/**
	 * 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 ();
		if(!$this->activeMenu) {
			$pmItemid = $this->app->getMenu()->getDefault()->id;
		} else {
			$pmItemid = $this->activeMenu->id;
		}
		$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_conference_chain=" . $this->cparams->get('conference_chain', 1) . ";" .
														  "var jchat_conference_autoaccept=" . $this->cparams->get('conference_autoaccept', 0) . ";" .
														  "var jchat_show_search=" . $this->cparams->get('show_search', 1) . ";" .
														  "var jchat_hide_sidebar=" . $this->cparams->get('hide_sidebar', 0) . ";" .
														  "var jchat_conference_access_guest=" . $this->cparams->get('conference_access_guest', 0) . ";" .
														  "var jchat_hide_own_webcam_mode=" . $this->cparams->get('hide_own_webcam_mode', 0) . ";");
		
		$userFieldName = $this->cparams->get('usefullname', 'username');
		$this->myUsername =  $this->user->{$userFieldName};
		
		// Scripts loading
		$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()->registerAndUseStyle ( 'jchat.conference', 'components/com_jchat/css/conference.css');
		
		// Inject js translations
		$translations = array('COM_JCHAT_END_VIDEOCONFERENCE',
							  'COM_JCHAT_NOACCESS_VIDEOCONFERENCE',
							  'COM_JCHAT_SAFARI_WEBRTC_SHORT_SUPPORT');
		$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_CONFERENCE_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'));
		}
	}
}