<?php
namespace WapplerSystems\Meilisearch\System\Meilisearch;

/***************************************************************
 *  Copyright notice
 *
 *  (c) 2009-2015 Ingo Renner <ingo@typo3.org>
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/

use MeiliSearch\Client;
use WapplerSystems\Meilisearch\System\Configuration\TypoScriptConfiguration;
use WapplerSystems\Meilisearch\System\Logging\MeilisearchLogManager;
use WapplerSystems\Meilisearch\System\Meilisearch\Parser\SchemaParser;
use WapplerSystems\Meilisearch\System\Meilisearch\Parser\StopWordParser;
use WapplerSystems\Meilisearch\System\Meilisearch\Parser\SynonymParser;
use WapplerSystems\Meilisearch\System\Meilisearch\Service\MeilisearchService;
use WapplerSystems\Meilisearch\Util;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
 * Meilisearch Service Access
 *
 * @author Ingo Renner <ingo@typo3.org>
 */
class MeilisearchConnection
{
    /**
     * @var MeilisearchService
     */
    protected $service;

    /**
     * @var TypoScriptConfiguration
     */
    protected $configuration;

    /**
     * @var array
     */
    protected $siteConfiguration;

    /**
     * @var SynonymParser
     */
    protected $synonymParser = null;

    /**
     * @var StopWordParser
     */
    protected $stopWordParser = null;

    /**
     * @var SchemaParser
     */
    protected $schemaParser = null;

    /**
     * @var Client
     */
    protected $client ;

    /**
     * @var MeilisearchLogManager
     */
    protected $logger = null;

    /**
     * @var ClientInterface[]
     */
    protected $clients = [];

    /**
     * @var ClientInterface
     */
    protected $psr7Client;


    /**
     * @var EventDispatcherInterface
     */
    protected $eventDispatcher;

    /**
     * Constructor
     *
     * @param Client $client
     * @param array $siteConfiguration
     * @param ?TypoScriptConfiguration $configuration
     * @param ?SynonymParser $synonymParser
     * @param ?StopWordParser $stopWordParser
     * @param ?SchemaParser $schemaParser
     * @param ?MeilisearchLogManager $logManager
     * @param ?ClientInterface $psr7Client
     * @param ?RequestFactoryInterface $requestFactory
     * @param ?StreamFactoryInterface $streamFactory
     * @param ?EventDispatcherInterface $eventDispatcher
     *
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function __construct(
        Client $client,
        array $siteConfiguration,
        TypoScriptConfiguration $configuration = null,
        SynonymParser $synonymParser = null,
        StopWordParser $stopWordParser = null,
        SchemaParser $schemaParser = null,
        MeilisearchLogManager $logManager = null,
        ClientInterface $psr7Client = null,
        EventDispatcherInterface $eventDispatcher = null
    ) {
        $this->client = $client;
        $this->siteConfiguration = $siteConfiguration;
        $this->configuration = $configuration ?? Util::getMeilisearchConfiguration();
        $this->synonymParser = $synonymParser;
        $this->stopWordParser = $stopWordParser;
        $this->schemaParser = $schemaParser;
        $this->logger = $logManager;
        $this->psr7Client = $psr7Client ?? GeneralUtility::getContainer()->get(ClientInterface::class);
        $this->eventDispatcher = $eventDispatcher ?? GeneralUtility::getContainer()->get(EventDispatcherInterface::class);
    }


    /**
     * @return MeilisearchService
     */
    public function getService(): MeilisearchService
    {
        if ($this->service === null) {
            $this->service = $this->buildService();
        }

        return $this->service;
    }

    /**
     * @return MeilisearchService
     * @noinspection PhpIncompatibleReturnTypeInspection
     */
    protected function buildService(): MeilisearchService
    {
        return GeneralUtility::makeInstance(MeilisearchService::class, $this, $this->client, $this->configuration, $this->logger, $this->synonymParser, $this->stopWordParser, $this->schemaParser);
    }


    /**
     * @param Client $client
     * @param ?string $endpointKey
     */
    public function setClient(Client $client, ?string $endpointKey = 'read')
    {
        $this->clients[$endpointKey] = $client;
    }

    /**
     * @return array
     */
    public function getSiteConfiguration(): array
    {
        return $this->siteConfiguration;
    }

}