{% import "fmt" %}
{% import "path/filepath" %}
{% import "sort" %}

{% import "github.com/bouncepaw/mycorrhiza/cfg" %}
{% import "github.com/bouncepaw/mycorrhiza/hyphae" %}
{% import "github.com/bouncepaw/mycorrhiza/user" %}
{% import "github.com/bouncepaw/mycorrhiza/util" %}
{% import "github.com/bouncepaw/mycorrhiza/l18n" %}

{% func BaseHTML(title, body string, lc *l18n.Localizer, u *user.User, headElements ...string) %}
<!doctype html>
<html lang="{%s lc.Locale %}">
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title>{%s title %}</title>
		<link rel="shortcut icon" href="/static/favicon.ico">
		<link rel="stylesheet" href="/static/style.css">
		<script src="/static/shortcuts.js"></script>
		{% for _, el := range headElements %}{%s= el %}{% endfor %}
	</head>
	<body>
		<header>
			<nav class="main-width top-bar">
				<ul class="top-bar__wrapper">
					<li class="top-bar__section top-bar__section_home">
						<div class="top-bar__home-link-wrapper">
							<a class="top-bar__home-link" href="/">{%s cfg.WikiName %}</a>
						</div>
					</li>
					<li class="top-bar__section top-bar__section_search">
						<form class="top-bar__search" method="GET" action="/title-search">
							<input type="text" name="q" placeholder="{%s lc.Get("ui.title_search") %}" class="top-bar__search-bar">
						</form>
					</li>
					<li class="top-bar__section top-bar__section_auth">
					{% if cfg.UseAuth %}
						<ul class="top-bar__auth auth-links">
							<li class="auth-links__box auth-links__user-box">
								{% if u.Group == "anon" %}
								<a href="/login" class="auth-links__link auth-links__login-link">{%s lc.Get("ui.login") %}</a>
								{% else %}
								<a href="/hypha/{%s cfg.UserHypha %}/{%s u.Name %}" class="auth-links__link auth-links__user-link">{%s util.BeautifulName(u.Name) %}</a>
								{% endif %}
							</li>
							{% if cfg.AllowRegistration && u.Group == "anon" %}
							<li class="auth-links__box auth-links__register-box">
								<a href="/register" class="auth-links__link auth-links__register-link">{%s lc.Get("ui.register") %}</a>
							</li>
							{% endif %}
						</ul>
					{% endif %}
					</li>
					<li class="top-bar__section top-bar__section_highlights">
						<ul class="top-bar__highlights">
						{%- for _, link := range cfg.HeaderLinks -%}
						{% if link.Href != "/" %}
							<li class="top-bar__highlight">
								<a class="top-bar__highlight-link" href="{%s link.Href %}">{%s link.Display %}</a>
							</li>
						{% endif %}
						{%- endfor -%}
						</ul>
					</li>
				</ul>
			</nav>
		</header>
		{%s= body %}
		<template id="dialog-template">
			<div class="dialog-backdrop"></div>
			<div class="dialog" tabindex="0">
				<div class="dialog__header">
					<h1 class="dialog__title"></h1>
					<button class="dialog__close-button" aria-label="{%s lc.Get("ui.close_dialog") %}"></button>
				</div>

				<div class="dialog__content"></div>
			</div>
		</template>
		{%= CommonScripts() %}
		<script src="/static/view.js"></script>
	</body>
</html>
{% endfunc %}

{% func TitleSearchHTML(query string, generator func(string) <-chan string, lc *l18n.Localizer) %}
<div class="layout">
<main class="main-width title-search">
	<h1>{%s lc.Get("ui.search_results_query", &l18n.Replacements{"query": query})%}</h1>
	<p>{%s lc.Get("ui.search_results_desc")%}</p>
	<ul class="title-search__results">
	{% for hyphaName := range generator(query) %}
		<li class="title-search__entry">
			<a class="title-search__link wikilink" href="/hypha/{%s hyphaName %}">{%s util.BeautifulName(hyphaName) %}</a>
		</li>
	{% endfor %}
</main>
</div>
{% endfunc %}

It outputs a poorly formatted JSON, but it works and is valid.
{% func TitleSearchJSON(query string, generator func(string) <-chan string) %}
{% code
	// Lol
	counter := 0
%}
{
	"source_query": "{%s query %}",
	"results": [
	{% for hyphaName := range generator(query) %}
		{% if counter > 0 %}, {% endif %}{
			"canonical_name": "{%s hyphaName %}",
			"beautiful_name": "{%s util.BeautifulName(hyphaName) %}",
			"url": "{%s cfg.URL + "/hypha/" + hyphaName %}"
		}{% code counter++ %}
	{% endfor %}
	]
}
{% endfunc %}

{% func BacklinksHTML(hyphaName string, generator func(string) <-chan string, lc *l18n.Localizer) %}
<div class="layout">
<main class="main-width backlinks">
	<h1>{%s= lc.Get(
		"ui.backlinks_heading",
		&l18n.Replacements{
			"hypha_link": fmt.Sprintf(
				`<a href="/hypha/%s">%s</a>`,
				hyphaName,
				util.BeautifulName(hyphaName),
			),
		},
	)%}</h1>
	<p>{%s lc.Get("ui.backlinks_desc")%}</p>
	<ul class="backlinks__list">
	{% for hyphaName := range generator(hyphaName) %}
		<li class="backlinks__entry">
			<a class="backlinks__link wikilink" href="/hypha/{%s hyphaName %}">{%s util.BeautifulName(hyphaName) %}</a>
		</li>
	{% endfor %}
	</ul>
</main>
</div>
{% endfunc %}

{% func HelpHTML(content, lang string, lc *l18n.Localizer) %}
<div class="layout">
<main class="main-width help">
	<article>
	{%s= content %}
	</article>
</main>
{%s= helpTopicsHTML(lang, lc) %}
</div>
{% endfunc %}

{% func HelpEmptyErrorHTML(lc *l18n.Localizer) %}
<h1>{%s lc.Get("help.empty_error_title") %}</h1>
<p>{%s lc.Get("help.empty_error_line_1") %}</p>
<p>{%s lc.Get("help.empty_error_line_2a") %} <a class="wikilink wikilink_external wikilink_https" href="https://github.com/bouncepaw/mycorrhiza">{%s lc.Get("help.empty_error_link") %}</a> {%s lc.Get("help.empty_error_line_2b") %}</p>
{% endfunc %}

{% func helpTopicsHTML(lang string, lc *l18n.Localizer) %}
<aside class="help-topics layout-card">
	<h2 class="layout-card__title">{%s lc.GetWithLocale(lang, "help.topics") %}</h2>
	<ul class="help-topics__list">
		<li><a href="/help/{%s lang %}">{%s lc.GetWithLocale(lang, "help.main") %}</a></li>
		<li><a href="/help/{%s lang %}/hypha">{%s lc.GetWithLocale(lang, "help.hypha") %}</a>
			<ul>
				<li><a href="/help/{%s lang %}/attachment">{%s lc.GetWithLocale(lang, "help.attachment") %}</a></li>
			</ul>
		</li>
		<li><a href="/help/{%s lang %}/mycomarkup">{%s lc.GetWithLocale(lang, "help.mycomarkup") %}</a></li>
		<li>{%s lc.GetWithLocale(lang, "help.interface") %}
			<ul>
				<li><a href="/help/{%s lang %}/prevnext">{%s lc.GetWithLocale(lang, "help.prevnext") %}</a></li>
				<li><a href="/help/{%s lang %}/top_bar">{%s lc.GetWithLocale(lang, "help.top_bar") %}</a></li>
				<li><a href="/help/{%s lang %}/sibling_hyphae_section">{%s lc.GetWithLocale(lang, "help.sibling_hyphae") %}</a></li>
			</ul>
		</li>
		<li>{%s lc.GetWithLocale(lang, "help.special_pages") %}
			<ul>
				<li><a href="/help/{%s lang %}/recent_changes">{%s lc.GetWithLocale(lang, "help.recent_changes") %}</a></li>
				<li><a href="/help/{%s lang %}/feeds">{%s lc.GetWithLocale(lang, "help.feeds") %}</a></li>
			</ul>
		</li>
		<li>{%s lc.GetWithLocale(lang, "help.configuration") %}
			<ul>
				<li><a href="/help/{%s lang %}/config_file">{%s lc.GetWithLocale(lang, "help.config_file") %}</a></li>
				<li><a href="/help/{%s lang %}/lock">{%s lc.GetWithLocale(lang, "help.lock") %}</a></li>
				<li><a href="/help/{%s lang %}/whitelist">{%s lc.GetWithLocale(lang, "help.whitelist") %}</a></li>
				<li><a href="/help/{%s lang %}/telegram">{%s lc.GetWithLocale(lang, "help.telegram") %}</a></li>
			</ul>
		</li>
	</ul>
</aside>
{% endfunc %}

{% func helpTopicBadgeHTML(lang, topic string) %}
<a class="help-topic-badge" href="/help/{%s lang %}/{%s topic %}">?</a>
{% endfunc %}

{% func UserListHTML(lc *l18n.Localizer) %}
<div class="layout">
<main class="main-width user-list">
	<h1>{%s lc.Get("ui.users_heading") %}</h1>
{% code
var (
	admins = make([]string, 0)
	moderators = make([]string, 0)
	editors = make([]string, 0)
)
for u := range user.YieldUsers() {
	switch u.Group {
	case "admin":
		admins = append(admins, u.Name)
	case "moderator":
		moderators = append(moderators, u.Name)
	case "editor", "trusted":
		editors = append(editors, u.Name)
	}
}
sort.Strings(admins)
sort.Strings(moderators)
sort.Strings(editors)
%}
	<section>
		<h2>{%s lc.Get("ui.users_admins") %}</h2>
		<ol>{% for _, name := range admins %}
			<li><a href="/hypha/{%s cfg.UserHypha %}/{%s name %}">{%s name %}</a></li>
		{% endfor %}</ol>
	</section>
	<section>
		<h2>{%s lc.Get("ui.users_moderators") %}</h2>
		<ol>{% for _, name := range moderators %}
			<li><a href="/hypha/{%s cfg.UserHypha %}/{%s name %}">{%s name %}</a></li>
		{% endfor %}</ol>
	</section>
	<section>
		<h2>{%s lc.Get("ui.users_editors") %}</h2>
		<ol>{% for _, name := range editors %}
			<li><a href="/hypha/{%s cfg.UserHypha %}/{%s name %}">{%s name %}</a></li>
		{% endfor %}</ol>
	</section>
</main>
</div>
{% endfunc %}

{% func HyphaListHTML(lc *l18n.Localizer) %}
<div class="layout">
<main class="main-width">
	<h1>{%s lc.Get("ui.list_heading") %}</h1>
	<p>{%s lc.GetPlural("ui.list_desc", hyphae.Count()) %}</p>
	<ul class="hypha-list">
		{% code 
		hyphaNames := make(chan string)
		sortedHypha := hyphae.PathographicSort(hyphaNames)
		for hypha := range hyphae.YieldExistingHyphae() {
			hyphaNames <- hypha.Name
		}
		close(hyphaNames)
		%}
		{% for hyphaName := range sortedHypha %}
		{% code hypha := hyphae.ByName(hyphaName) %}
		<li class="hypha-list__entry">
			<a class="hypha-list__link" href="/hypha/{%s hypha.Name %}">{%s util.BeautifulName(hypha.Name) %}</a>
			{% if hypha.BinaryPath != "" %}
			<span class="hypha-list__amnt-type">{%s filepath.Ext(hypha.BinaryPath)[1:] %}</span>
			{% endif %}
		</li>
		{% endfor %}
	</ul>
</main>
</div>
{% endfunc %}

{% func AboutHTML(lc *l18n.Localizer) %}
<div class="layout">
<main class="main-width">
	<section>
		<h1>{%s lc.Get("ui.about_title", &l18n.Replacements{"name": cfg.WikiName}) %}</h1>
		<ul>
			<li><b>{%s= lc.Get("ui.about_version", &l18n.Replacements{"pre": "<a href=\"https://mycorrhiza.wiki\">", "post": "</a>"}) %}</b> 1.7.0</li>
		{%- if cfg.UseAuth -%}
			<li><b>{%s lc.Get("ui.about_usercount") %}</b> {%dul user.Count() %}</li>
			<li><b>{%s lc.Get("ui.about_homepage") %}</b> <a href="/">{%s cfg.HomeHypha %}</a></li>
			<li><b>{%s lc.Get("ui.about_admins") %}</b> {%- for i, username := range user.ListUsersWithGroup("admin") -%}
				{%- if i > 0 -%}<span aria-hidden="true">, </span>
				{%- endif -%}
				<a href="/hypha/{%s cfg.UserHypha %}/{%s username %}">{%s username %}</a>{%- endfor -%}</li>
		{%- else -%}
			<li>{%s lc.Get("ui.about_noauth") %}</li>
		{%- endif -%}
		</ul>
		<p>{%s= lc.Get("ui.about_hyphae", &l18n.Replacements{"link": "<a href=\"/list\">/list</a>"}) %}</p>
	</section>
</main>
</div>
{% endfunc %}

{% func CommonScripts() %}
{% for _, scriptPath := range cfg.CommonScripts %}
<script src="{%s scriptPath %}"></script>
{% endfor %}
{% endfunc %}