<?php

namespace AutoAdmin\Bundle\BrickBuilder\Brick\Handler;

use AutoAdmin\Bundle\BrickBuilder\Brick\BrickHandlerManagerInterface;
use AutoAdmin\Bundle\BrickBuilder\Brick\Config\Configurator;
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\AbstractBaseBrick;
use AutoAdmin\Bundle\BrickBuilder\Model\BrickInterface;
use AutoAdmin\Bundle\BrickBuilder\Model\RenderingBrick;
use AutoAdmin\Bundle\BrickBuilder\Model\RenderingBrickInterface;

class ColumnBrickHandler extends AbstractContainerBrickHandler
{
    const BRICK_TYPE = 'brick.column';

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

        $attributeMap = new AttributeMap($brick->getSetting('_attr', []));
        if ($attributeMap->hasClass('col')) {
            $attributeMap->removeClass('col');
        }

        /** @var AbstractBaseBrick $parent */
        if (
            ($parent = $brick->getParent())
            && $parent->getType() === RowBrickHandler::BRICK_TYPE
            && ($brickIndex = $renderBrick->getRenderingIndex()) !== null
        ) {
            if ($colPattern = $parent->getSetting('column_pattern')) {
                $colClasses = [
                    ColumnPatternType::PATTERN_COL1_100 => ['12',],
                    ColumnPatternType::PATTERN_COL2_50_50 => ['6', '6',],
                    ColumnPatternType::PATTERN_COL2_33_66 => ['4', '8',],
                    ColumnPatternType::PATTERN_COL2_25_75 => ['3', '9',],
                    ColumnPatternType::PATTERN_COL2_66_33 => ['8', '4',],
                    ColumnPatternType::PATTERN_COL2_75_25 => ['9', '3',],
                    ColumnPatternType::PATTERN_COL3_33_33_33 => ['4', '4', '4',],
                    ColumnPatternType::PATTERN_COL3_50_25_25 => ['6', '3', '3',],
                    ColumnPatternType::PATTERN_COL3_25_50_25 => ['3', '6', '3',],
                    ColumnPatternType::PATTERN_COL3_25_25_50 => ['3', '3', '6',],
                    ColumnPatternType::PATTERN_COL3_20_60_20 => ['2', '8', '2',],
                    ColumnPatternType::PATTERN_COL4_25_25_25_25 => ['3', '3', '3', '3',],
                    // ColumnPatternType::PATTERN_COL5_20_20_20_20_20 => ['2', '2', '2', '2','2',], Commented because bootstrap doesn't support 20% columns
                    ColumnPatternType::PATTERN_COL6_16_16_16_16_16_16 => ['2','2','2','2','2','2',],
                ];


                $colClass = 'col-md';
                if (isset($colClasses[$colPattern])) {
                    $brickIndex = $brickIndex % count($colClasses[$colPattern]);
                    if (isset($colClasses[$colPattern][$brickIndex])) {
                        $colClass .= '-' . $colClasses[$colPattern][$brickIndex];
                    }
                }

                $attributeMap->addClass($colClass);
            } else {
                $attributeMap->addClass('col-md');
            }
        }
        $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([RowBrickHandler::BRICK_TYPE,]);
    }
}