<?php

namespace WapplerSystems\Meilisearch\System\Util;

/***************************************************************
 *  Copyright notice
 *
 *  (c) 2019 Timo Hund <timo.hund@dkd.de>
 *  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 TYPO3\CMS\Core\Exception\SiteNotFoundException;
use TYPO3\CMS\Core\Site\Entity\Site;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
 * This class contains related functions for the new site management that was introduced with TYPO3 9.
 */
class SiteUtility
{

    /**
     * Determines if the site where the page belongs to is managed with the TYPO3 site management.
     *
     * @param int $pageId
     * @return boolean
     */
    public static function getIsSiteManagedSite(int $pageId): bool
    {

        $siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
        try {
            /* @var SiteFinder $siteFinder */
            $site = $siteFinder->getSiteByPageId($pageId);
        } catch (SiteNotFoundException $e) {
            return false;
        }

        return $site instanceof Site;
    }

    /**
     *
     *
     * @param Site $typo3Site
     * @param string $property
     * @param string $defaultValue
     * @return string
     */
    public static function getConnectionProperty(Site $typo3Site, string $property, string $defaultValue = null): string
    {
        $value = self::getConnectionPropertyOrFallback($typo3Site, $property);
        if ($value === null) {
            return $defaultValue;
        }
        return $value;
    }

    /**
     * Resolves site configuration properties.
     * Language context properties have precedence over global settings.
     *
     * @param Site $typo3Site
     * @param string $property
     * @return mixed
     */
    protected static function getConnectionPropertyOrFallback(Site $typo3Site, string $property)
    {
        // convention key meilisearch_$property_$scope
        $keyToCheck = 'meilisearch_' . $property;

        // if not found check global configuration
        $siteBaseConfiguration = $typo3Site->getConfiguration();
        return self::getValueOrFallback($siteBaseConfiguration, $keyToCheck);
    }


    /**
     * @param array $data
     * @param string $keyToCheck
     * @return string|bool|null
     */
    protected static function getValueOrFallback(array $data, string $keyToCheck)
    {
        $value = $data[$keyToCheck] ?? null;
        if ($value === '0' || $value === 0 || !empty($value)) {
            return self::evaluateConfigurationData($value);
        }
        return null;
    }

    /**
     * Evaluate configuration data
     *
     * Setting boolean values via environment variables
     * results in strings like 'false' that may be misinterpreted
     * thus we check for boolean values in strings.
     *
     * @param string|bool|null $value
     * @return string|bool|null
     */
    protected static function evaluateConfigurationData($value)
    {
        if ($value === 'true') {
            return true;
        }
        if ($value === 'false') {
            return false;
        }

        return $value;
    }
}
