<?php

declare(strict_types=1);

/*
 * This file is part of the package itzbund/gsb-metadata-cleaner of the GSB 11 Project by ITZBund.
 *
 * Copyright (C) 2023 - 2024 Bundesrepublik Deutschland, vertreten durch das
 * BMI/ITZBund. Author: Ole Hartwig, Patrick Schriner
 *
 * This program 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.
 *
 * This program 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

namespace ITZBund\GsbMetadataCleaner\Configuration;

use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationExtensionNotConfiguredException;
use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationPathDoesNotExistException;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration as CoreExtensionConfiguration;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\PathUtility;

final class ExtensionConfiguration
{
    final public const EXIFTOOL_BINARY = 'exiftool';

    final public const QPDF_BINARY = 'qpdf';

    public function __construct(
        private readonly CoreExtensionConfiguration $extensionConfiguration,
        private readonly LoggerInterface $logger
    ) {}

    /**
     * Get the path to the exiftool executable, defined by extension configuration or default
     *
     * @return string the path to the exiftool executable
     */
    public function getExifToolPath(): string
    {
        $exifToolPath = trim($this->extensionConfiguration->get('gsb_metadata_cleaner', 'exifToolPath'));

        if (!$this->utilityExistsAndIsSafe($exifToolPath, self::EXIFTOOL_BINARY)) {
            throw new \RuntimeException('exitfool path not properly configured', 1710517552);
        }

        return $exifToolPath . self::EXIFTOOL_BINARY;
    }

    /**
     * Get the path to the qpdf executable, defined by extension configuration or default
     *
     * @return string the path to the pdf executable
     */
    public function getQpdfToolPath(): string
    {
        $qpdfToolPath = trim($this->extensionConfiguration->get('gsb_metadata_cleaner', 'qpdfToolPath'));

        if (!$this->utilityExistsAndIsSafe($qpdfToolPath, self::QPDF_BINARY)) {
            throw new \RuntimeException('qpdf path not properly configured', 1710517551);
        }

        return $qpdfToolPath . self::QPDF_BINARY;
    }

    public function getUseQpdf(): bool
    {
        $useQpdf = true;
        try {
            $useQpdf = (bool)$this->extensionConfiguration->get('gsb_metadata_cleaner', 'useQpdf');
        } catch (ExtensionConfigurationExtensionNotConfiguredException $ecence) {
            $this->logger->log(LogLevel::INFO, 'Extension not configured');
        } catch (ExtensionConfigurationPathDoesNotExistException $ecpdnee) {
            $this->logger->log(LogLevel::INFO, 'useQpdf not configured');
        }
        return $useQpdf;
    }

    /**
     * @param string $path
     * @param string $binaryName
     * @return bool
     */
    protected function utilityExistsAndIsSafe(string $path, string $binaryName): bool
    {
        if ($path === '' || !PathUtility::isAbsolutePath($path)
            || !GeneralUtility::validPathStr($path) || str_starts_with($path, Environment::getPublicPath())
            || !is_dir($path)
        ) {
            return false;
        }

        if (file_exists($path . $binaryName)) {
            return true;
        }
        return false;
    }
}