<?php

namespace Plainwork\Ui\Renderer;

use Plainwork\Http\Request;
use Plainwork\Http\Server;
use Plainwork\Http\RequestFactory;
use Plainwork\Ui\Component\HtmlElement;
use Plainwork\Ui\Component\Menu;
use Plainwork\Ui\Component\Link;
use Plainwork\Ui\Ui;
use Plainwork\Ui\Html;

class MenuRenderer
{
    public $ui = Ui::class;
    public $requestFactory = RequestFactory::class;
    public $server = Server::class;

    public function __invoke(Menu $el, Request $request)
    {
        $ret = '';

        $isPrintView = $this->ui->isPrintView();
        if ($isPrintView) {
            return $ret;
        }

        $html = new Html();

        $collapsibleLabel = $el->collapsibleLabel;
        // if (null !== $collapsibleLabel) {
            // $el->setVertical();
        // }

        $items = $el->getItems();
        $items = array_map(fn($e) => $this->finalizeLink($e, $request), $items);
        $items = array_filter($items, fn($e) => $this->isItemAllowed($e, $request));
        if (!$items) {
            return $ret;
        }

        $isVertical = $el->isVertical();

        $ret = new HtmlElement('div');
        $ret->withAttr('class', 'pw-menu');
        if ($isVertical) {
            $ret->withAttr('class', 'pw-block-list');
        } else {
            $ret->withAttr('class', 'pw-inline-list');
        }
        // $ret->withAttr('class', 'pw-valign-top');

        foreach ($items as $item) {
            if (method_exists($item, 'setMenu')) {
                $item->setMenu(true);
            }
            $ret_ = (new HtmlElement('div', $item));
            $ret->append($ret_);
        }

        if (null !== $collapsibleLabel) {
            // if (count($items) > 1) {
                $summary = $html->el('summary', $collapsibleLabel);
                $ret = $html->el('details', $summary, $ret);
            // }
        }

        $attr = $el->getAttr();
        foreach ($attr as $k_ => $v_) {
            $ret->withAttr($k_, $v_);
        }

        return $ret;
    }

    private function finalizeLink($item, Request $request)
    {
        if (!($item instanceof Link)) {
            return $item;
        }

        $path = $item->getPath();
        if (null === $path) {
            return $item;
        }

        $request_ = $this->requestFactory->fromOrParent($request, $path, $item->getParams());
        $path_ = $request_->getPath();
        if ($path_ !== $path) {
            $item->setPath($path_);
        }

        return $item;
    }

    public function isItemAllowed($item, Request $request): bool
    {
        if (!($item instanceof Link)) {
            return true;
        }

        $path = $item->getPath();
        if (null === $path) {
            return true;
        }

        // $request_ = $this->requestFactory->from($request, $path, $item->getParams());
        $request_ = $this->requestFactory->fromOrParent($request, $path, $item->getParams());
// $path_ = $request_->getPath();
// echo "CHECKAM '$path': '$path_'<br>";

        $can = $this->server->can($request_);
        $ret = (false === $can) ? false : true;

        return $ret;
    }
}
