<?php

namespace Snaatch\Typo3\Backend;

use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;

/**
 * Trait for shared Snaatch functionality between container classes.
 * Avoids code duplication between FilesControlContainer and InlineControlContainer.
 * Uses Fluid template for HTML generation following TYPO3 best practices.
 */
trait SnaatchContainerTrait
{
    /**
     * Generate the common Snaatch button and modal HTML structure using Fluid template
     *
     * @param string $buttonText The localized button text
     * @param string $objectPrefix The DOM object prefix
     * @param string $uid Unique ID for this instance
     * @param string $iframeUrl The Snaatch iframe URL
     * @return string The complete HTML structure
     */
    protected function renderSnaatchButton(string $buttonText, string $objectPrefix, string $uid, string $iframeUrl): string
    {
        $view = GeneralUtility::makeInstance(StandaloneView::class);
        $view->setTemplatePathAndFilename(
            GeneralUtility::getFileAbsFileName('EXT:snaatch_typo3/Resources/Private/Templates/SnaatchButton.html')
        );
        $iconFactory = $this->iconFactory ?? GeneralUtility::makeInstance(IconFactory::class);
        
        $view->assignMultiple([
            'buttonText' => $buttonText,
            'objectPrefix' => $objectPrefix,
            'uid' => $uid,
            'iframeUrl' => $iframeUrl,
            'buttonIcon' => $iconFactory ->getIcon('actions-snaatch-show-modal', Icon::SIZE_SMALL)->render(),
            'closeIcon' => $iconFactory ->getIcon('actions-close', Icon::SIZE_SMALL)->render(),
        ]);
        
        return $view->render();
    }
}
