<?php


namespace AutoAdmin\Bundle\BrickBuilder\Model;


class RenderingBrick implements RenderingBrickInterface
{
    /**
     * Real brick reference
     */
    protected ?BrickInterface $underlyingBrick = null;

    /**
     * Index among siblings
     */
    protected ?int $renderingIndex = null;

    public static function from(BrickInterface $brick): RenderingBrickInterface
    {
        $self = new static();
        $self->setUnderlyingBrick($brick);
        if ($parent = $brick->getParent()) {
            $self->setRenderingIndex(array_search($brick, $parent->getChildren()->toArray()) ?: 0);
        }

        return $self;
    }

    public function getUnderlyingBrick(): ?BrickInterface
    {
        return $this->underlyingBrick;
    }

    public function setUnderlyingBrick($underlyingBrick): void
    {
        $this->underlyingBrick = $underlyingBrick;
    }

    public function getRenderingIndex(): ?int
    {
        return $this->renderingIndex;
    }

    public function setRenderingIndex(?int $renderingIndex): void
    {
        $this->renderingIndex = $renderingIndex;
    }
}