<?php

namespace SGalinski\SgNews\Controller;

/***************************************************************
 *  Copyright notice
 *
 *  (c) sgalinski Internet Services (https://www.sgalinski.de)
 *
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/

use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;

/**
 * Controller that handles the page browser.
 */
class PageBrowserController extends ActionController {
	/**
	 * @var string
	 */
	protected $pageParameterName = '';

	/**
	 * @var int
	 */
	protected $numberOfPages = 0;

	/**
	 * @var int
	 */
	protected $currentPage = 0;

	/**
	 * @var int
	 */
	protected $pagesBefore = 0;

	/**
	 * @var int
	 */
	protected $pagesAfter = 0;

	/**
	 * @var bool
	 */
	protected $enableMorePages = FALSE;

	/**
	 * @var bool
	 */
	protected $enableLessPages = FALSE;

	/**
	 * Renders the index view.
	 *
	 * @param int $currentPage
	 * @return void
	 */
	public function indexAction($currentPage = 0) {
		$this->currentPage = (int) $currentPage;

		$this->initializeConfiguration();
		$this->addDataToView();
	}

	/**
	 * Initializes the configuration.
	 *
	 * @return    void
	 */
	protected function initializeConfiguration() {
		$this->pageParameterName = 'tx_sgnews_pagebrowser[currentPage]';
		$this->numberOfPages = (int) $this->settings['numberOfPages'];
		$this->pagesBefore = (int) $this->settings['pagesBefore'];
		$this->pagesAfter = (int) $this->settings['pagesAfter'];
		$this->enableMorePages = (bool) $this->settings['enableMorePages'];
		$this->enableLessPages = (bool) $this->settings['enableLessPages'];
	}

	/**
	 * Adds the necessary data to the view.
	 *
	 * @return void
	 */
	protected function addDataToView() {
		if ($this->numberOfPages <= 1) {
			$this->view = NULL;
			return;
		}

		$pageLinks = [];
		$start = \max($this->currentPage - $this->pagesBefore, 0);
		$end = \min($this->numberOfPages, $this->currentPage + $this->pagesAfter + 1);
		for ($i = $start; $i < $end; $i++) {
			$pageLinks[] = [
				'number' => $i + 1,
				'link' => $this->getPageLink($i),
				'isCurrentPage' => $i === $this->currentPage,
			];
		}

		$this->view->assignMultiple(
			[
				'enableLessPages' => $this->enableLessPages,
				'enableMorePages' => $this->enableMorePages,
				'previousLink' => $this->getPageLink($this->currentPage - 1),
				'nextLink' => $this->getPageLink($this->currentPage + 1),
				'enableLessPagesLink' => $this->getPageLink($this->currentPage - $this->pagesBefore - 1),
				'enableMorePagesLink' => $this->getPageLink($this->currentPage + $this->pagesAfter + 1),
				'pageLinks' => $pageLinks,
				'prevPageExist' => $this->currentPage > 0,
				'showLessPages' => ($this->currentPage - $this->pagesBefore) > 0,
				'showNextPages' => ($this->currentPage + $this->pagesAfter + 1) < $this->numberOfPages,
				'nextPageExist' => $this->currentPage < ($this->numberOfPages - 1),
			]
		);
	}

	/**
	 * Generates page link. Keeps all current URL parameters except for cHash and tx_pagebrowse_pi1[page].
	 *
	 * @param int $page Page number starting from 1
	 * @return string
	 */
	protected function getPageLink($page): string {
		return $this->uriBuilder->reset()->setAddQueryString(TRUE)
		    ->uriFor('index', ['currentPage' => $page,]);
	}
}