<?php


namespace AutoAdmin\Bundle\BrickBuilder\Brick\Handler;


use AutoAdmin\Bundle\BrickBuilder\Brick\BrickHandlerManagerInterface;
use AutoAdmin\Bundle\BrickBuilder\Brick\Config\Configurator;
use AutoAdmin\Bundle\BrickBuilder\Brick\ConfigSetting\ScalarSetting;
use AutoAdmin\Bundle\BrickBuilder\BrickType\BrickCategory;
use AutoAdmin\Bundle\BrickBuilder\BrickType\BrickNature;
use AutoAdmin\Bundle\BrickBuilder\Form\ColumnPatternType;
use AutoAdmin\Bundle\BrickBuilder\Html\AttributeMap;
use AutoAdmin\Bundle\BrickBuilder\Model\BrickInterface;
use AutoAdmin\Bundle\BrickBuilder\Model\RenderingBrickInterface;
use AutoAdmin\Bundle\BrickBuilder\Model\Setting;

class RowBrickHandler extends AbstractContainerBrickHandler
{
    const BRICK_TYPE = 'brick.row';

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

        $attributeMap = new AttributeMap($brick->getSetting('_attr', []));
        $attributeMap->addClass('row');
        $brick->setSetting('_attr', $attributeMap->toArray());

        return $renderBrick;
    }

    public function configure(Configurator $configurator)
    {
        parent::configure($configurator);
        $configurator->setNature(BrickNature::NATURE_LAYOUT);
        $configurator->setCategory(BrickCategory::CATEGORY_LAYOUT);
        $configurator->setAllowedIn([SectionBrickHandler::BRICK_TYPE,]);
        $configurator->addSetting(
            Setting::new('column_pattern')
                ->setType(ScalarSetting::class)
                ->setDefaultValue(null)
                ->setFormType(ColumnPatternType::class)
                ->setFormTypeOptions(
                    [
                        'required' => false,
                        'label' => 'Largeur de colonnes',
                    ]
                )
        );
    }
}