<?php
namespace JExtstore\Component\JRealtimeAnalytics\Administrator\Controller;
/**
 * @package JREALTIMEANALYTICS::EVENTSTATS::administrator::components::com_jrealtimeanalytics
 * @subpackage controllers
 * @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 Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use JExtstore\Component\JRealtimeAnalytics\Administrator\Framework\Controller as JRealtimeController;
use JExtstore\Component\JRealtimeAnalytics\Administrator\Framework\Renderers\Adapter\Pdf as RenderersAdapterPdf;
use JExtstore\Component\JRealtimeAnalytics\Administrator\Framework\Renderers\Adapter\Csv as RenderersAdapterCsv;
use JExtstore\Component\JRealtimeAnalytics\Administrator\Framework\Renderers\Adapter\Xls as RenderersAdapterXls;

/**
 * Main controller
 * @package JREALTIMEANALYTICS::EVENTSTATS::administrator::components::com_jrealtimeanalytics
 * @subpackage controllers
 * @since 2.0
 */
class EventstatsController extends JRealtimeController {
	/**
	 * Set model state from session userstate
	 * @access protected
	 * @param string $scope
	 * @return object
	 */
	protected function setModelState($scope = 'default', $ordering = true): object {
		$option = $this->option;
	
		// Get default model
		$defaultModel = $this->getModel();
	
		$fromPeriod = $this->getUserStateFromRequest( "$option.$scope.fromperiod", 'fromperiod');
		$toPeriod = $this->getUserStateFromRequest( "$option.$scope.toperiod", 'toperiod');
		$filter_state = $this->getUserStateFromRequest ( "$option.$scope.filterstate", 'filter_state', null );
		$filter_catid = $this->getUserStateFromRequest ( "$option.$scope.filtercatid", 'filter_catid', null );
		parent::setModelState($scope);
	
		// Set model state
		$defaultModel->setState('fromPeriod', $fromPeriod);
		$defaultModel->setState('toPeriod', $toPeriod);
		$defaultModel->setState('state', $filter_state); 
		$defaultModel->setState('catid', $filter_catid);
	
		return $defaultModel;
	}
	
	/**
	 * Default listEntities
	 * 
	 * @access public
	 * @param $cachable string
	 *       	 the view output will be cached
	 * @return void
	 */
	public function display($cachable = false, $urlparams = false) {
		// Set model state
		$defaultModel = $this->setModelState('eventstats');
		 
		// Parent construction and view display
		parent::display($cachable);
	}
	
	/**
	 * Manage entity apply/save after edit entity
	 *
	 * @access public
	 * @return bool
	 */
	public function saveEntity(): bool {
		$parentSave = parent::saveEntity();
		
		if($parentSave && $this->task == 'changeEntity') {
			$this->message = null;
		}
		
		return true;
	}
	
	/**
	 * Edit entity
	 *
	 * @access public
	 * @return void
	 */
	public function showEntity() {
		$idEntity = $this->app->getInput()->get ( 'id', null, 'int' );
		$model = $this->getModel ();
		$model->setState ( 'option', $this->option );
	
		// Try to load record from model
		if ( !$record = $model->loadEntity ( $idEntity )) {
			// Model set exceptions for something gone wrong, so enqueue exceptions and levels on application object then set redirect and exit
			$modelExceptions = $model->getExceptions ();
			foreach ( $modelExceptions as $exception ) {
				$this->app->enqueueMessage ( $exception->getMessage (), $exception->getExceptionLevel () );
			}
			$this->setRedirect ( "index.php?option=" . $this->option . "&task=" . $this->name . ".display", Text::_('COM_JREALTIME_ERROR_EDITING')  );
			return false;
		}
	
		// Additional model state setting
		$model->setState ( 'option', $this->option );
	
		// Get view and pushing model
		$view = $this->getView ('eventstats', 'html', '', array('base_path' => $this->basePath, 'layout' => 'default'));
		$view->setModel ( $model, true );
	
		if($this->task === 'showEntitypdf') {
			$tpl = 'showpdf';
		}
		if($this->task === 'showEntitycsv') {
			$tpl = 'showcsv';
		}
		if($this->task == 'showEntityxls') {
			$tpl = 'showxls';
		}
		
		//Creazione buffer output
		ob_start ();
		// Call edit view
		$view->showEntity ( $record, $tpl );
		$bufferContent = ob_get_contents ();
		ob_end_clean ();
		
		// Choose if plain HTML or PDF conversion is required based on tasks instead of document format
		switch ($this->task) {
			case 'showEntitypdf':
				// Do conversion to PDF format using adapter
				$pdfRenderer = new RenderersAdapterPdf();
				$pdfRenderer->renderContent ( $bufferContent, $model, 'event' . $idEntity . '_report_' );
				break;
			case 'showEntitycsv':
				$csvRenderer = new RenderersAdapterCsv();
				$csvRenderer->renderContent ( $bufferContent, $model, 'event' . $idEntity . '_report_' );
				break;
			case 'showEntityxls':
				$xlsRenderer = new RenderersAdapterXls();
				$xlsRenderer->renderContent ( $bufferContent, $model, 'event' . $idEntity . '_report_' );
				break;
			default:
				echo $bufferContent;
		}
	}
	
	/**
	 * Manage entity apply/save after edit entity
	 *
	 * @access public
	 * @return boolean
	 */
	public function changeEntity() {
		$context = implode ( '.', array (
				$this->option,
				strtolower ( $this->getName () ),
				'errordataload'
		) );
	
		// Store data for session recover
		$this->app->setUserState ( $context, $this->requestArray );
		$this->setRedirect ( "index.php?option=" . $this->option . "&task=" . $this->name . ".editEntity");

		return true;
	}
	
	/**
	 * Manage cancel edit for entity and unlock record checked out
	 *
	 * @access public
	 * @return bool
	 */
	public function resetEntity(): bool {
		$cids = $this->app->getInput()->get ( 'cid', array (), 'array' );
		// Load della model e checkin before exit
		$model = $this->getModel ();
		
		if (! $model->resetEntity ( $cids )) {
			// Model set exceptions for something gone wrong, so enqueue exceptions and levels on application object then set redirect and exit
			$modelException = $model->getException ( null, false );
			$this->app->enqueueMessage ( $modelException->getMessage (), $modelException->getExceptionLevel () );
			$this->setRedirect ( "index.php?option=" . $this->option . "&task=" . $this->name . ".display", Text::_ ( 'COM_JREALTIME_ERROR_DELETE' ) );
			return false;
		}
		
		$this->setRedirect ( "index.php?option=" . $this->option . "&task=" . $this->name . ".display", Text::_ ( 'COM_JREALTIME_EVENT_STATS_DELETED' ) );
		
		return true;
	}
	
	/**
	 * Redirects to categories bypassing setModelState
	 *
	 * @access public
	 * @return void
	 */
	public function showCategories() {
		// Execute only redirect
		$this->setRedirect ( "index.php?option=" . $this->option . "&task=categories.display" );
	}
	
	/**
	 * Class Constructor
	 * 
	 * @access public
	 * @return Object&
	 */
	public function __construct($config = array(), ?MVCFactoryInterface $factory = null, $app = null, $input = null) {
		parent::__construct($config, $factory, $app, $input);
		
		// Register Extra tasks
		$this->registerTask ( 'moveorder_up', 'moveOrder' );
		$this->registerTask ( 'moveorder_down', 'moveOrder' );
		$this->registerTask ( 'applyEntity', 'saveEntity' );
		$this->registerTask ( 'saveEntity2New', 'saveEntity' );
		$this->registerTask ( 'unpublish', 'publishEntities' );
		$this->registerTask ( 'publish', 'publishEntities' );
		$this->registerTask ( 'showEntitycsv', 'showEntity' );
		$this->registerTask ( 'showEntityxls', 'showEntity' );
		$this->registerTask ( 'showEntitypdf', 'showEntity' );
	}
}