{{/* Hugo Blox: Languages */}}
{{/* Documentation: https://hugoblox.com/blocks/ */}}
{{/* License: https://github.com/HugoBlox/kit/blob/main/LICENSE.md */}}

{{/* Initialise */}}
{{ $page := .wcPage }}
{{ $block := .wcBlock }}
{{ $content := $block.content }}
{{ if not (reflect.IsMap $content) }}{{ $content = dict }}{{ end }}
{{ $design := $block.design }}
{{ if not (reflect.IsMap $design) }}{{ $design = dict }}{{ end }}

{{ $username := "me" }}
{{ $username_raw := index $content "username" }}
{{ if eq (printf "%T" $username_raw) "string" }}
  {{ $username = strings.TrimSpace $username_raw }}
{{ end }}
{{ $profile := partial "functions/get_author_profile" $username }}

{{ $langs_raw := $profile.languages }}
{{ $langs_iter := slice }}
{{ if reflect.IsSlice $langs_raw }}
  {{ $langs_iter = $langs_raw }}
{{ else if and $langs_raw (reflect.IsMap $langs_raw) }}
  {{ $langs_iter = slice $langs_raw }}
{{ end }}

{{ $langs := slice }}
{{ if gt (len $langs_iter) 0 }}
  {{ range $langs_iter }}
    {{ if reflect.IsMap . }}
      {{ $name := "" }}
      {{ $level_text := "" }}
      {{ $label := "" }}
      {{ $percent_raw := "" }}

      {{ with index . "name" }}{{ $name = strings.TrimSpace (printf "%v" .) }}{{ end }}
      {{ with index . "level" }}{{ $level_text = strings.TrimSpace (printf "%v" .) }}{{ end }}
      {{ with index . "label" }}{{ $label = strings.TrimSpace (printf "%v" .) }}{{ end }}
      {{ with index . "percent" }}{{ $percent_raw = strings.TrimSpace (printf "%v" .) }}{{ end }}

      {{ $percent := 100.0 }}
      {{ $numeric_level := gt (len (findRE "^[0-9]+(?:\\.[0-9]+)?$" $level_text)) 0 }}
      {{ if $numeric_level }}
        {{ $percent = mul (float $level_text) 20 }}
      {{ else if $percent_raw }}
        {{ if gt (len (findRE "^[0-9]+(?:\\.[0-9]+)?$" $percent_raw)) 0 }}
          {{ $percent = float $percent_raw }}
        {{ end }}
      {{ end }}

      {{ if gt $percent 100 }}{{ $percent = 100 }}{{ end }}
      {{ if lt $percent 0 }}{{ $percent = 0 }}{{ end }}

      {{ if $name }}
        {{ $langs = $langs | append (dict "name" $name "level" $level_text "label" $label "percent" $percent "numeric_level" $numeric_level) }}
      {{ end }}
    {{ end }}
  {{ end }}
{{ end }}

{{ $title := "" }}
{{ $title_raw := index $content "title" }}
{{ if $title_raw }}{{ $title = strings.TrimSpace (printf "%v" $title_raw) }}{{ end }}

{{ $text := "" }}
{{ $text_raw := index $content "text" }}
{{ if $text_raw }}{{ $text = strings.TrimSpace (printf "%v" $text_raw) }}{{ end }}

<div class="flex flex-col items-center max-w-prose mx-auto gap-3 justify-center">

  <div class="mb-6 text-3xl font-bold text-gray-900 dark:text-white">
    {{ $title | markdownify | emojify }}
  </div>

  {{ with $text }}<p>{{ . | markdownify | emojify }}</p>{{ end }}

  <div class="flex items-center flex-col lg:flex-row gap-y-10 lg:gap-y-0 lg:gap-x-8">
    {{ range $langs }}
    {{ $percent := index . "percent" }}
    {{ $level_text := index . "level" }}
    {{ $label := index . "label" }}
    {{ $numeric_level := index . "numeric_level" }}

    <div class="flex flex-col items-center">
      <div class="flex items-center justify-center w-28 h-28">
        <svg class="transform -rotate-90" viewBox="0 0 288 288">
          <circle cx="145" cy="145" r="120" stroke="currentColor" stroke-width="30" fill="transparent"
                  class="text-gray-200 dark:text-gray-700" />
          <circle cx="145" cy="145" r="120" stroke="currentColor" stroke-width="30" fill="transparent"
                  class="text-primary-600 dark:text-primary-300"
                  style="stroke-dasharray: calc(2 * 22 / 7 * 120); stroke-dashoffset: calc((2 * 22 / 7 * 120) - ((2 * 22 / 7 * 120) * {{$percent}}/100))" />
        </svg>
        <span class="absolute text-3xl">{{ $percent }}%</span>
      </div>
      <span class="text-1xl">{{ index . "name" }}</span>
      {{ if $label }}
        <span class="text-sm text-gray-500 dark:text-gray-400 mt-1">{{ $label }}</span>
      {{ else if and $level_text (not $numeric_level) }}
        <span class="text-sm text-gray-500 dark:text-gray-400 mt-1">{{ $level_text }}</span>
      {{ end }}
    </div>
    {{ end }}
  </div>

</div>
