<?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\Configurator;
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;

abstract class AbstractHtmlBrickHandler extends AbstractBrickHandler implements HtmlBrickHandlerInterface
{
    public function preRender(BrickInterface $brick, BrickHandlerManagerInterface $manager): RenderingBrickInterface
    {
        $renderBrick = parent::preRender($brick, $manager);
        $brick = $renderBrick->getUnderlyingBrick();

        if ($brick->getChildren()) {
            foreach ($brick->getChildren() as $child) {
                $rendered = $manager->getHandler($child)->preRender($child, $manager);
            }
        }

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

        if ($brick->getSetting('center')) {
            $attributeMap->addClass('text-center mx-auto');
        }

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

        return $renderBrick;
    }

    public function render(RenderingBrickInterface $renderBrick, BrickRendererInterface $renderer): ?string
    {
        $brick = $renderBrick->getUnderlyingBrick();

        $childrenContent = '';
        if ($brick->getChildren()) {
            foreach ($brick->getChildren() as $child) {
                $rendered = $renderer->render($child);
                if ($rendered) {
                    $childrenContent .= $rendered;
                }
            }
        }

        return $this->twig->render(
            $brick->getSetting('_template'),
            [
                'brick' => $brick,
                'brickChildrenContent' => $childrenContent,
                'settings' => $brick->getSettings(),
                'tag' => $this->buildTag($brick),
            ]
        );
    }

    public function buildTag(BrickInterface $brick): array
    {
        $tag = $brick->getSetting('_tag');

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

        if ($this->appEnv !== 'prod') {
            $attributeMap->set('data-brick-type', $this::BRICK_TYPE ?? '');
        }

        if ($brick->getSetting('_self_closing') !== false) {
            return [sprintf('<%s%s/>', $tag, $attributeMap->compile()),];
        }

        return [sprintf('<%s%s>', $tag, $attributeMap->compile()), sprintf('</%s>', $tag)];
    }

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

        $configurator->setTemplate('@AutoAdminBrickBuilder/brick/html_brick.html.twig');
        $configurator->setOption('_tag', 'div');
        $configurator->setOption('_self_closing', false);

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