<?php

namespace SGalinski\SgYoutube\Hooks\PageLayoutView;

/***************************************************************
 *  Copyright notice
 *  (c) sgalinski Internet Services (https://www.sgalinski.de)
 *  All rights reserved
 *  This script is part of the TYPO3 project. The TYPO3 project 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.
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *  This script 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.
 *  This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/

use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Backend\View\PageLayoutView;
use TYPO3\CMS\Backend\View\PageLayoutViewDrawItemHookInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;
use \TYPO3\CMS\Core\Localization\LanguageService;

/**
 * Class PluginRenderer
 * Renders back-end preview for the SG YouTube Videos plugin
 *
 * @package SGalinski\SgYoutube\Hooks
 */
class PluginRenderer implements PageLayoutViewDrawItemHookInterface {
	/**
	 * Preprocesses the preview rendering of a content element of type "sg_youtube"
	 *
	 * @param PageLayoutView $parentObject Calling parent object
	 * @param bool $drawItem Whether to draw the item using the default functionality
	 * @param string $headerContent Header content
	 * @param string $itemContent Item content
	 * @param array $row Record row of tt_content
	 * @return void
	 * @noinspection ReferencingObjectsInspection
	 */
	public function preProcess(
		PageLayoutView &$parentObject, &$drawItem, &$headerContent, &$itemContent, array &$row
	): void {
		if ($row['list_type'] === 'sgyoutube_youtube') {
			$drawItem = FALSE;

			$view = GeneralUtility::makeInstance(StandaloneView::class);
			$view->setTemplateRootPaths(['EXT:sg_youtube/Resources/Private/Templates/Youtube']);
			$view->setTemplate('Backend.html');
			$view->assign('uid', $row['uid']);

			// Set plugin options array
			$pluginOptions = GeneralUtility::xml2array($row['pi_flexform'], 'T3:');
			$templateOptions = [];
			if (is_array($pluginOptions)) {
				foreach ($pluginOptions['data']['sDEF']['lDEF'] as $option => $value) {
					if ($option !== 'settings.showApiResult') {
						// $option has the format 'settings.<key>', but in the template we want the language label which
						// has the format 'flexform.<key>'.
						$templateOptions[str_replace('settings.', '', $option)] = $value['vDEF'];
					}
				}

				$languageService = GeneralUtility::makeInstance(LanguageService::class);
				$templateOptions['flexform.thumbnailType'] = $languageService->sL(
					'LLL:EXT:sg_youtube/Resources/Private/Language/locallang.xlf:flexform.thumbnailType.' . $templateOptions['flexform.thumbnailType']
				);

				$view->assignMultiple([
					'pluginOptions' => $templateOptions,
					'showApiResult' => $pluginOptions['data']['sDEF']['lDEF']['settings.showApiResult']['vDEF']
				]);
			}

			// Content element labels
			$pluginName = BackendUtility::getLabelFromItemListMerged(
				$row['pid'], 'tt_content', 'list_type', $row['list_type']
			);
			$headerContent = '<strong>[' . $pluginName . '] </strong>' . $headerContent;

			$itemContent .= $view->render();
		}
	}
}