<?php
namespace Meramo\Mrmbepages\Controller;

use Meramo\Mrmbepages\Paginator\Paginator;
use \TYPO3\CMS\Backend\View\BackendTemplateView;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use \TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
use \TYPO3\CMS\Extbase\Utility\LocalizationUtility;

/**
 * Abstract backend controller provides functions for all backend controllers
 */
class AbstractBackendController extends ActionController
{
    /**
     * Backend Template Container
     * 
     * @var BackendTemplateView
     */
    protected $view;

    /**
     * Backend Template Container
     * 
     * @var string
     * @return string
     */
    protected $defaultViewObjectName = \TYPO3\CMS\Backend\View\BackendTemplateView::class;

    protected function translate($key, $arguments = null): string
    {
        return LocalizationUtility::translate(
            $key,
            'mrmbepages',
            $arguments
        );
    }

    protected function resolveView(): BackendTemplateView
    {
        $view = parent::resolveView();
        
        $view->assignMultiple([
            'extensionName' => $this->request->getControllerExtensionName(),
            'controllerName' => $this->request->getControllerName(),
            'actionName' => $this->request->getControllerActionName()
        ]);
        return $view;
    }

    protected function generateMenu()
    {
        $menuItems = [
           'dashboardA' => [
                'controller' => 'Dashboard',
                'action' => 'index',
                'label' => $this->translate('mrmbepage.dashboard.headline')
            ],
            'dashboardB' => [
                'controller' => 'Dashboard',
                'action' => 'index',
                'label' => $this->translate('mrmbepage.dashboard.headline')
            ],

        ];

        $menu = $this->view->getModuleTemplate()->getDocHeaderComponent()->getMenuRegistry()->makeMenu();
        $menu->setIdentifier('mrmbepagesModuleMenu');
        foreach($menuItems as $menuItemConfig) {
            $isActive = false;
            if($this->request->getControllerName() === $menuItemConfig['controller']) {
                if($this->request->getControllerActionName() === $menuItemConfig['action']) {
                    $isActive = true;
                }
            }
            $menuItem = $menu->makeMenuItem()
                ->setTitle($menuItemConfig['label'])
                ->setHref($this->getHref($menuItemConfig['controller'], $menuItemConfig['action']))
                ->setActive($isActive);
            $menu->addMenuItem($menuItem);
        }
        $this->view->getModuleTemplate()->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
    }

    protected function getHref($controller, $action, $parameters = [])
    {
        $uriBuilder = $this->objectManager->get(UriBuilder::class);
        $uriBuilder->setRequest($this->request);
        return $uriBuilder->reset()->uriFor($action, $parameters, $controller);
    }

    protected function buildPagination(array $data, $itemsPerPage, $currentPage) 
    {
        $paginator = GeneralUtility::makeInstance(Paginator::class);
        $paginator->setData($data);
        $paginator->setItemsPerPage($itemsPerPage);
        $paginator->setCurrentPage($currentPage);

        $totalRecords = $paginator->getTotalRecords();
        $totalPages = $paginator->getTotalPages();
        $pageSize = $paginator->getItemsPerPage();
        $currentPage = $paginator->getCurrentPage();
        if($currentPage == 1) {
            $previousPage = 0;
        } else {
            $previousPage = $paginator->getPreviousPage();
        }
        $previousPage = $paginator->getPreviousPage();
        $nextPage = $paginator->getNextPage();
        $offSet = $paginator->getOffSet();
        $paginatedData = $paginator->getPaginatedData($data, $offSet, $currentPage);
        $firstPage = min(range(1, $totalPages));
        $lastPage = max(range(1, $totalPages));
        $isFirstPage = $currentPage == min(range(1, $totalPages));
        $isLastPage = $currentPage == $lastPage = max(range(1, $totalPages));

        return [
            'paginatedData' => $paginatedData,
            'totalRecords' => $totalRecords,
            'totalPages' => $totalPages,
            'pageSize' => $pageSize,
            'currentPage' => $currentPage,
            'previousPage' => $previousPage,
            'nextPage' => $nextPage,
            'firstPage' => $firstPage,
            'lastPage' => $lastPage,
            'pageRange' => range($firstPage, $lastPage),
            'isFirstPage' => $isFirstPage,
            'isLastPage' => $isLastPage,

        ];
    
    }
}
