<?php

namespace KupShop\GTMBundle\PageType;

use KupShop\GTMBundle\Utils\DataLoaders\Products;
use KupShop\GTMBundle\Utils\PriceComputer;
use KupShop\GTMBundle\Utils\ViewDataHelper;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use KupShop\KupShopBundle\Views\View;
use Symfony\Contracts\Service\Attribute\Required;

abstract class AbstractPageType
{
    use ViewDataHelper;

    /** @var View */
    protected $view;

    /**
     * @var PriceComputer
     */
    protected $priceComputer;

    /**
     * @var Products
     */
    protected $productLoader;

    public function __construct()
    {
        // Největší hnus světa, ale prostě to jinak nejde
        // Je to kvůli kolizi předefinování servisy symfony a typový kontroly php
        // Až se odstrihne v2 / komponentový GTM, může se tohle zahodit
        $this->productLoader = ServiceContainer::getService(findModule(\Modules::COMPONENTS) ? \KupShop\ComponentsBundle\GTM\Utils\DataLoaders\Products::class : Products::class);
    }

    /**
     * Collect data over getPageData() method.
     */
    public function collectData(&$dataContainer)
    {
        $this->getPageData($dataContainer);
        $this->getPageType($dataContainer);
    }

    /**
     * Specific PageData.
     */
    abstract public function getPageData(&$dataContainer);

    /**
     * Page type must be defined on every PageType.
     */
    abstract public function getPageType(&$dataContainer);

    public function setView(&$view)
    {
        $this->view = $view;
    }

    protected function addAdditionalFields(&$container, $object)
    {
    }

    /**
     * @required
     */
    public function setPriceComputer(PriceComputer $priceComputer): void
    {
        $this->priceComputer = $priceComputer;
    }
}
