<script>
  import { _ } from "svelte-i18n";
  import { clickOutside } from "../base/outsideclick";
  import { focusTrap } from "svelte-focus-trap";
  export let modal_open;
  (function () {
    document.onkeydown = function (e) {
      e = e || window.event;
      if (e.key === "Escape") {
        modal_open = false;
      }
    };
  })();
  const license_promise = fetch("/licenses.json");
  let licenses = [];
  $: currentlicense = "";
  $: licensetext = "";
  license_promise
    .then((response) => response.json())
    .then((json) => {
      licenses = json;
    });
</script>

{#if modal_open}
  <div
    class="fixed z-10 inset-0 overflow-y-auto"
    use:focusTrap
    use:clickOutside
    on:click_outside={() => {
      modal_open = false;
    }}>
    <div
      class="flex items-end justify-center min-h-screen pt-4 px-4 pb-20 text-center sm:block sm:p-0">
      <div class="fixed inset-0 transition-opacity" aria-hidden="true">
        <div
          class="absolute inset-0 bg-gray-500 opacity-75"
          data-id="modal_backdrop" />
      </div>
      <span
        class="hidden sm:inline-block sm:align-middle sm:h-screen"
        aria-hidden="true">&#8203;</span>
      <div
        class="inline-block align-bottom bg-white rounded-lg text-left overflow-hidden shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-lg sm:w-full"
        role="dialog"
        aria-modal="true"
        aria-labelledby="modal-headline">
        <div class="bg-white px-4 pt-5 pb-4 sm:p-6 sm:pb-4">
          <div class="sm:flex sm:items-start">
            <div
              class="mx-auto flex-shrink-0 flex items-center justify-center h-12 w-12 rounded-full bg-blue-100 sm:mx-0 sm:h-10 sm:w-10">
              <svg
                fill="currentColor"
                class="h-6 w-6 text-blue-600"
                xmlns="http://www.w3.org/2000/svg"
                viewBox="0 0 24 24"
                width="24"
                height="24"><path fill="none" d="M0 0h24v24H0z" />
                <path
                  d="M14 20v2H2v-2h12zM14.586.686l7.778 7.778L20.95 9.88l-1.06-.354L17.413 12l5.657 5.657-1.414 1.414L16 13.414l-2.404 2.404.283 1.132-1.415 1.414-7.778-7.778 1.415-1.414 1.13.282 6.294-6.293-.353-1.06L14.586.686z" /></svg>
            </div>
            <div class="mt-3 text-center sm:mt-0 sm:ml-4 sm:text-left">
              <h3 class="text-lg leading-6 font-medium">
                {$_('read-license')}
              </h3>
              <div class="mt-2 mb-6">
                <p class="text-sm text-gray-500">{currentlicense}</p>
              </div>
              <div class="mt-2 mb-6">
                <p class="text-sm text-gray-500">{licensetext}</p>
              </div>
            </div>
          </div>
        </div>
        <div class="bg-gray-50 px-4 py-3 sm:px-6 sm:flex sm:flex-row-reverse">
          <button
            on:click={() => {
              modal_open = false;
            }}
            type="button"
            class="w-full inline-flex justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-blue-600 text-base font-medium text-white hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 sm:ml-3 sm:w-auto sm:text-sm">
            {$_('close')}
          </button>
        </div>
      </div>
    </div>
  </div>
{/if}
<!-- /// -->
<div class="pt-12 px-4 sm:px-6 lg:px-8 lg:pt-20 bg-gray-900 pb-12">
  <div class="text-center mb-8">
    <h1
      class="mt-9 font-display text-4xl leading-none font-semibold text-white sm:text-5xl lg:text-6xl">
      {$_('about')}
      🧾
    </h1>
    <p
      class="mt-2 max-w-xl mx-auto text-xl lg:max-w-3xl lg:text-2xl text-gray-300">
      Lauf für Kaya!
      <strong class="text-white font-medium">
        {$_('by')}
        <a href="https://odit.services" class="underline">ODIT.Services</a>
      </strong>
      <br />
      <span class="text-lg">{$_('lfk-is-os')}</span>
    </p>
  </div>
</div>

<div class="pt-0 pb-16 overflow-hidden lg:pt-12 lg:py-24">
  <div class="max-w-7xl mx-auto py-6 px-4 sm:px-6 lg:px-8">
    <h2 class="text-4xl font-display font-semibold md:text-5xl">
      {$_('credits')}
    </h2>
    <div class="max-w-3xl mx-auto text-xl leading-8 font-medium mt-8">
      <p class="text-center">{$_('oss_credit_description')}</p>
    </div>
    <div class="w-screen leading-8 pl-5 mt-5">
      {#await license_promise}
        <p class="text-center w-full">{$_('licenses-are-being-loaded')}</p>
      {:then}
        <table>
          <thead>
            <tr>
              <th>{$_('dependency_name')}</th>
              <th>{$_('license')}</th>
              <th>{$_('repo_link')}</th>
              <th>{$_('installed-version')}</th>
              <th>{$_('author')}</th>
            </tr>
          </thead>
          <tbody>
            {#each licenses as l}
              <tr>
                <td>{l.name}</td>
                <td>
                  {l.license || '?'}<br /><span
                    class="underline cursor-pointer"
                    on:click={() => {
                      modal_open = true;
                      currentlicense = l.name + '@' + l.version;
                      licensetext = l.licensetext || $_('no-license-text-could-be-found');
                    }}>{$_('read-license')}</span>
                </td>
                <td>
                  {(l.repo?.url || l.repo)
                    .replace('git+', '')
                    .replace('git://', '')}
                </td>
                <td>{l.version || '?'}</td>
                <td>{l.author?.name || l.author || '?'}</td>
              </tr>
            {/each}
          </tbody>
        </table>
      {:catch error}
        <div
          class="text-white px-6 py-4 border-0 rounded relative mb-4 bg-red-500">
          <span class="inline-block align-middle mr-8">
            <b class="capitalize">{$_('general_promise_error')}</b>
            {error}
          </span>
        </div>
      {/await}
    </div>
    <div class="w-full leading-8 mt-8">
      <p class="text-xl font-medium">{$_('icon-image-credits')}</p>
      <ul class="list-disc">
        <li>
          <a
            class="underline"
            target="_blank"
            rel="noopener noreferrer"
            href="https://storyset.com">https://storyset.com</a>
        </li>
        <li>
          <a
            class="underline"
            target="_blank"
            rel="noopener noreferrer"
            href="https://undraw.co">https://undraw.co</a>
        </li>
        <li>
          <a
            class="underline"
            target="_blank"
            rel="noopener noreferrer"
            href="https://remixicon.com">https://remixicon.com</a>
        </li>
      </ul>
    </div>
  </div>
</div>
