<?php

namespace App\Mvc;

abstract class View_Window_PaginatedListing_ListAbstract extends ViewAbstract
{

	/**
	 * The default results per page list (can be overwritten in the concrete classes).
	 * @var array
	 */
	protected $resultsPerPageList= [5, 10, 15, 20, 25, 30];

	/**
	 * Hint set if the total number of results is cached or not
	 * @var bool
	 */
	protected $totalRecordsCountIsCached = false;


	/**
	 * Get the listing object, initialized & ready for display
	 * @throws \Exception
	 */
	protected function getListObject()
	{
		$listObj = $this->createListingObj();
		$listObj->setColumns($this->list_getThDetails());

		$listObj->setResultsPerPageList($this->resultsPerPageList);

		$listObj->setRecords($this->input['recordsList']);
		$listObj->setRecordsCountTotal($this->input['totalRecordsCount']);
		$listObj->setRecordsCountTotalIsCached($this->totalRecordsCountIsCached);

		$listObj->importFromListingParams($this->input['listingParams']);
		$listObj->setListingSelection($this->input['listingSelection']);

		return $listObj;
	}

	/**
	 * Create the listing object that will be used by the class
	 * @return \App\Mvc\View\Listing\Simple
	 */
	protected function createListingObj()
	{
		return \Theme::getInstance()->listing()->createListingObj();
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\ViewAbstract::render()
	 */
	public function render()
	{
		$this->setOutputVar('listObj', $this->getListObject());

		return $this->render_smarty(\Theme::getInstance()->listing()->getListingBodyTemplate());
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\ViewAbstract::assignSmartyDefaultVars()
	 */
	protected function assignSmartyDefaultVars($smarty)
	{
		$smarty->assign('messagesCollector', $this->messagesCollector);
		return parent::assignSmartyDefaultVars($smarty);
	}

	/**
	 * Render the actual records, the <TR/> tags
	 */
	public function renderRecords()
	{
		$this->render_prepare(\Engine\Smarty\SmartyMultiton::getInstance());

		// run plugin trigger for
		\Engine\Plugins\Manager::getInstance()->executeEvent("MVC-BeforeViewRender-[{$this->controller->getModule()}-{$this->controller->getAction()}]", ['view'=>$this]); // TODO: this trigger is defined in two places

		$this->getListObject()->templateInitialize();

		$filteredModuleOptions = $this->controller->filterOptionsForSections($this->controller->getAllDefinedOptions(), ['row']);
		$filteredModuleMenuComponent = $this->controller->buildOptionsComponent($filteredModuleOptions)->setOption('staticRender', true);

		$recordsHtml = $this->getListObject()->renderRecords([
			'filteredModuleOptions' => 			$filteredModuleOptions,
			'filteredModuleMenuComponent' => 	$filteredModuleMenuComponent,
		]);
		$this->getListObject()->templateCleanup();

		return $recordsHtml . "<script type='text/javascript'>{$filteredModuleMenuComponent->renderJs(\Engine\Theme\JSLoader\JSLoader::getInstance())}</script>";
	}


	/**
	 * Render the pagination JSON object. This will be sent down to the browser via a header
	 */
	public function getPaginationData()
	{
		return $this->getListObject()->renderJsonPagination();
	}


	/**
	 * Returns an array for the listing table head.
	 * @return Array  - the listing table head
	 */
	abstract protected function list_getThDetails() ;

}