<?php

namespace DanielSiepmann\DsSite\Hooks\View;

/*
 * Copyright (C) 2019 Daniel Siepmann <coding@daniel-siepmann.de>
 *
 * This program 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 2
 * of the License, or (at your option) any later version.
 *
 * This program 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

use TYPO3\CMS\Backend\View\PageLayoutView as Typo3PageLayoutView;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;

/**
 * Holds all TYPO3 Hooks for PageLayoutView class.
 */
class PageLayoutView
{
    /**
     * @var Typo3PageLayoutView
     */
    private $layoutView;

    public function processColPosGrid(
        Typo3PageLayoutView $layoutView,
        array $columnConfig
    ): string {
        $this->layoutView = $layoutView;
        if (isset($columnConfig['colPos']) && trim($columnConfig['colPos']) !== '') {
            return '';
        }

        if (isset($columnConfig['name']) && $columnConfig['name'] === 'Meta Info') {
            return $this->renderMetaInfo();
        }
    }

    private function renderMetaInfo(): string
    {
        $view = $this->getView();
        $view->assignMultiple([
            'record' => $this->layoutView->pageRecord,
            'metaInfo' => [
                [
                    'label' => 'introduction',
                    'value' => $this->layoutView->pageRecord['abstract'],
                    'field' => 'abstract',
                    'type' => 'string',
                ],
                [
                    'label' => 'published',
                    'value' => $this->layoutView->pageRecord['lastUpdated'],
                    'field' => 'lastUpdated',
                    'type' => 'date',
                ],
                [
                    'label' => 'updated',
                    'value' => $this->layoutView->pageRecord['SYS_LASTCHANGED'],
                    'type' => 'date',
                ],
            ],
        ]);
        $view->setTemplatePathAndFilename('EXT:ds_site/Resources/Private/Templates/Backend/Page/MetaInfo.html');

        return $view->render();
    }

    private function getView(): StandaloneView
    {
        return GeneralUtility::makeInstance(StandaloneView::class);
    }
}