<?php

namespace Plainwork\Ui\Component;

abstract class AbstractTag
{
    protected string $tag = 'div';
    private array $attr = [];
    private bool $isDeleted = false;
    private array $children = [];
    private bool $alignCenter = false;
    private bool $alignEnd = false;

    public function __construct($content = null)
    {
        if (null !== $content) {
            $this->append($content);
        }
    }

    public function withTag(string $tag): self
    {
        $ret = clone $this;
        $ret->tag = $tag;

        return $ret;
    }

    public function setTag(string $tag): self
    {
        $this->tag = $tag;

        return $this;
    }

    public function getTag()
    {
        return $this->tag;
    }

    public function withAttr(string $name, $value): self
    {
        return $this->setAttr($name, $value);
    }

    public function removeAttr(string $name): self
    {
        unset($this->attr[$name]);

        return $this;
    }

    public function unsetAttr(string $name): self
    {
        unset($this->attr[$name]);

        return $this;
    }

    public function setAttr(string $name, $value): self
    {
        if ('class' === $name) {
            if (!isset($this->attr[$name])) {
                $this->attr[$name] = [];
            }
            if (!is_array($this->attr[$name])) {
                $this->attr[$name] = [$this->attr[$name] => $this->attr[$name]];
            }
            if (is_array($value)) {
                $this->attr[$name] = array_merge($this->attr[$name], $value);
            } else {
                $this->attr[$name][$value] = $value;
            }
        } elseif ('style' === $name) {
            if (!isset($this->attr[$name])) {
                $this->attr[$name] = [];
            }

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

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

        return $this;
    }

    public function getAttr(string $name = '')
    {
        return strlen($name) ? ($this->attr[$name] ?? null) : $this->attr;
    }

    public function setDeleted(bool $on = true): self
    {
        $this->isDeleted = $on;

        return $this;
    }

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

    final public function append($el): self
    {
        $this->children[] = $el;

        return $this;
    }

    public function prepend($el): self
    {
        array_unshift($this->children, $el);

        return $this;
    }

    public function getChildren(): array
    {
        return $this->children;
    }

    public function setContent($content): self
    {
        if ($this->children) {
            $this->children = [];
        }
        $this->append($content);

        return $this;
    }

    public function setAlignCenter(bool $on = true): self
    {
        if ($on) {
            $this->alignCenter = true;
            $this->alignEnd = false;
        } else {
            $this->alignCenter = false;
        }

        return $this;
    }

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

    public function setAlignEnd(bool $on = true): self
    {
        if ($on) {
            $this->alignEnd = true;
            $this->alignCenter = false;
        } else {
            $this->alignEnd = false;
        }

        return $this;
    }

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