<?php

namespace KupShop\ContentBundle\View;

use KupShop\ComponentsBundle\View\ComponentsViewInterface;
use KupShop\ContentBundle\Entity\Page;
use KupShop\ContentBundle\Entity\Wrapper\PageWrapper;
use KupShop\ContentBundle\Util\MenuLinksPage;
use KupShop\KupShopBundle\Views\View;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class PageView extends View implements ComponentsViewInterface
{
    protected $template = 'page.tpl';
    protected string $entrypoint = 'home';

    /** @var Page */
    protected $page;

    private $menuLinkId;

    public function __construct(
        private MenuLinksPage $menuLinksPage,
        private PageWrapper $pageWrapper,
    ) {
        $this->proxyCacheEnabled = findModule(\Modules::PROXY_CACHE, 'content');
    }

    public function setMenuLinkId($menuLinkId)
    {
        $this->menuLinkId = $menuLinkId;
    }

    /**
     * @throws \Exception
     */
    public function getPage(): PageWrapper
    {
        if (!$this->page || $this->page->getId() != $this->menuLinkId) {
            $this->page = $this->menuLinksPage->getPage((int) $this->menuLinkId);
        }

        if (!$this->page || ($this->page->getFigure() === 'N' && !getAdminUser())) {
            throw new NotFoundHttpException('Page not found');
        }

        return $this->pageWrapper->setObject($this->page);
    }

    public function getTitle()
    {
        return $this->getPage()->getName();
    }

    public function getShowInSearch(): bool
    {
        return $this->getPage()->getShowInSearch() == 'Y';
    }

    public function getWpjToolbar()
    {
        $arr = [
            'url' => getAdminUrl('menulinks', ['ID' => $this->getPage()->getId()]),
            'title' => 'Upravit stránku',
            'translateUrl' => getAdminUrl('list', [
                'type' => 'translateMenuLinks',
                'iframe' => 1,
                'objectIDs' => $this->getPage()->getId(),
            ]),
            'translateTitle' => 'Překlad stránky',
        ];

        return array_merge(parent::getWpjToolbar(), $arr);
    }

    public function getBreadcrumbs()
    {
        return getReturnNavigation($this->getPage()->getId(), 'PAGE', [$this->getPage()->getName()]);
    }

    public function getBodyVariables()
    {
        $data = parent::getBodyVariables();

        $data['page'] = $this->getPage();

        return $data;
    }

    public function getTemplate(): string
    {
        return $this->getPage()->getTemplate() ?: $this->template;
    }

    public function getMetaTitle()
    {
        return $this->getPage()->getMetaTitle();
    }

    public function getMetaDescription()
    {
        return $this->getPage()->getMetaDescription();
    }

    public function getMetaKeywords()
    {
        return $this->getPage()->getMetaKeywords();
    }

    public function getCorrectUrl(): ?string
    {
        return createScriptURL(['URL' => $this->getPage()['url']]);
    }

    public function getTemplates(): iterable
    {
        // List all shop templates in modals
        if (is_dir(__DIR__.'/../../../../../shop/twig/view/page')) {
            foreach (new \FilesystemIterator(__DIR__.'/../../../../../shop/twig/view/page') as $file) {
                yield 'view/page/'.$file->getFilename();
            }
        }

        yield $this->template;
    }
}
