<?php


namespace AutoAdmin\Bundle\Content\Controller;


use AutoAdmin\Bundle\Content\Configuration\ConfigManager;
use AutoAdmin\Bundle\Content\Context\ContentContext;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

abstract class AbstractContentController extends AbstractController implements ContentControllerInterface
{
    protected ContentContext $contentContext;
    protected ConfigManager $config;
    protected EntityManagerInterface $em;

    public function __construct(ContentContext $contentContext, ConfigManager $config, EntityManagerInterface $em)
    {
        $this->contentContext = $contentContext;
        $this->config = $config;
        $this->em = $em;
    }

    public function setContentContext(ContentContext $contentContext)
    {
        $this->contentContext = $contentContext;
    }

    public function setConfigManager(ConfigManager $config)
    {
        $this->config = $config;
    }

    public function getTemplate(string $slugOrObject, $action = 'view')
    {
        if ($slugOrObject === 'index') {
            return $this->config->getTemplate('page', 'index');
        }

        return $this->config->getTemplate($slugOrObject, $action);
    }

    public function getGlobalTemplate(string $key)
    {
        $templates = $this->config->getTemplates();
        if (array_key_exists($key, $templates)) {
            return $templates[$key];
        }

        return null;
    }

    public function isTranslated($class): bool
    {
        return class_exists($class . 'Translation');
    }
}
