<?php

namespace Plainwork\Ui\Renderer;

use Plainwork\Ui\Component\Link;
use Plainwork\Ui\Component\HtmlElement;
use Plainwork\Ui\Component\InlineList;
use Plainwork\Core\App;
use Plainwork\Http\Uri;
use Plainwork\Http\RequestFactory;
use Plainwork\Http\Request;
use Plainwork\Ui\Ui;
use Plainwork\Ui\Html;

final class LinkRenderer
{
    public $app = App::class;
    public $ui = Ui::class;
    public $uri = Uri::class;
    public $requestFactory = RequestFactory::class;

    public function __invoke(Link $el, Request $request): string
    {
        $isHidden = $this->ui->isPrintView();

        $ret = [];

        $isPrintView = $this->ui->isPrintView();
        $isMenu = $el->isMenu();
        $isDisabled = $el->isDisabled();

        if ($isPrintView) {
            if ($isMenu) {
                return '';
            }
        }

        $jsConfirm = $el->getJsConfirm();
        if (null !== $jsConfirm) {
            $el->withAttr('onclick', 'if (!confirm(\'' . $jsConfirm . '\')){event.stopPropagation(); return false;}');
        }

        $href = null;
        $myRequest = null;
        $path_ = null;

        if ($isPrintView) {
        } elseif ($isDisabled) {
        } else {
            $fullUri = $el->isFull();
            if (strlen($fullUri)) {
                $href = $fullUri;
            } else {
                $path_ = $el->getPath();
                if (null !== $path_) {
                    $params = $el->getParams();
                    // $request_ = $this->requestFactory->from($request, $path, $params);
                    // $path = $request_->getPath();
                    // $params = $request_->getQueryParams();
                    // _print_r($params);
                    // $path_ = $request->resolvePath($path_);

                // if ('admin/record/total' === $path_) {
                    // _print_r($request);
                    $request_ = $this->requestFactory->from($request, $path_, $params);
                    $path_ = $request_->getPath();

                    $params_ = $request_->getQueryParams();
// if (isset($params['cal'])) {
    // _print_r($request);
    // _print_r($request_);
    // _print_r($params_);
// }
                    // _print_r($params);
                    // exit;
                // }

                    $href = $this->uri->to($path_, $params_);
                }
            }
        }

        $label = $el->getLabel();
        foreach (array_keys($label) as $ii) {
            if (is_array($label[$ii]) && (null !== $path_)) {
                $request_ = $this->requestFactory->from($request, $path_, $params);
                $label[$ii] = $this->app->call($label[$ii], $request_);
            }
        }

    // render each part of the label
        $iis = array_keys($label);
        foreach ($iis as $ii) {
            $label[$ii] = $this->ui->render($label[$ii], $request);
            if (null === $label[$ii]) {
                unset($label[$ii]);
            }
        }

        if (count($label) > 1) {
            $iis = array_keys($label);
            foreach ($iis as $ii) {
                if ('<' !== substr($label[$ii], 0, 1)) {
                    $label[$ii] = '<span>' . $label[$ii] . '</span>';
                }
            }
            $label = '<span class="pw-label">' . join('', $label) . '</span>';
            $el->withAttr('class', 'pw-multipart-el');
        } else {
            $label = current($label);
            $label = $this->ui->render($label, $request);
        }

        if ($isMenu) {
            $el->withAttr('class', 'pw-menu');
        }

        $attr = $el->getAttr();
        foreach (array_keys($attr) as $k_) {
            while (is_object($attr[$k_])) {
                $attr[$k_] = $this->ui->render($attr[$k_], $request);
            }
        }

        $targetBlank = (isset($attr['target']) && ('_blank' == $attr['target'])) ? true : false;
        if ($targetBlank) {
            // $el->appendIcon('&nearr;');
            // $el->appendIcon('&rarr;');
        }

        if (null !== $href) {
            $attr['href'] = $href;
        }

        $isDanger = $el->isDanger();
        if ($isDanger) {
            $ret[] = '<strong>';
        }

        $ret[] = '<a ' . Html::attr($attr) . '>';

        $isDeleted = $el->isDeleted();
        if ($isDeleted) {
            $ret[] = '<del>';
        }

        if ((null !== $label) && strlen($label)) {
            $ret[] = $label;
        }

        if ($isDeleted) {
            $ret[] = '</del>';
        }
        $ret[] = '</a>';

        if ($isDanger) {
            $ret[] = '</strong>';
        }

        $ret = join('', $ret);

        return $ret;
    }
}
