<?php

declare(strict_types=1);

namespace FiloBlu\Refilo\Helper\ContentManager;

use Blackbird\ContentManager\Api\Data\ContentTypeInterface;
use Blackbird\ContentManager\Api\Data\ContentTypeInterfaceFactory;
use Blackbird\ContentManager\Model\ContentFactory;
use Blackbird\ContentManager\Model\ResourceModel\ContentType\CollectionFactory;
use FiloBlu\Refilo\Remote\Entity\UrlRewrite;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\DataObject;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;

/**
 *
 */
class Data
{
    /** @var CollectionFactory */
    protected $contentTypeCollectionFactory;

    /** @var ContentTypeInterface[] */
    private $contentTypes;

    /** @var ContentTypeInterfaceFactory */
    private $contentTypeInterfaceFactory;

    /** @var ContentTypeInterface */
    private $default;

    /** @var ScopeConfigInterface */
    private $scopeConfig;

    /** @var ContentFactory */
    private $contentFactory;

    /** @var StoreManagerInterface */
    private $storeManager;

    /**
     * @param CollectionFactory $contentTypeCollectionFactory
     * @param ContentTypeInterfaceFactory $contentTypeInterfaceFactory
     * @param ContentFactory $contentFactory
     * @param ScopeConfigInterface $scopeConfig
     * @param StoreManagerInterface $storeManager
     */
    public function __construct(
        CollectionFactory           $contentTypeCollectionFactory,
        ContentTypeInterfaceFactory $contentTypeInterfaceFactory,
        ContentFactory              $contentFactory,
        ScopeConfigInterface        $scopeConfig,
        StoreManagerInterface       $storeManager
    ) {
        $this->contentTypeCollectionFactory = $contentTypeCollectionFactory;
        $this->contentTypeInterfaceFactory = $contentTypeInterfaceFactory;
        $this->scopeConfig = $scopeConfig;
        $this->contentFactory = $contentFactory;
        $this->storeManager = $storeManager;
    }

    /**
     * @param $ctId
     * @return mixed|null
     */
    public function get($ctId): ContentTypeInterface
    {
        if (!$this->contentTypes) {
            $this->contentTypes = [];
            $types = $this->contentTypeCollectionFactory->create()->getItems();
            foreach ($types as $type) {
                $this->contentTypes[$type->getId()] = $type;
            }
            $this->default = $this->contentTypeInterfaceFactory->create();
        }
        return $this->contentTypes[$ctId] ?? $this->default;
    }

    /**
     * @param $ctId
     * @return string
     */
    public function getIdentifierFromId($ctId)
    {
        return $this->get($ctId)->getIdentifier();
    }

    /**
     * @param $ctId
     * @return mixed
     */
    public function getDataFromId($ctId)
    {
        return $this->get($ctId)->getData();
    }

    /**
     * @return DataObject[]
     */
    public function getContentTypeList()
    {
        return $this->contentTypeCollectionFactory->create()->getItems();
    }

    /**
     * @param UrlRewrite $urlRewrite
     * @return array|array[]
     * @throws NoSuchEntityException
     * @throws LocalizedException
     */
    public function getContentTypeBreadcrumbs(UrlRewrite $urlRewrite): array
    {
        /** @var Store $store */
        $store = $this->storeManager->getStore($urlRewrite->getStore());

        $content = $this->contentFactory->create()->setStore($store)->load($urlRewrite->getEntityId());
        $contentType = $content->getContentType();

        // Frontend viene gestito il blog come static page
        if($contentType->getIdentifier() === 'blog' && $this->isBlogEnabled($store))
        {
            return [
                [
                    'label' => __('Home'),
                    'url'   => '/'
                ],
                [
                    'label' => __('Blog'),
                    'url'   => $this->scopeConfig->getValue('bluheadless_static_pages/blog/path', ScopeInterface::SCOPE_STORE, $store)
                ],
                [
                    'label' => $content->getTitle(),
                    'url'   => '/' . $content->getUrl()
                ]
            ];
        }

        return [];
    }

    /**
     * @param $store
     * @return bool
     */
    public function isBlogEnabled($store) : bool
    {
        return $this->scopeConfig->isSetFlag('bluheadless_static_pages/blog/enabled', ScopeInterface::SCOPE_STORE, $store);
    }
}
