<?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\MargeType;
use AutoAdmin\Bundle\BrickBuilder\Html\AttributeMap;
use AutoAdmin\Bundle\BrickBuilder\Model\BrickInterface;
use AutoAdmin\Bundle\BrickBuilder\Model\RenderingBrickInterface;
use AutoAdmin\Bundle\BrickBuilder\Model\Setting;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use AutoAdmin\Bundle\BrickBuilder\Brick\Handler\AbstractHtmlBrickHandler;

class SectionBrickHandler extends AbstractHtmlBrickHandler
{
    const BRICK_TYPE = 'brick.section';

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

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

        $marginTop = $brick->getSetting('margin_top');
        if ($marginTop !== null) {
            $attributeMap->addClass('mt-' . $marginTop);
        }

        $marginBottom = $brick->getSetting('margin_bottom');
        if ($marginBottom !== null) {
            $attributeMap->addClass('mb-' . $marginBottom);
        }

        $paddingTop = $brick->getSetting('padding_top');
        if ($paddingTop !== null) {
            $attributeMap->addClass('pt-' . $paddingTop);
        }

        $paddingBottom = $brick->getSetting('padding_bottom');
        if ($paddingBottom !== null) {
            $attributeMap->addClass('pb-' . $paddingBottom);
        }

        $brick->setSetting('_attr', $attributeMap->toArray());

        return $renderBrick;
    }

    public function configure(Configurator $configurator)
    {
        parent::configure($configurator);

        $configurator->setTemplate('@AutoAdminBrickBuilder/brick/section_brick.html.twig');
        $configurator->setOption('_tag', 'section', true);
        $configurator->setNature(BrickNature::NATURE_LAYOUT);
        $configurator->setCategory(BrickCategory::CATEGORY_LAYOUT);
        $configurator->setAllowedIn([RootBrickHandler::BRICK_TYPE,]);

        $configurator->addSetting(
            Setting::new('full_width')
                ->setDefaultValue(false)
                ->setFormType(CheckboxType::class)
                ->setFormTypeOptions(['required' => false, 'label' => 'brick.section.form.full_width.label'])
        );

        $configurator->addSetting(
            Setting::new('margin_top')
                ->setType(ScalarSetting::class)
                ->setDefaultValue(null)
                ->setFormType(MargeType::class)
                ->setFormTypeOptions(
                    [
                        'required' => false,
                        'label' => 'Marges externe haut',
                    ]

                )
        );

        $configurator->addSetting(
            Setting::new('margin_bottom')
                ->setType(ScalarSetting::class)
                ->setDefaultValue(null)
                ->setFormType(MargeType::class)
                ->setFormTypeOptions(
                    [
                        'required' => false,
                        'label' => 'Marges externe bas',
                    ]

                )
        );

        $configurator->addSetting(
            Setting::new('padding_top')
                ->setType(ScalarSetting::class)
                ->setDefaultValue(null)
                ->setFormType(MargeType::class)
                ->setFormTypeOptions(
                    [
                        'required' => false,
                        'label' => 'Marges interne haut',
                    ]

                )
        );

        $configurator->addSetting(
            Setting::new('padding_bottom')
                ->setType(ScalarSetting::class)
                ->setDefaultValue(null)
                ->setFormType(MargeType::class)
                ->setFormTypeOptions(
                    [
                        'required' => false,
                        'label' => 'Marges interne bas',
                    ]

                )
        );
    }
}
