<?php

namespace KupShop\ContentBundle\Entity\Wrapper;

use Doctrine\Common\Collections\Collection;
use KupShop\ContentBundle\Entity\Block;
use KupShop\KupShopBundle\Template\CachingWrapperTrait;
use KupShop\KupShopBundle\Template\EntityWrapper;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;

/**
 * Class BlockWrapper.
 *
 * @property Block object
 */
class BlockWrapper extends EntityWrapper
{
    use CachingWrapperTrait;

    protected static $objectType = Block::class;

    public function field_json_content()
    {
        return $this->object->getJsonContent();
    }

    public function field_children()
    {
        /** @var Collection $children */
        if (!$this->getCachedValue($children)) {
            $children = $this->object->getChildren();

            $children = $children->map(function ($block) {
                return BlockWrapper::wrap($block);
            });

            $this->setCachedValue($children);
        }

        return $children;
    }

    public function field_photos()
    {
        return $this->object->getPhotos()->map(function ($photo) {
            return ServiceContainer::getService(PhotoWrapper::class)->setObject($photo);
        });
    }

    public function field_object_info()
    {
        return $this->object->object_info ?? '';
    }

    protected function jsonSerializeIntrospect($exclude = [])
    {
        return parent::jsonSerializeIntrospect(['getTranslation', 'getJsonContent', 'getIsWrapped', 'getUnwrapped_content']);
    }

    protected function jsonSerializeObject()
    {
        $data = parent::jsonSerializeObject();

        $this->jsonSerializeGetValues($data);

        return $data;
    }
}
