{use Nette\Application\UI\Presenter}
{use Tracy\Dumper}

<style class="tracy-debug">
	#tracy-debug .nette-RoutingPanel-grid {
		background: #FDF5CE;
		display: grid;
		grid-template-columns: auto 1fr auto auto;
		border: 1px solid #E6DFBF;
	}

	#tracy-debug .nette-RoutingPanel-grid-inner,
	#tracy-debug .nette-RoutingPanel-grid-columns {
		grid-column: 1 / span 4;
		display: grid;
		grid-template-columns: subgrid;
	}

	#tracy-debug .nette-RoutingPanel-grid-columns:nth-child(2n) {
		background: rgba(0,0,0,0.02);
	}

	#tracy-debug .nette-RoutingPanel-grid-header {
		color: #655E5E;
		background: #F4F3F1;
		font-size: 90%;
		font-weight: bold;
	}

	#tracy-debug .nette-RoutingPanel-grid-group-header {
		grid-column: 1 / span 4;
		font-size: 90%;
		font-weight: bold;
		text-align: center;
	}

	#tracy-debug .nette-RoutingPanel-grid-inner .nette-RoutingPanel-grid-inner {
		background: #23180007;
		box-shadow: 0 1px 20px 0px #00000040;
		border-right: 8px solid #0000002e;
	}

	#tracy-debug .nette-RoutingPanel-grid-columns > div {
		border-bottom: 1px solid #95770026;
		border-right: 1px solid #95770026;
		padding: 2px 5px;
	}

	#tracy-debug .nette-RoutingPanel-status-yes {
		background: #BDE678 !important;
	}

	#tracy-debug .nette-RoutingPanel-status-may {
		background: #C1D3FF !important;
	}

	#tracy-debug .nette-RoutingPanel-status-error {
		background: #ffd2c3 !important;
	}

	#tracy-debug .nette-RoutingPanel-symbol {
		text-align: right;
	}

	#tracy-debug .nette-RoutingPanel .tracy-dump.tracy-dump {
		padding: 0;
		margin: 0;
		border: none;
	}

	#tracy-debug .nette-RoutingPanel pre, #tracy-debug .nette-RoutingPanel code {
		display: inline;
		background: transparent;
	}

	#tracy-debug .nette-RoutingPanel-rel {
		background: #eee;
		white-space: nowrap;
	}
</style>

<h1>
	{if $matched === null}
		no route
	{elseif isset($matched[Presenter::PresenterKey])}
		{$matched[Presenter::PresenterKey]}:{$matched[Presenter::ActionKey] ?? Presenter::DefaultAction}
		{if isset($matched[Presenter::SignalKey])}{$matched[Presenter::SignalKey]}!{/if}
	{/if}
</h1>

<div class="tracy-inner nette-RoutingPanel">
<div class="tracy-inner-container">
	<p>
		<code>{$method}</code>
		<code>{$url->getBaseUrl()}<wbr><span class="nette-RoutingPanel-rel">{str_replace(['&amp;', '?'], ['<wbr>&amp;', '<wbr>?'], htmlspecialchars($url->getRelativeUrl()))|noescape}</span></code>
	</p>

	{if is_string($source)}
		<p>{$source} (class not found)</p>
	{elseif $source}
		<p><a href="{Tracy\Helpers::editorUri($source->getFileName(), $source->getStartLine())}">{$source instanceof ReflectionClass ? $source->getName() : $source->getDeclaringClass()->getName() . '::' . $source->getName() . '()'}</a></p>
	{/if}
</div>

<div class="tracy-inner-container">
	{if empty($routes)}
		<p>No routes defined.</p>
	{else}
		<div class="nette-RoutingPanel-grid">
			<div class="nette-RoutingPanel-grid-columns nette-RoutingPanel-grid-header">
				<div></div>
				<div>Mask / Class</div>
				<div>Defaults</div>
				<div>Matched as</div>
			</div>

			{define routeList $list, $path = ''}
				<div class="nette-RoutingPanel-grid-inner">
					{if $list[domain] || $list[module]}
						<div class="nette-RoutingPanel-grid-group-header">
							{if $list[domain]}domain = {$list[domain]}{/if}
							{if $list[module]}module = {$list[module]}{/if}
						</div>
					{/if}
					{do $path .= $list[path]}
					{foreach $list[routes] as $router}
						{if is_array($router)}
							{include routeList $router, $path}
						{else}
							{include route $router, $path}
						{/if}
					{/foreach}
				</div>
			{/define}

			{define route $route, $path}
				<div class="nette-RoutingPanel-grid-columns nette-RoutingPanel-status-{$route->matched}">
					<div class="nette-RoutingPanel-symbol" title="{=[yes => 'matched as first', may => 'suitable but not matched as first', no => '', oneway => one-way, error => error][$route->matched]}">
						{=[yes => '✓', may => '≈', no => '', oneway => '⛔', error => '❌'][$route->matched]}
					</div>

					<div>
						<code title="{$route->class}">
							{if $path !== ''}<small>{$path}</small>{/if}
							{isset($route->mask) ? str_replace(['/', '-'], ['<wbr>/', '<wbr>-'], htmlspecialchars($route->mask)) : str_replace('\\', '<wbr>\\', htmlspecialchars($route->class))|noescape}
						</code>
					</div>

					<div>
						<code>
							{foreach $route->defaults as $key => $value}
								{$key}&nbsp;=&nbsp;{if is_string($value)}{$value}<br>{Dumper::toHtml($value, [Dumper::COLLAPSE => true, Dumper::LIVE => true])}{/if}
							{/foreach}
						</code>
					</div>

					<div>
						{if $route->params}
							<code>
								{do $params = $route->params}
								{if isset($params[Presenter::PresenterKey])}
									<strong>{$params[presenter]}:{$params[Presenter::ActionKey] ?? Presenter::DefaultAction}</strong>
									<br>
									{do unset($params[Presenter::PresenterKey], $params[Presenter::ActionKey])}
								{/if}
								{foreach $params as $key => $value}
									{$key}&nbsp;=&nbsp;{if is_string($value)}{$value}<br>{Dumper::toHtml($value, [Dumper::COLLAPSE => true, Dumper::LIVE => true])}{/if}
								{/foreach}
							</code>
						{elseif $route->error}
							<strong>{$route->error->getMessage()}</strong>
						{/if}
					</div>
				</div>
			{/define}

			{include routeList $routes}
		</div>
	{/if}
</div>
</div>
