<?php

/**
 * Copyright (c) D3 Data Development (Inh. Thomas Dartsch)
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 *
 * https://www.d3data.de
 *
 * @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
 * @author    D3 Data Development - Max Buhe, Daniel Seifert <info@shopmodule.com>
 * @link      https://www.oxidmodule.com
 */

declare(strict_types=1);

namespace D3\ManufacturerInformation\Modules\Application\Model;

use OxidEsales\Eshop\Application\Model\Manufacturer;
use OxidEsales\Eshop\Core\Field;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Facade\ModuleSettingService;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Facade\ModuleSettingServiceInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Templating\TemplateRendererBridgeInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Templating\TemplateRendererInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class ManufacturerInfoList extends ManufacturerInfoList_parent
{
    /**
     * @param Manufacturer|D3ManufacturerLongDesc $oManufacturer
     *
     * @return void
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    protected function addCategoryFields($oManufacturer): void
    {
        parent::addCategoryFields($oManufacturer);

        /** @var ModuleSettingService $setting */
        $setting = ContainerFactory::getInstance()->getContainer()->get(ModuleSettingServiceInterface::class);

        if ($setting->getBoolean('d3manufacturerinformation_showManufacturertextOnManufacturerPage', 'd3manufacturerinformation')) {
            $longdesc = $oManufacturer->d3GetLongdesc(  )->getRawValue() ?? '';
            $deactivateSmarty = Registry::getConfig()->getConfigParam('deactivateSmartyForCmsContent');
            $processLongDescriptions = Registry::getConfig()->getConfigParam('bl_perfParseLongDescinSmarty');
            if (!$deactivateSmarty && $processLongDescriptions) {
                $viewData = array_merge(
                    Registry::getConfig()->getActiveView()->getViewData(),
                    [
                        'oShop' => Registry::getConfig()->getActiveShop(),
                        'oViewConf' => Registry::getConfig()->getActiveView()->getViewConfig()
                    ]
                );

                $longdesc = $this->getRenderer()->renderFragment(
                    $longdesc,
                    sprintf('ox:%s%s', $oManufacturer->getId(), $oManufacturer->getLanguage()),
                    $viewData
                ) ?? '';
            }

            $oManufacturer->oxcategories__oxlongdesc = new Field(html_entity_decode($longdesc), Field::T_RAW);
        }
    }

    /**
     * @return TemplateRendererInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    private function getRenderer(): TemplateRendererInterface
    {
        return ContainerFactory::getInstance()->getContainer()->get(TemplateRendererBridgeInterface::class)->getTemplateRenderer();
    }
}