@use crate::{
config::{Config, UrlKind},
data::Node,
templates::{info_html, instance_html, statics::index_css},
};

@(local: &[Node], nodes: &[Node], config: &Config)

<!doctype html>
<html>

<head lang="en">
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>@config.hostname() | ActivityPub Relay</title>
  <link rel="stylesheet" href="/static/@index_css.name" type="text/css" />
</head>

<body>
  <header>
    <div class="header-text">
      <h1>@Config::software_name()<span class="smaller">@Config::software_version()</span></h1>
      <p>on @config.hostname()</p>
    </div>
  </header>
  <main>
    @if !local.is_empty() || config.local_blurb().is_some() {
    <article>
      <h3>About</h3>
      <section class="local-explainer">
        @if let Some(blurb) = config.local_blurb() {
        @blurb
        } else {
        <p>These domains are run by the same admins as this relay.</p>
        }
      </section>
      @if !local.is_empty() {
      <ul>
        @for node in local {
        @if let Some(inst) = node.instance.as_ref() {
        <li>
          @:instance_html(inst, node.info.as_ref().map(|info| { info.software.as_ref() }), node.contact.as_ref(),
          &node.base)
        </li>
        } else {
        @if let Some(inf) = node.info.as_ref() {
        <li>
          @:info_html(inf, &node.base)
        </li>
        }
        }
        }
      </ul>
      }
    </article>
    }
    <article>
      <h3>Joining</h3>
      <section class="joining">
        @if config.restricted_mode() {
        <h4>
          This relay is Restricted
        </h4>
        <p>
          This relay is currently in Restricted Mode, which means servers need to be approved ahead of time by the relay
          administrator. Please contact the admin before attempting to join.
        </p>
        } else {
        <p>
          If you are the admin of a server that supports activitypub relays, you can add
          this relay to your server.
        </p>
        }
        <h4>Mastodon</h4>
        <p>
          Mastodon admins can add this relay by adding
        <pre>@config.generate_url(UrlKind::Inbox)</pre> in their relay settings.
        </p>
        <h4>Pleroma</h4>
        <p>
          Pleroma admins can add this relay by adding
        <pre>@config.generate_url(UrlKind::Actor)</pre> to their relay settings.
        </p>
        <h4>Others</h4>
        <p>
          Consult the documentation for your server. It's likely that it follows either
          Mastodon or Pleroma's relay formatting.
        </p>
      </section>
    </article>
    @if !nodes.is_empty() {
    <article>
      <h3>@nodes.len() Connected Servers</h3>
      <ul>
        @for node in nodes {
        @if let Some(inst) = node.instance.as_ref() {
        <li>
          @:instance_html(inst, node.info.as_ref().map(|info| { info.software.as_ref() }), node.contact.as_ref(),
          &node.base)
        </li>
        } else {
        @if let Some(inf) = node.info.as_ref() {
        <li>
          @:info_html(inf, &node.base)
        </li>
        }
        }
        }
      </ul>
    </article>
    }
  </main>
  <footer>
    @if let Some(blurb) = config.footer_blurb() {
    <div>@blurb</div>
    }
    <p>
      The source code for this project can be found at
      <a href="@config.source_code()">@config.source_code()</a>
    </p>
  </footer>
</body>

</html>