<?php
namespace Meramo\Mrmbepages\Controller;

use Meramo\Mrmbepages\Domain\Repository\ActivepageRepository;
use Meramo\Mrmbepages\Controller\AbstractBackendController;
use TYPO3\CMS\Backend\View\BackendTemplateView;
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
use TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings;
use TYPO3\CMS\Core\Utility\GeneralUtility;


/**
 * Dashboard controller class
 */
class DashboardController extends AbstractBackendController
{
    /**
     * Activepage repository
     *
     * @var Meramo\Mrmbepages\Domain\Repository\ActivepageRepository activepageRepository;
     */
    protected $activepageRepository;

    /**
     * Import Active repository by dependency injection
     *
     * @param ActivepageRepository $activepageRepository
     */
    public function injectBlogRepository(\Meramo\Mrmbepages\Domain\Repository\ActivepageRepository  $activepageRepository): void
    {
         $this->activepageRepository = $activepageRepository;
    }

    /**
     * Initialize view
     *
     * @param ViewInterface
     */
    protected function initializeView(ViewInterface $view): void
    {
        if ($view instanceof BackendTemplateView) {
            /** @var BackendTemplateView $view */
            parent::initializeView($view);
            $this->generateMenu();
        }
    }

    /**
     * Initialize action
     */
    public function initializeAction(): void
    {
        $querySettings = $this->objectManager->get(Typo3QuerySettings::class);
        $querySettings->setRespectStoragePage(false);
        $querySettings->setIgnoreEnableFields(true);
        $this->activepageRepository->setDefaultQuerySettings($querySettings);
    }

    /**
     * Index action
     */
    public function indexAction()
    {
        $allActivepages = $this->activepageRepository->findActivePages();

        if(!$this->request->hasArgument('currentPage')) {
            $currentPage = 1;
        } else {
            $currentPage = $this->request->getArgument('currentPage');
        }

        $itemsPerPage = GeneralUtility::_GP('itemsPerPage') ?? 50;
        
        $paginations = $this->buildPagination($allActivepages, $itemsPerPage, $currentPage);
        //DebuggerUtility::var_dump($paginations);
  
        $pagination = [
            'pages' => $paginations['paginatedData'],
            'totalRecords' => $paginations['totalRecords'],
            'totalPages' => $paginations['totalPages'],
            'currentPage' => $paginations['currentPage'],
            'previousPage' => $paginations['previousPage'],
            'nextPage' => $paginations['nextPage'],
            'firstPage' => $paginations['firstPage'],
            'lastPage' => $paginations['lastPage'],
            'pageRange' => range($paginations['firstPage'], $paginations['lastPage']),
            'isFirstPage' => $paginations['isFirstPage'],
            'isLastPage' => $paginations['isLastPage'],
        ];

        $this->view->assign('pagination', $pagination);
    
    }

}