<?php

declare(strict_types=1);

/*
 * 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!
 */

namespace TYPO3\CMS\Backend\Form\NodeExpansion;

use TYPO3\CMS\Backend\Form\AbstractNode;
use TYPO3\CMS\Backend\Form\NodeFactory;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Imaging\IconSize;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Service\DependencyOrderingService;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
 * Field controls are additional HTML on a single element level that are typically
 * shown right aside the main element HTML.
 *
 * They are restricted to only allow an icon as output.
 *
 * The "edit popup" button next to a select field of a renderType "selectMultipleSideBySide"
 * is an example of such an additional control.
 *
 * The element itself must position any field controls at an appropriate place.
 * For instance the "group" element shows them in a row vertically, while others
 * display single controls next to each other.
 */
class FieldControl extends AbstractNode
{
    public function __construct(
        private readonly NodeFactory $nodeFactory,
        private readonly IconFactory $iconFactory,
        private readonly DependencyOrderingService $dependencyOrderingService,
    ) {}

    /**
     * Order the list of field wizards to be rendered with the ordering service,
     * then call each wizard element through the node factory and merge their
     * results.
     *
     * @return array Result array
     */
    public function render(): array
    {
        $result = $this->initializeResultArray();
        if (!isset($this->data['renderData']['fieldControl'])) {
            return $result;
        }

        $languageService = $this->getLanguageService();

        $fieldControl = $this->data['renderData']['fieldControl'];
        $orderedFieldControl = $this->dependencyOrderingService->orderByDependencies($fieldControl);

        foreach ($orderedFieldControl as $anOrderedFieldControl => $orderedFieldControlConfiguration) {
            if (isset($orderedFieldControlConfiguration['disabled']) && $orderedFieldControlConfiguration['disabled']
                || !isset($fieldControl[$anOrderedFieldControl]['renderType'])
            ) {
                // Don't consider this control if disabled.
                // Also ignore if renderType is not given.
                // Missing renderType may happen if an element registers a default field control
                // as disabled, and TCA enabled that. If then additionally for instance the
                // element renderType is changed to an element that doesn't register the control
                // by default anymore, this would then fatal if we don't continue here.
                // @todo: the above scenario indicates a small configuration flaw, maybe log an error somewhere?
                continue;
            }

            $options = $this->data;
            $options['renderType'] = $fieldControl[$anOrderedFieldControl]['renderType'];
            $options['renderData']['fieldControlOptions'] = $orderedFieldControlConfiguration['options'] ?? [];
            $controlResult = $this->nodeFactory->create($options)->render();

            // If the controlResult is empty (this control rendered nothing), continue to next one
            if (empty($controlResult)) {
                continue;
            }

            if (empty($controlResult['iconIdentifier'])) {
                throw new \RuntimeException(
                    'Field controls must return an iconIdentifier',
                    1483890332
                );
            }
            if (empty($controlResult['title'])) {
                throw new \RuntimeException(
                    'Field controls must return a title',
                    1483890482
                );
            }
            if (empty($controlResult['linkAttributes'])) {
                throw new \RuntimeException(
                    'Field controls must return link attributes',
                    1483891272
                );
            }

            $icon = $controlResult['iconIdentifier'];
            $title = $languageService->sL($controlResult['title']);
            $linkAttributes = $controlResult['linkAttributes'];
            if (!isset($linkAttributes['class'])) {
                $linkAttributes['class'] = 'btn btn-default';
            } else {
                $linkAttributes['class'] .= ' btn btn-default';
            }
            if (!isset($linkAttributes['href'])) {
                $linkAttributes['href'] = '#';
            }

            unset($controlResult['iconIdentifier']);
            unset($controlResult['title']);
            unset($controlResult['linkAttributes']);

            $html = [];
            $html[] = '<a ' . GeneralUtility::implodeAttributes($linkAttributes, true) . '>';
            $html[] =     $this->iconFactory->getIcon($icon, IconSize::SMALL)->setTitle($title)->render();
            $html[] = '</a>';

            $finalControlResult = $this->initializeResultArray();
            $finalControlResult = array_merge($finalControlResult, $controlResult);
            $finalControlResult['html'] = implode(LF, $html);

            $result = $this->mergeChildReturnIntoExistingResult($result, $finalControlResult);
        }
        return $result;
    }

    protected function getLanguageService(): LanguageService
    {
        return $GLOBALS['LANG'];
    }
}
