<?php
declare(strict_types=1);

namespace Nette\Bridges\ApplicationTracy;

use Nette\Application\UI\Presenter;
use Tracy;
use Tracy\Dumper;
use Tracy\Helpers;

?>
<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>
<?php if ($matched === null): ?>
	no route
<?php elseif (isset($matched[Presenter::PresenterKey])): ?>
	<?= Helpers::escapeHtml($matched[Presenter::PresenterKey] . ':' . ($matched[Presenter::ActionKey] ?? Presenter::DefaultAction) . (isset($matched[Presenter::SignalKey]) ? " {$matched[Presenter::SignalKey]}!" : '')) ?>
<?php endif ?>
</h1>

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

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

<div class="tracy-inner-container">
<?php if (empty($routes)): ?>
	<p>No routes defined.</p>

<?php 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>
<?php

$show = function ($info, $path = '') use (&$show) {
	if (is_array($info)) {
?>
		<div class="nette-RoutingPanel-grid-inner">
		<?php if ($info['domain'] || $info['module']): ?>
			<div class="nette-RoutingPanel-grid-group-header">
			<?= $info['domain'] ? 'domain = ' . Helpers::escapeHtml($info['domain']) : '' ?>
			<?= $info['module'] ? ' module = ' . Helpers::escapeHtml($info['module']) : '' ?>
			</div>
		<?php endif ?>
		<?php
		$path .= $info['path'];
		foreach ($info['routes'] as $route) {
			$show($route, $path);
		}
		?>
		</div>
<?php
		return;
	}

	$route = $info;
?>
	<div class="nette-RoutingPanel-grid-columns nette-RoutingPanel-status-<?= $route->matched ?>">
		<div class="nette-RoutingPanel-symbol" title="<?= Helpers::escapeHtml(['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="<?= Helpers::escapeHtml($route->class) ?>"><?=
			$path === '' ? '' : '<small>' . Helpers::escapeHtml($path) . '</small>',
			isset($route->mask) ? str_replace(['/', '-'], ['<wbr>/', '<wbr>-'], Helpers::escapeHtml($route->mask)) : str_replace('\\', '<wbr>\\', Helpers::escapeHtml($route->class))
		?></code></div>

		<div><code>
		<?php foreach ($route->defaults as $key => $value): ?>
			<?= Helpers::escapeHtml($key), '&nbsp;=&nbsp;', is_string($value) ? Helpers::escapeHtml($value) . '<br>' : Dumper::toHtml($value, [Dumper::COLLAPSE => true, Dumper::LIVE => true]) ?>
		<?php endforeach ?>
		</code></div>

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

$show($routes);

?>
	</div>
<?php endif ?>
</div>
</div>
