<?php
namespace WapplerSystems\Meilisearch\Widget;

/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

use WapplerSystems\Meilisearch\ConnectionManager;
use WapplerSystems\Meilisearch\Domain\Search\ResultSet\SearchResultSetService;
use WapplerSystems\Meilisearch\Search;
use WapplerSystems\Meilisearch\System\Configuration\ConfigurationManager;
use WapplerSystems\Meilisearch\Mvc\Controller\SolrControllerContext;
use WapplerSystems\Meilisearch\System\Configuration\TypoScriptConfiguration;
use WapplerSystems\Meilisearch\Util;
use WapplerSystems\Meilisearch\Widget\WidgetRequest;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetController as CoreAbstractWidgetController;

/**
 * Class AbstractWidgetController
 *
 * @author Frans Saris <frans@beech.it>
 * @author Timo Hund <timo.hund@dkd.de>
 */
class AbstractWidgetController extends CoreAbstractWidgetController
{

    /**
     * @var array
     */
    protected $supportedRequestTypes = [WidgetRequest::class];

    /**
     * @var ConfigurationManager
     */
    private $solrConfigurationManager;

    /**
     * @var \WapplerSystems\Meilisearch\Mvc\Controller\SolrControllerContext
     */
    protected $controllerContext;

    /**
     * @param \WapplerSystems\Meilisearch\System\Configuration\ConfigurationManager
     */
    public function injectSolrConfigurationManager(ConfigurationManager $configurationManager)
    {
        $this->solrConfigurationManager = $configurationManager;
    }

    /**
     * Initialize the controller context
     *
     * @return \WapplerSystems\Meilisearch\Mvc\Controller\SolrControllerContext ControllerContext to be passed to the view
     * @api
     */
    protected function buildControllerContext()
    {
        /** @var $controllerContext \WapplerSystems\Meilisearch\Mvc\Controller\SolrControllerContext */
        $controllerContext = $this->objectManager->get(SolrControllerContext::class);
        $controllerContext->setRequest($this->request);
        $controllerContext->setResponse($this->response);
        if ($this->arguments !== null) {
            $controllerContext->setArguments($this->arguments);
        }
        $controllerContext->setUriBuilder($this->uriBuilder);
        $typoScriptConfiguration = $this->solrConfigurationManager->getTypoScriptConfiguration();
        $controllerContext->setTypoScriptConfiguration($typoScriptConfiguration);

        $this->setActiveSearchResultSet($controllerContext);

        return $controllerContext;
    }

    /**
     * @param \WapplerSystems\Meilisearch\Mvc\Controller\SolrControllerContext $controllerContext
     * @return \WapplerSystems\Meilisearch\Mvc\Controller\SolrControllerContext
     */
    protected function setActiveSearchResultSet($controllerContext)
    {
        $resultSetService = $this->initializeSearch($controllerContext->getTypoScriptConfiguration());
        $lastResult = $resultSetService->getLastResultSet();
        if (!is_null($lastResult)) {
            $controllerContext->setSearchResultSet($lastResult);
        }

        return $controllerContext;
    }

    /**
     * @param TypoScriptConfiguration $typoScriptConfiguration
     * @return SearchResultSetService
     */
    protected function initializeSearch(TypoScriptConfiguration $typoScriptConfiguration)
    {
        /** @var \WapplerSystems\Meilisearch\ConnectionManager $solrConnection */
        $solrConnection = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionByPageId($GLOBALS['TSFE']->id, Util::getLanguageUid(), $GLOBALS['TSFE']->MP);
        $search = GeneralUtility::makeInstance(Search::class, /** @scrutinizer ignore-type */ $solrConnection);

        return GeneralUtility::makeInstance(
            SearchResultSetService::class,
            /** @scrutinizer ignore-type */ $typoScriptConfiguration,
            /** @scrutinizer ignore-type */ $search
        );
    }
}
