<?php

declare(strict_types=1);

namespace Medienreaktor\Courses\Controller;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Object\ObjectManager;

/**
 * This file is part of the "Courses" Extension for TYPO3 CMS.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * (c) 2023 Matthias Schieber <matthias.schieber@medienreaktor.de>, Medienreaktor
 */

/**
 * SubjectController
 */
class SubjectController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
{

    /**
     * subjectRepository
     *
     * @var \Medienreaktor\Courses\Domain\Repository\SubjectRepository
     */
    protected $subjectRepository = null;

    /**
     * action list
     *
     * @return string|object|null|void
     */

    /**
     * @var ConfigurationManagerInterface
     */
    protected $configurationManager;

    public function __construct(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager, \Medienreaktor\Courses\Domain\Repository\SubjectRepository $subjectRepository)
    {
        $this->configurationManager = $configurationManager;
        $this->subjectRepository = $subjectRepository;
    }

    public function listAction(): \Psr\Http\Message\ResponseInterface
    {
        $piFlexFormValues = $this->request->getAttribute('currentContentObject')->data['pi_flexform'];

        $flexFormValues = GeneralUtility::xml2array($piFlexFormValues);


        $heading = $flexFormValues['data']['sDEF']['lDEF']['settings.subjectHeading']['vDEF'];
        $linkTitle = $flexFormValues['data']['sDEF']['lDEF']['settings.subjectLinkTitle']['vDEF'];
        $link = $flexFormValues['data']['sDEF']['lDEF']['settings.subjectLinkToAll']['vDEF'];
        $subjectTypes = $flexFormValues['data']['sDEF']['lDEF']['settings.subjectTypes']['vDEF'];
        $subjectTypesByCategory = $flexFormValues['data']['sDEF']['lDEF']['settings.subjectTypesByCategory']['vDEF'];
        $pid = $flexFormValues['data']['sDEF']['lDEF']['settings.filterPid']['vDEF'];

        if($subjectTypes){
            $types = explode(",", $subjectTypes);

            foreach ($types as $type){
                $results[] = $this->subjectRepository->findByType($type);
            }
            foreach($results as $result){
                $subjects[] = $result[0];
            }
            if($subjectTypesByCategory != "none"){
                $this->view->assign('category', $subjectTypesByCategory);
            }

        }else if ($subjectTypesByCategory != "none"){
            $subjectIds = $this->subjectRepository->findByCategory($subjectTypesByCategory);
            foreach($subjectIds as $subjectId){
               $subject = $this->subjectRepository->findByImportId($subjectId);
               $subjects[] = $subject[0];
            }


            $this->view->assign('category', $subjectTypesByCategory);

        } else{

            $subjects = $this->subjectRepository->findAll();
        }

        $this->view->assign('page', $pid);
        $this->view->assign('imageLayout', $flexFormValues['data']['sDEF']['lDEF']['settings.subjectBigpicture']['vDEF']);
        $this->view->assign('subjects', $subjects);
        $this->view->assign('heading', $heading);
        $this->view->assign('link', $link);
        $this->view->assign('linkTitle', $linkTitle);
        return $this->htmlResponse();
    }
}
