<?php
/**
 * @author La clic ! <contact@laclic.fr>
 */

namespace Lc\CaracoleBundle\Resolver;

use Doctrine\ORM\EntityManagerInterface;
use Lc\CaracoleBundle\Model\Section\SectionInterface;
use Lc\CaracoleBundle\Repository\Section\SectionRepository;
use Lc\CaracoleBundle\Repository\Section\SectionStore;
use Lc\SovBundle\Resolver\UrlResolver;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Security;

class SectionResolver
{
    protected EntityManagerInterface $entityManager;
    protected MerchantResolver $merchantResolver;
    protected SectionStore $sectionStore;
    protected RequestStack $requestStack;
    protected UrlResolver $urlResolver;

    public function __construct(
            EntityManagerInterface $entityManager,
            MerchantResolver $merchantResolver,
            SectionStore $sectionStore,
            RequestStack $requestStack,
            UrlResolver $urlResolver
    ) {
        $this->entityManager = $entityManager;
        $this->merchantResolver = $merchantResolver;
        $this->sectionStore = $sectionStore;
        $this->requestStack = $requestStack;
        $this->urlResolver = $urlResolver;
    }

    public function getCurrent()
    {
        $requestAttributesArray = $this->requestStack->getMainRequest()->attributes->all();

        // admin
        if (isset($requestAttributesArray['easyadmin_context'])) {
            $currentAdminSection = null;
            $userMerchant = $this->merchantResolver->getUserMerchant();

            if ($userMerchant !== null) {
                $currentAdminSection = $userMerchant->getCurrentAdminSection();
            }

            if ($currentAdminSection === null) {
                $currentAdminSection = $this->sectionStore
                        ->setMerchant($userMerchant->getMerchant())
                        ->getOneDefault();

                if ($currentAdminSection === null) {
                    throw new \ErrorException('Aucune section par défaut définie pour ce merchant');
                }
            }

            return $currentAdminSection;
        } // front
        else {
            $sectionCurrent = null;
            $isCli = php_sapi_name() === 'cli';

            // local
            if ($isCli || $this->urlResolver->isServerLocalhost()) {
                $sectionArray = $this->sectionStore
                        ->setMerchant($this->merchantResolver->getCurrent())
                        ->getOnline();

                foreach ($sectionArray as $section) {
                    if ($section->getDevAlias() == $_ENV['CURRENT_SECTION_LOCAL']) {
                        $sectionCurrent = $section;
                    }
                }
            }
            // distant
            else {
                $sectionCurrent = $this->sectionStore
                        ->setMerchant($this->merchantResolver->getCurrent())
                        ->getOneDefault();
            }

            return $sectionCurrent;
        }
    }


}