<?php
namespace Meramo\Mrmbepages\Domain\Repository;

use Meramo\Mrmbepages\Domain\Model\Activepage;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Utility\DebugUtility;
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Persistence\Repository;
use TYPO3\CMS\Extbase\Utility\DebuggerUtility;
use TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper;
use \TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser;
use TYPO3\CMS\Extbase\Persistence\QueryInterface;


/**
 * Repository class Page
 */
class ActivepageRepository extends Repository
{
    /**
     * Datamapper
     *
     * 
     * @param \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper $dataMapper
     * 
     */
    protected $dataMapper;

    public function injectDataMapper(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper $dataMapper)
    {
        $this->dataMapper = $dataMapper;
    }

    /**
     * Objectmanager
     * 
     * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
     */
    protected $objectManager;
    public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager)
    {
        $this->objectManager = $objectManager;
    }
    


    /**
     * Returns selected pages from the pages table
     * 
     * @return QueryResult|array
     */
    public function findActivePages()
    {
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');

        $queryBuilder->getRestrictions()
            ->removeByType(HiddenRestriction::class);

        $result = $queryBuilder
            ->select('uid', 'starttime', 'endtime', 'title', 'roof', 'slug')
            ->from('pages')
            ->where(
                $queryBuilder->expr()->gt('starttime', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)),
                $queryBuilder->expr()->gt('endtime', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)),
                $queryBuilder->expr()->eq('red_status', $queryBuilder->createNamedParameter(12, \PDO::PARAM_INT)),
                $queryBuilder->expr()->eq('deleted', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)),
                $queryBuilder->expr()->eq('hidden', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT))

            )
            ->orderBy('endtime', 'ASC')
            ->groupBy('slug')
            ->execute();

        $activepageObjects = $this->dataMapper->map(Activepage::class, $result->fetchAll());

        return $activepageObjects;
    }

    public function generateSelectListOptions()
    {
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');

        $result = $queryBuilder
            ->select('slug')
            ->from('pages')
            ->where(
                $queryBuilder->expr()->gt('starttime', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)),
                $queryBuilder->expr()->gt('endtime', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)),
                $queryBuilder->expr()->eq('red_status', $queryBuilder->createNamedParameter(12, \PDO::PARAM_INT))
            )
            ->orderBy('endtime', 'ASC')
            ->execute();

            $selectOptions = [];

            while($row = $result->fetch()) {
                $slug  = $this->createOptionFromSlug($row['slug']);

                if(in_array($slug, $selectOptions)) {
                    continue;
                } else {
                    $selectOptions[] = $slug;
                    $arrayValues = array_values($selectOptions);
                }                          
            }
            return $arrayValues;

    }

    protected function createOptionFromSlug($value)
    { 
        if(substr($value, 0, 1) === '/') {
            $value = ltrim($value, '/');

            if(substr($value, 0, 1) === '_') {
                $value = ltrim($value, '_');
            }

            $data = explode('/', $value);
        }
        return $data[0];
    }
}
