<?php

namespace Plainwork\Ui\Component;

final class InlineList extends AbstractTag
{
    private array $items = [];
    private int $spacing = 2;
    private bool $separateWithComma = false;
    private bool $separateWithMiddot = false;
    private ?int $heroIndex = null;

    public function __construct(...$items)
    {
        if ((1 === count($items)) && is_array(current($items))){
            $items = current($items);
        }

        $this->items = $items;
    }

    public function smallSpacing(): self
    {
        return $this->setSpacing(1);
    }

    public function largeSpacing(): self
    {
        return $this->setSpacing(3);
    }

    public function noSpacing(): self
    {
        return $this->setSpacing(0);
    }

    public function setSpacing(int $v): self
    {
        if ($v < 0) {
            $v = 0;
        }
        if ($v > 3) {
            $v = 3;
        }
        $this->spacing = $v;

        return $this;
    }

    public function getSpacing(): int
    {
        return $this->spacing;
    }

    public function withHero(int $heroIndex): self
    {
        $this->heroIndex = $heroIndex;

        return $this;
    }

    public function addHero($item): self
    {
        $this->add($item);
        $index = count($this->items) - 1;
        $this->heroIndex = $index;

        return $this;
    }

    public function getHeroIndex(): ?int
    {
        return $this->heroIndex;
    }

    public function add(...$items): self
    {
        foreach ($items as $item) {
            $this->items[] = $item;
        }

        return $this;
    }

    public function getItems(): array
    {
        return $this->items;
    }

    public function separateWithComma(bool $v = true): self
    {
        $this->separateWithComma = $v;
        $this->separateWithMiddot = !$v;

        return $this;
    }

    public function separateWithMiddot(bool $v = true): self
    {
        $this->separateWithMiddot = $v;
        $this->separateWithComma = !$v;

        return $this;
    }

    public function isCommaSeparated(): bool
    {
        return $this->separateWithComma;
    }

    public function isMiddotSeparated(): bool
    {
        return $this->separateWithMiddot;
    }
}
