<?php

namespace Plainwork\Ui;

use Plainwork\Ui\Component\HtmlElement;
use Plainwork\Ui\Component\Form\Form;
use Plainwork\Ui\Component\Form\FormErrors;
use Plainwork\Ui\Component\Form\ColorInput;
use Plainwork\Ui\Component\Form\NumberInput;
use Plainwork\Ui\Component\Form\FileInput;
use Plainwork\Ui\Component\Form\DatePeriodInput;
use Plainwork\Ui\Component\Form\DateInput;
use Plainwork\Ui\Component\Form\TimeInput;
use Plainwork\Ui\Component\Form\TextInput;
use Plainwork\Ui\Component\Form\TextareaInput;
use Plainwork\Ui\Component\Form\ButtonInput;
use Plainwork\Ui\Component\Form\SelectInput;
use Plainwork\Ui\Component\Form\RadioInput;
use Plainwork\Ui\Component\Form\RadioSetInput;
use Plainwork\Ui\Component\Form\CheckboxInput;
use Plainwork\Ui\Component\Form\CheckboxSetInput;
use Plainwork\Ui\Component\Form\PasswordInput;
use Plainwork\Ui\Component\Form\Button;
use Plainwork\Ui\Component\Collection;
use Plainwork\Ui\Component\CardList;
use Plainwork\Ui\Component\BlockList;
use Plainwork\Ui\Component\Card;
use Plainwork\Ui\Component\Badge;
use Plainwork\Ui\Component\Grid;
use Plainwork\Ui\Component\Ul;
use Plainwork\Ui\Component\Ol;
use Plainwork\Ui\Component\Breadcrumbs;
use Plainwork\Ui\Component\InlineList;
use Plainwork\Ui\Component\Pager;
use Plainwork\Ui\Component\Link;
use Plainwork\Ui\Component\Menu;
use Plainwork\Ui\Component\TabMenu;
use Plainwork\Ui\Component\Main;
use Plainwork\Ui\Component\Dl;
use Plainwork\Ui\Component\Table;
use Plainwork\Ui\Component\Icon;

class Html
{
    public function el($tag, ...$args)
    {
        return new HtmlElement($tag, ...$args);
    }

    public function main(...$args): Main
    {
        return new Main(...$args);
    }

    public function card(...$args): Card
    {
        return new Card(...$args);
    }

    public function badge(...$args): Badge
    {
        return new Badge(...$args);
    }

    public function breadcrumbs(...$args): Breadcrumbs
    {
        return new Breadcrumbs(...$args);
    }

    public function grid(...$args): Grid
    {
        return new Grid(...$args);
    }

    public function table(...$args): Table
    {
        return new Table(...$args);
    }

    public function pager(...$args): Pager
    {
        return new Pager(...$args);
    }

    public function ul(...$args): Ul
    {
        return new Ul(...$args);
    }

    public function ol(...$args): Ol
    {
        return new Ol(...$args);
    }

    public function button(...$args): Button
    {
        return new Button(...$args);
    }

    public function buttonInput(...$args): ButtonInput
    {
        return new ButtonInput(...$args);
    }

    public function menu(...$args): Menu
    {
        return new Menu(...$args);
    }

    public function tabMenu(...$args): TabMenu
    {
        return new TabMenu(...$args);
    }

    public function blockList(...$args): BlockList
    {
        return new BlockList(...$args);
    }

    public function cardList(...$args): CardList
    {
        return new CardList(...$args);
    }

    public function collection(...$args): Collection
    {
        return new Collection(...$args);
    }

    public function inlineList(...$args): InlineList
    {
        return new InlineList(...$args);
    }

    public function dl(...$args): Dl
    {
        return new Dl(...$args);
    }

    public function numberInput(...$args): NumberInput
    {
        return new NumberInput(...$args);
    }

    public function fileInput(...$args): FileInput
    {
        return new FileInput(...$args);
    }

    public function checkboxInput(...$args): CheckboxInput
    {
        return new CheckboxInput(...$args);
    }

    public function checkboxSetInput(...$args): CheckboxSetInput
    {
        return new CheckboxSetInput(...$args);
    }

    public function radioSetInput(...$args): RadioSetInput
    {
        return new RadioSetInput(...$args);
    }

    public function radioInput(...$args): RadioInput
    {
        return new RadioInput(...$args);
    }

    public function selectInput(...$args): SelectInput
    {
        return new SelectInput(...$args);
    }

    public function colorInput(...$args): ColorInput
    {
        return new ColorInput(...$args);
    }

    public function textInput(...$args): TextInput
    {
        return new TextInput(...$args);
    }

    public function dateInput(...$args): DateInput
    {
        return new DateInput(...$args);
    }

    public function datePeriodInput(...$args): DatePeriodInput
    {
        return new DatePeriodInput(...$args);
    }

    public function timeInput(...$args): TimeInput
    {
        return new TimeInput(...$args);
    }

    public function passwordInput(...$args): PasswordInput
    {
        return new PasswordInput(...$args);
    }

    public function textareaInput(...$args): TextareaInput
    {
        return new TextareaInput(...$args);
    }

    public function form(...$args): Form
    {
        return new Form(...$args);
    }

    public function formErrors(...$args): FormErrors
    {
        return new FormErrors(...$args);
    }

    public function link(...$args): Link
    {
        return new Link(...$args);
    }

    public function icon(...$args): Icon
    {
        return new Icon(...$args);
    }

    // public function __call($name, $args)
    // {
        // return new HtmlElement($name, ...$args);
    // }

    // public static function __callStatic($name, $args)
    // {
        // return new HtmlElement($name, ...$args);
    // }

    public static function attr(array $attr): string
    {
        $ret = [];

        foreach ($attr as $k => $v) {
            if ('class' == $k) {
                if (is_array($v)) {
                    $va = [];
                    foreach ($v as $k2 => $v2) {
                        if (! is_numeric($k2)) {
                            if (! $v2) {
                                continue;
                            }
                            $v2 = $k2;
                        }
                        if (! in_array($v2, $va)) {
                            $va[] = $v2;
                        }
                    }
                    $v = join(' ', $va);
                }
            } elseif ('style' == $k) {
                if (is_array($v)) {
                    $va = [];
                    foreach ($v as $k2 => $v2) {
                        if (null === $v2) {
                            continue;
                        }
                        $va[] = $k2 . ':' . $v2 . ';';
                    }
                    if (!$va) {
                        continue;
                    }
                    $v = join('', $va);
                }
            }

            if (is_bool($v)) {
                if ($v) {
                    $ret[] = $k;
                }
            } else {
                if (is_array($v)) {
                    $v = join(' ', $v);
                }

                $v_ = $v;
                if (!in_array($k, ['href', 'onclick'])) {
                    $v_ = esc_attr($v_);
                }
                $ret[] = $k . '="' . $v_ . '"';
            }
        }
        $ret = join(' ', $ret);

        return $ret;
    }
}
