<?php


namespace AutoAdmin\Bundle\BrickBuilder\Brick\Handler;


use AutoAdmin\Bundle\BrickBuilder\Brick\BrickHandlerManagerInterface;
use AutoAdmin\Bundle\BrickBuilder\Brick\BrickRendererInterface;
use AutoAdmin\Bundle\BrickBuilder\Brick\Config\BrickConfiguration;
use AutoAdmin\Bundle\BrickBuilder\Brick\Config\Configurator;
use AutoAdmin\Bundle\BrickBuilder\BrickType\BrickCategory;
use AutoAdmin\Bundle\BrickBuilder\BrickType\BrickNature;
use AutoAdmin\Bundle\BrickBuilder\Html\AttributeMap;
use AutoAdmin\Bundle\BrickBuilder\Model\BrickInterface;
use AutoAdmin\Bundle\BrickBuilder\Model\RenderingBrick;
use AutoAdmin\Bundle\BrickBuilder\Model\RenderingBrickInterface;
use AutoAdmin\Bundle\BrickBuilder\Model\Setting;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Twig\Environment;

abstract class AbstractBrickHandler implements BrickHandlerInterface
{
    public const BRICK_TYPE = null;

    protected Environment $twig;
    protected BrickConfiguration $configuration;
    protected ?string $appEnv;

    public function setTwig(Environment $twig): void
    {
        $this->twig = $twig;
    }

    public function setAppEnv(?string $appEnv)
    {
        $this->appEnv = $appEnv;
    }

    public function getConfiguration(): BrickConfiguration
    {
        return $this->configuration;
    }

    public function setConfiguration(BrickConfiguration $configuration)
    {
        $this->configuration = $configuration;
    }

    public function process(BrickInterface $brick): void
    {
    }

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

        $attributeMap = new AttributeMap($brick->getSetting('_attr', []));
        $attributeMap->addClass('brick');
        if ($brickId = $brick->getId()) {
            $attributeMap->set('data-brick-id', $brickId);
        }

        $cssClasses = $brick->getSetting('css_classes', '');
        if ($cssClasses) {
            $attributeMap->addClass($cssClasses);
        }


        $wrapper = $brick->getSetting('wrapper', '');
        if ($wrapper) {
            $attributeMap->addClass('brick-wrapper');
        }

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

        return $renderBrick;
    }

    public function render(RenderingBrickInterface $brick, BrickRendererInterface $renderer): ?string
    {
        return null;
    }

    public function configure(Configurator $configurator)
    {
        $configurator->setOption('_attr', ['class' => 'brick']);
        $configurator->setNature(BrickNature::NATURE_CONTENT);
        $configurator->setCategory(BrickCategory::CATEGORY_MISC);
        $configurator->setAllowedIn([ColumnBrickHandler::BRICK_TYPE,]);

        $configurator->addSetting(
            Setting::new('css_classes')
                ->setDefaultValue('')
                ->setFormType(TextType::class)
                ->setFormTypeOptions(
                    [
                        'required' => false,
                        'label' => 'brick.form.css_classes.label',
                        'help' => 'brick.form.css_classes.help',
                        'attr' => [
                            'pattern' => '[a-z0-9-_ ]+',
                        ],
                    ]
                )
        );

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

    public final function getBrickType(): ?string
    {
        return self::BRICK_TYPE;
    }
}
