<?php

namespace Plainwork\Ui\Component;

final class Dl extends AbstractTag
{
    private array $head = [];
    private array $items = [];
    private array $itemAttr = [];
    private $title = null;
    private bool $isVertical = false;
    private bool $asTable = false;
    private array $itemLabelAttr = [];
    private array $itemValueAttr = [];
    private array $titleAttr = [];

    public function __construct(...$items)
    {
        for ($i = 0; $i < count($items); $i+=2) {
            $this->add($items[$i], $items[$i+1]);
        }
    }

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

        return $this;
    }

    public function isAsTable(): bool
    {
        return $this->asTable;
    }

    public function setVertical(bool $v = true): self
    {
        $this->isVertical = $v;

        return $this;
    }

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

    public function setTitle($title): self
    {
        $this->title = $title;

        return $this;
    }

    public function getTitle()
    {
        return $this->title;
    }

    public function setHead($label, $item): self
    {
        $this->head = [$label, $item];

        return $this;
    }

    public function getHead(): array
    {
        return $this->head;
    }

    public function add($label, $item, int $weight = 0): self
    {
        if (!$weight) {
            if ($this->items) {
                $weight = $this->items[count($this->items) - 1][2] + 1;
            } else {
                $weight = 50;
            }
        }
        $this->items[] = [$label, $item, $weight];

        return $this;
    }

    public function withLabelAttr(string $attrName, $v): self
    {
        $ii = count($this->items) - 1;
        $this->itemLabelAttr[$ii] ??= [];
        
        if (in_array($attrName, ['style'])) {
            $this->itemLabelAttr[$ii][$attrName] ??= [];
            $this->itemLabelAttr[$ii][$attrName] = array_merge($this->itemLabelAttr[$ii][$attrName], $v);
        } else {
            $this->itemLabelAttr[$ii][$attrName] = $v;
        }

        return $this;
    }

    public function getTitleAttr(): array
    {
        return $this->titleAttr;
    }

    public function withTitleAttr(string $attrName, $v): self
    {
        if (in_array($attrName, ['style'])) {
            $this->titleAttr[$attrName] ??= [];
            $this->titleAttr[$attrName] = array_merge($this->titleAttr[$attrName], $v);
        } else {
            $this->titleAttr[$attrName] = $v;
        }

        return $this;
    }

    public function getLabelAttr(int $ii): array
    {
        return $this->itemLabelAttr[$ii] ?? [];
    }

    public function withValueAttr(string $attrName, $v): self
    {
        $ii = count($this->items) - 1;
        $this->itemValueAttr[$ii] ??= [];
        
        if (in_array($attrName, ['style'])) {
            $this->itemValueAttr[$ii][$attrName] ??= [];
            $this->itemValueAttr[$ii][$attrName] = array_merge($this->itemValueAttr[$ii][$attrName], $v);
        } else {
            $this->itemValueAttr[$ii][$attrName] = $v;
        }

        return $this;
    }

    public function getValueAttr(int $ii): array
    {
        return $this->itemValueAttr[$ii] ?? [];
    }

    public function getItems(): array
    {
        $ret = $this->items;

        usort($ret, fn($a, $b) => ($a[2] - $b[2]));
        $ret = array_map(fn($a) => [$a[0], $a[1]], $ret);

        return $ret;
    }

    public function withItemAttr(string $itemId, string $name, $value): self
    {
        $attr = $this->itemAttr[$itemId] ?? [];

        if ('class' === $name) {
            if (!isset($attr[$name])) {
                $attr[$name] = [];
            }
            if (!is_array($attr[$name])) {
                $attr[$name] = [$attr[$name] => $attr[$name]];
            }
            $attr[$name][$value] = $value;
        } elseif ('style' === $name) {
            if (!isset($attr[$name])) {
                $attr[$name] = [];
            }

            if (!is_array($value)) {
                $value = explode(':', $value);
                $k = trim($value[0]);
                $v = trim($value[1]);
                $v = trim($v, ';');
                $value = [$k => $v];
            }

            $attr[$name] = array_merge($attr[$name], $value);
        } else {
            $attr[$name] = $value;
        }

        $this->itemAttr[$itemId] = $attr;

        return $this;
    }

    public function getItemAttr(string $itemId, string $name = '')
    {
        return strlen($name) ? ($this->itemAttr[$itemId][$name] ?? null) : ($this->itemAttr[$itemId] ?? []);
    }
}
