<?php

namespace Snaatch\Typo3\Backend;

use TYPO3\CMS\Core\Page\JavaScriptModuleInstruction;

/**
 * Overrides the FilesControlContainer to inject the "Select from snaatch" button.
 *
 * This works only for v12.
 */
class FilesControlContainer extends \TYPO3\CMS\Backend\Form\Container\FilesControlContainer
{
    use SnaatchContainerTrait;

    protected function getFileSelectors(array $inlineConfiguration, $fileExtensionFilter): array
    {
        $languageService = $this->getLanguageService();
        $buttonText = htmlspecialchars($languageService->sL('LLL:EXT:snaatch_typo3/Resources/Private/Language/locallang_be.xlf:modal_view.button'));

        $foreign_table = $inlineConfiguration['foreign_table'];
        $currentStructureDomObjectIdPrefix = $this->inlineStackProcessor->getCurrentStructureDomObjectIdPrefix($this->data['inlineFirstPid']);
        $objectPrefix = htmlspecialchars($currentStructureDomObjectIdPrefix . '-' . $foreign_table);

        $uid = uniqid();
        $iframe_url = 'https://app.vaulo.com?appMode=file-selector&fileSelectorAction=select&fileSelectorAction=crop&fileSelectorAction=multi-select&fileSelectorMode=direct&fileSelectorFormat=jpeg';

        $button = $this->renderSnaatchButton($buttonText, $objectPrefix, $uid, $iframe_url);

        $this->javaScriptModules[] = JavaScriptModuleInstruction::create('@snaatch/typo3/ClientSideCodeV12.js');

        return [
            ...parent::getFileSelectors($inlineConfiguration, $fileExtensionFilter),
            $button
        ];
    }

    /**
     * Inject necessary snaatch_typo3 style sheet
     *
     * @return array
     */
    public function render(): array
    {
        $resultArray = parent::render();

        // inject stylesheet in render process to be loaded in ce and irre elements
        $resultArray['stylesheetFiles'][] = 'EXT:snaatch_typo3/Resources/Public/StyleSheet/Styles.css';
        return $resultArray;
    }
}
