<?php
namespace Meramo\Mrmbepages\Paginator;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Utility\DebuggerUtility;

class Paginator
{
    /**
     * @var array $data
     */
    protected $data;

    /**
     * @var int $totalRecords
     */
    protected $totalRecords;

    /**
     * @var int $itemsPerPage
     */
    protected $itemsPerPage;

    /**
     * @var int @offSet
     */
    protected $offSet;

    /**
     * @var int $totalPages
     */
    protected $totalPages;

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

    /**
      * @var $nextPage
      */
    protected $nextPage;

    /**
      * @var $previousPage
      */
    protected $previousPage;
     

     /**
     * @var $paginatedData
     */
    protected $paginatedData;

    public function __construct(){}

    public function setData(array $data) {
        $this->data = $data;
    }

    public function getData()
    {
        return $this->data;
    }

    public function getTotalRecords(): int
    {
        $this->totalRecords = count($this->getData());
        
        return $this->totalRecords;
    }

    public function setItemsPerPage(int $itemsPerPage)
    {
        $this->itemsPerPage = $itemsPerPage;
    }

    public function getItemsPerPage(): int
    {
        return $this->itemsPerPage;
    }

    public function getTotalPages()
    {
        $this->totalPages = ceil($this->totalRecords / $this->itemsPerPage);

        return $this->totalPages;
    }

    public function getCurrentPage(): int
    {

        if($this->currentPage < 1) $this->currentPage = 1;

        if($this->currentPage > $this->totalPages) $this->currentPage = $this->totalPages;

        return (int) $this->currentPage;

    }

    public function setCurrentPage($currentPage)
    {
        $this->currentPage = $currentPage;
    }

    public function getNextPage():int
    {

        if($this->getCurrentPage() < $this->getTotalPages()) {
            $this->nextPage = (int) ($this->getCurrentPage() + 1);
        } else $this->nextPage = $this->getTotalPages();

        return $this->nextPage;
    }

    public function getPreviousPage()
    {
        if($this->currentPage > 1) {
            $this->previousPage = (int) $this->currentPage - 1;
        }
        return $this->previousPage;
    }

    public function getOffSet(): int
    {
        return $this->offSet =  ((int) $this->currentPage -1) * $this->pageSize;
    }

    public function getPaginatedData()
    {
        $offset = ((int) $this->getCurrentPage() -1) * $this->itemsPerPage;
        $this->paginatedData = array_slice($this->data, $offset, $this->itemsPerPage);

        return $this->paginatedData;
    }
       
}