<?php

namespace Lc\SovBundle\Definition;

use Lc\SovBundle\Container\ComponentContainer;
use Lc\SovBundle\Container\File\FileContainer;
use Lc\SovBundle\Container\Newsletter\NewsletterContainer;
use Lc\SovBundle\Container\Reminder\ReminderContainer;
use Lc\SovBundle\Container\Setting\SiteSettingContainer;
use Lc\SovBundle\Container\Site\NewsContainer;
use Lc\SovBundle\Container\Site\PageContainer;
use Lc\SovBundle\Container\Site\SiteContainer;
use Lc\SovBundle\Container\Ticket\TicketContainer;
use Lc\SovBundle\Container\Ticket\TicketMessageContainer;
use Lc\SovBundle\Container\User\GroupUserContainer;
use Lc\SovBundle\Container\User\UserContainer;
use function Symfony\Component\String\u;

class ApplicationDefinition
{

    public static function getContainerList(): array
    {
        return [
            FileContainer::class,
            NewsletterContainer::class,
            ReminderContainer::class,
            NewsContainer::class,
            PageContainer::class,
            SiteContainer::class,
            TicketContainer::class,
            TicketMessageContainer::class,
            GroupUserContainer::class,
            UserContainer::class,
            SiteSettingContainer::class,
            ComponentContainer::class,
        ];
    }

    public function getContainerByEntityFqcn(string $entityFqcn)
    {
        foreach (static::getContainerList() as $containerFqcn) {
            if ($this->isContainerManageEntity($containerFqcn)) {
                if ($containerFqcn::getEntityFqcn() == $entityFqcn) {
                    return $containerFqcn;
                }
            }
        }
        return null;
    }

    public function isContainerManageEntity(string $containerFqcn)
    {
        if (method_exists($containerFqcn, 'getEntityFqcn')) {
            return true;
        }
        return false;
    }

    public static function getSubscribedContainerServices(): array
    {
        $array = [];


        foreach (static::getContainerList() as $container) {
            $array[$container] = $container;
        }

        return $array;
    }


    public static function getContainerListForTwigGlobals(): array
    {
        $array = [];

        foreach (static::getContainerList() as $containerNamespace) {
            //récupère le nom du fichier dans le namespace du container
            $key = u(substr($containerNamespace, strrpos($containerNamespace, '\\')))->snake();

            $array[$key->toString()] = '@' . $containerNamespace;
        }

        return $array;
    }


}
