<?php

namespace Opentalent\OtTemplating\Templating;

use Opentalent\OtCore\Website\OtPageRepository;
use Opentalent\OtCore\Website\OtWebsiteRepository;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;

/**
 * Class TemplateRepository
 * Provides methods to retrieve data about the current site's template
 *
 * @package Opentalent\OtCore\Templating
 */
class TemplateRepository
{
    CONST templates = [
        'Classic' => [
            'name' => 'Classique',
            'description' => "Le thème classique, simple et complet. C'est le thème par défaut.",
            'picture' => 'EXT:ot_templating/Resources/Public/media/theme_classic.png'
        ],
        'Modern' => [
            'name' => 'Moderne',
            'description' => '[Nouveauté 2020] Un thème moderne et intuitif.',
            'picture' => 'EXT:ot_templating/Resources/Public/media/theme_modern.png'
        ]
    ];

    CONST defaultTemplate = 'Classic';

    CONST defaultPreferences = [
        'themeColor' => 'light-blue',
        'displayCarousel' => '1',
        'displayBreadcrumb' => '1',
    ];

    /**
     * Returns the site template's name
     *
     * @param int $rootUid
     * @return string
     */
    public function getTemplate(array $website): string
    {
        if (!($website['uid'] >= 0)) {
            return self::defaultTemplate;
        }

        $templateName = $website['template'];

        if ($templateName==='' || $templateName==null) {
            return self::defaultTemplate;
        }
        return $templateName;
    }

    /**
     * Returns the current site template's name
     * @param int $rootUid
     * @return array
     */
    public function getTemplatePreferences(array $website): array
    {
        $templatePreferences = $website['template_preferences'] ?? '';

        if ($templatePreferences !== '') {
            $templatePreferences = json_decode($templatePreferences, true);
        } else {
            $templatePreferences = [];
        }
        return array_merge(self::defaultPreferences, $templatePreferences);
    }
}
