{{ define "main" }}
<article>
  {{ if .Params.showHero | default (.Site.Params.article.showHero | default false) }}
  {{ $heroStyle := print "partials/hero/" .Site.Params.article.heroStyle ".html" }}
  {{ if templates.Exists $heroStyle }}
  {{ partial $heroStyle . }}
  {{ else }}
  {{ partial "partials/hero/basic.html" . }}
  {{ end }}
  {{- end -}}
  
  <header id="single_header" class="mt-5 max-w-prose">
    {{ if .Params.showBreadcrumbs | default (.Site.Params.article.showBreadcrumbs | default false) }}
    {{ partial "breadcrumbs.html" . }}
    {{ end }}
    <h1 class="mt-0 text-4xl font-extrabold text-neutral-900 dark:text-neutral">
      {{ .Title | emojify }}
    </h1>
    <div class="mt-1 mb-6 text-base text-neutral-500 dark:text-neutral-400 print:hidden">
      {{ partial "article-meta/basic.html" (dict "context" . "scope" "single") }}
    </div>
  </header>
  <section class="flex flex-col max-w-full mt-0 prose dark:prose-invert lg:flex-row">
    {{ if and (.Params.showTableOfContents | default (.Site.Params.article.showTableOfContents | default false)) (in
    .TableOfContents "<ul") }} 
    <div class="order-first sm:max-w-prose lg:ml-auto px-0 lg:order-last lg:max-w-xs ltr:lg:pl-8 rtl:lg:pr-8">
      <div class="toc ltr:pl-5 rtl:pr-5 print:hidden lg:sticky {{ if eq .Site.Params.header.layout "fixed" }} lg:top-[140px] {{ else }} lg:top-10 {{ end }}">
        {{ partial "toc.html" . }}
      </div>
      </div>
      {{ end }}
      <div class="min-w-0 min-h-0 max-w-prose">
        {{ partial "series.html" . }}
        {{ .Content | emojify }}
        </br></br>
        {{ partial "series-closed.html" . }}
      </div>
      <script>

        var liked_article = false

        if (typeof auth !== 'undefined') {
          var oid = "views_{{ .File.Path }}"
          var id = oid ? oid.replaceAll("/", "-") : oid

          var viewed = localStorage.getItem(id);

          if (!viewed) {
            auth.signInAnonymously()
              .then(() => {
                var docRef = db.collection('views').doc(id)
                localStorage.setItem(id, true);
                docRef.get().then((doc) => {
                  if (doc.exists) {
                    db.collection('views').doc(id).update({
                      views: firebase.firestore.FieldValue.increment(1)
                    });
                  } else {
                    db.collection('views').doc(id).set({ views: 1 })
                  }
                }).catch((error) => {
                  console.log("Error getting document:", error);
                });
              })
              .catch((error) => {
                var errorCode = error.code;
                var errorMessage = error.message;
                console.error(errorCode, errorMessage)
              });
          }

          var oid_likes = "likes_{{ .File.Path }}"
          var id_likes = oid_likes ? oid_likes.replaceAll("/", "-") : oid_likes

          var liked = localStorage.getItem(id_likes);

          if (liked) {
            liked_article = true
            document.querySelectorAll("span[id='likes_button_heart']")[0].style.display = ""
            document.querySelectorAll("span[id='likes_button_emtpty_heart']")[0].style.display = "none"
            document.querySelectorAll("span[id='likes_button_text']")[0].innerText = ""
          }

        }

        function like_article(id_likes) {
          auth.signInAnonymously()
            .then(() => {
              var docRef = db.collection('likes').doc(id_likes)
              docRef.get().then((doc) => {
                liked_article = true
                localStorage.setItem(id_likes, true);
                document.querySelectorAll("span[id='likes_button_heart']")[0].style.display = ""
                document.querySelectorAll("span[id='likes_button_emtpty_heart']")[0].style.display = "none"
                document.querySelectorAll("span[id='likes_button_text']")[0].innerText = ""
                if (doc.exists) {
                  db.collection('likes').doc(id_likes).update({
                    likes: firebase.firestore.FieldValue.increment(1)
                  });
                } else {
                  db.collection('likes').doc(id_likes).set({ likes: 1 })
                }
              }).catch((error) => {
                console.log("Error getting document:", error);
              });
            })
            .catch((error) => {
              var errorCode = error.code;
              var errorMessage = error.message;
              console.error(errorCode, errorMessage)
            });
        }

        function remove_like_article(id_likes) {
          auth.signInAnonymously()
            .then(() => {
              var docRef = db.collection('likes').doc(id_likes)
              docRef.get().then((doc) => {
                liked_article = false
                localStorage.removeItem(id_likes);
                document.querySelectorAll("span[id='likes_button_heart']")[0].style.display = "none"
                document.querySelectorAll("span[id='likes_button_emtpty_heart']")[0].style.display = ""
                document.querySelectorAll("span[id='likes_button_text']")[0].innerText = "\xa0Like"
                if (doc.exists) {
                  db.collection('likes').doc(id_likes).update({
                    likes: firebase.firestore.FieldValue.increment(-1)
                  });
                } else {
                  db.collection('likes').doc(id_likes).set({ likes: 0 })
                }
              }).catch((error) => {
                console.log("Error getting document:", error);
              });
            })
            .catch((error) => {
              var errorCode = error.code;
              var errorMessage = error.message;
              console.error(errorCode, errorMessage)
            });
        }

        function process_article() {
          var oid_likes = "likes_{{ .File.Path }}"
          var id_likes = oid_likes ? oid_likes.replaceAll("/", "-") : oid_likes
          if (!liked_article) {
            like_article(id_likes)
          } else {
            remove_like_article(id_likes)
          }
        }

      </script>
  </section>
  <footer class="pt-8 max-w-prose print:hidden">

    {{ partial "author.html" . }}

    {{ $authorsData := .Site.Data.authors }}
    {{ $taxonomies := .Site.Taxonomies.authors }}
    {{ $baseURL := .Site.BaseURL }}

    {{ if not (strings.HasSuffix $baseURL "/") }}
      {{ $baseURL = delimit (slice $baseURL "/") "" }}
    {{ end }}

    {{ range $author := .Page.Params.authors }}
    {{ $authorData := index $authorsData $author }}
    {{- if $authorData -}}

    {{ $taxonomyLink := 0 }}
    {{ range $taxonomyname, $taxonomy := $taxonomies }}
    {{ if (eq $taxonomyname $author) }}
    {{ $taxonomyLink = delimit (slice $baseURL "/authors/" $author) "" }}
    {{ end }}
    {{ end }}

    {{ partial "author-extra.html" (dict "context" . "data" $authorData "link" $taxonomyLink) }}
    {{- end -}}
    {{ end }}

    {{ partial "sharing-links.html" . }}
    {{ partial "article-pagination.html" . }}
    {{ if .Params.showComments | default (.Site.Params.article.showComments | default false) }}
    {{ if templates.Exists "partials/comments.html" }}
    <div class="pt-3">
      <hr class="border-dotted border-neutral-300 dark:border-neutral-600" />
      <div class="pt-3">
        {{ partial "comments.html" . }}
      </div>
    </div>
    {{ else }}
    {{ warnf "[BLOWFISH] Comments are enabled for %s but no comments partial exists." .File.Path }}
    {{ end }}
    {{ end }}
  </footer>
</article>
{{ end }}
