<?php


namespace AutoAdmin\Bundle\BrickBuilder\Brick\Handler;


use AutoAdmin\Bundle\Admin\Form\Type\Component\RichText;
use AutoAdmin\Bundle\BrickBuilder\Brick\BrickHandlerManagerInterface;
use AutoAdmin\Bundle\BrickBuilder\Brick\Config\Configurator;
use AutoAdmin\Bundle\BrickBuilder\BrickType\BrickCategory;
use AutoAdmin\Bundle\BrickBuilder\Model\BrickInterface;
use AutoAdmin\Bundle\BrickBuilder\Model\RenderingBrickInterface;
use AutoAdmin\Bundle\BrickBuilder\Model\Setting;

class TextBrickHandler extends AbstractHtmlBrickHandler implements BrickAdminPreviewInterface
{
    const BRICK_TYPE = 'brick.text';

    public function preRender(BrickInterface $brick, BrickHandlerManagerInterface $manager): RenderingBrickInterface
    {
        $renderBrick = parent::preRender($brick, $manager);
        $brick = $renderBrick->getUnderlyingBrick();

        $brick->setSettings(
            array_replace_recursive(
                $brick->getSettings(),
                [
                    '_output' => $brick->getSetting('content', ''),
                ]
            )
        );

        return $renderBrick;
    }

    public function getAdminPreview(BrickInterface $brick): string
    {
        return sprintf('<div>%s</div>', $brick->getSetting('content', ''));
    }

    public function configure(Configurator $configurator)
    {
        $configurator->addSetting(
            Setting::new('content')
                ->setDefaultValue('Texte par défaut')
                ->setFormType(RichText::class)
                ->setFormTypeOptions(['empty_data' => ''])
        );

        parent::configure($configurator);

        $configurator->setCategory(BrickCategory::CATEGORY_TEXT);
    }
}