<?php 
namespace JExtstore\Component\JChat\Site\View\Form;

/**
 * @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\Factory;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\Language\Text;
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 1.0
 */
class HtmlView extends JChatView {
	// Template view variables
	protected $menuTitle;
	protected $activeMenu;
	protected $cparams;
	protected $userInfo;
	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 ();
		$this->cparams = $this->getModel ()->getState ( 'cparams' );
		if (isset ( $this->activeMenu )) {
			$this->menuTitle = $this->activeMenu->title;
		}
		
		$this->userInfo = $this->get('Data')[0];
		
		// If user is a guest already joined to chat or user logged
		if($this->cparams->get('guestenabled', 0) != 2) {
			$tpl = 'noavailable';
		} elseif($this->user->id) {
			$tpl = 'joined';
		} else {
			$this->loadJQuery($this->doc, false);
			$this->loadFrontendValidation($this->doc);
			
			$this->doc->getWebAssetManager()->addInlineScript("var jchat_meeting_url_redirection=" . $this->cparams->get('meeting_url_redirection', 0) . ";" .
															 "var jchat_meeting_url_redirect='" . Text::_($this->cparams->get('meeting_url_redirect', ''), true) . "';");
			
			// 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.form', 'components/com_jchat/js/form.js', [], ['defer'=>$defer, 'async'=>$async], ['jquery']);
			$this->doc->getWebAssetManager()->registerAndUseStyle ( 'jchat.form', 'components/com_jchat/css/form.css');
			
			// Inject js translations
			$translations = array('COM_JCHAT_FORM_JOINEDCHAT',
								  'COM_JCHAT_FORM_JOINED',
								  'COM_JCHAT_EXIT_CHAT',
								  'COM_JCHAT_MEETING_JOINEDMEETING',
								  'COM_JCHAT_MEETING_JOINED',
								  'COM_JCHAT_EXIT_MEETING',
								  'COM_JCHAT_VALIDATION_ERROR_ANTISPAM',
								  'COM_JCHAT_VALIDATION_ERROR_MOBILE_EXCLUDED',
								  'COM_JCHAT_PRIVACY_POLICY_LABEL_REQUIRED'
			);
			$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', 'Chat form');
		
		$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'));
		}
	}
}