<script>
  import { getLocaleFromNavigator, _ } from "svelte-i18n";
  import {
    ScanService,
  } from "@odit/lfk-client-js";
  import store from "../../store";
  import Toastify from "toastify-js";
  import ScansEmptyState from "./ScansEmptyState.svelte";
  $: searchvalue = "";
  $: active_deletes = [];
  export let current_scans = [];
  const scans_promise = ScanService.scanControllerGetAll().then((val) => {
    current_scans = val;
  });
  function should_display_based_on_id(id) {
    if (searchvalue.toString().slice(-1) === "*") {
      return id.toString().startsWith(searchvalue.replace("*", ""));
    }
    return id.toString() === searchvalue;
  }
  function format_laptime(laptime){
    if(laptime == 0 || laptime == null){return $_('first-scan-of-the-day')}
    if(laptime < 60){return `${laptime}s`}
    if(laptime < 3600){return `${Math.floor(laptime / 60)}min ${laptime - (Math.floor(laptime / 60)*60)}s`}
    return `${Math.floor(laptime / 3600)}h ${laptime - (Math.floor(laptime / 3600)*3600)}min ${laptime - (Math.floor(laptime / 3600)*3600) - (Math.floor(laptime / 60)*60)}`
  }
</script>

{#if store.state.jwtinfo.userdetails.permissions.includes('SCAN:GET')}
  {#await scans_promise}
    <div
      class="bg-teal-lightest border-t-4 border-teal rounded-b text-teal-darkest px-4 py-3 shadow-md my-2"
      role="alert">
      <p class="font-bold">{$_('scans-are-being-loaded')}</p>
      <p class="text-sm">{$_('this-might-take-a-moment')}</p>
    </div>
  {:then}
    {#if current_scans.length === 0}
      <ScansEmptyState />
    {:else}
      <input
        type="search"
        bind:value={searchvalue}
        placeholder={$_('datatable.search')}
        aria-label={$_('datatable.search')}
        class="gridjs-input gridjs-search-input mb-4" />
      <div
        class="shadow border-b border-gray-200 sm:rounded-lg overflow-x-scroll">
        <table class="divide-y divide-gray-200 w-full">
          <thead class="bg-gray-50">
            <tr>
              <th
                scope="col"
                class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                {$_('runner')}
              </th>
              <th
                scope="col"
                class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                {$_('distance-track')}
              </th>
              <th
                scope="col"
                class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                {$_('laptime')}
              </th>
              <th
                scope="col"
                class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                {$_('status')}
              </th>
              <th scope="col" class="relative px-6 py-3">
                <span class="sr-only">{$_('action')}</span>
              </th>
            </tr>
          </thead>
          <tbody class="divide-y divide-gray-200">
            {#each current_scans as scan}
              {#if scan.track?.name
                .toLowerCase()
                .includes(
                  searchvalue.toLowerCase()
                ) || scan.runner?.firstname
                  .toLowerCase()
                  .includes(
                    searchvalue.toLowerCase()
                  ) || scan.runner?.lastname
                  .toLowerCase()
                  .includes(
                    searchvalue.toLowerCase()
                  ) || should_display_based_on_id(scan.id)}
                <tr data-rowid="scan_{scan.id}">
                  <td class="px-6 py-4 whitespace-nowrap">
                    <div class="flex items-center">
                      <a
                        href="../runners/{scan.runner.id}"
                        class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-gray-100 text-gray-800">{scan.runner.firstname}
                        {scan.runner.middlename || ''}
                        {scan.runner.lastname}</a>
                    </div>
                  </td>
                  <td class="px-6 py-4 whitespace-nowrap">
                    <div class="text-sm font-medium text-gray-900">
                      {#if scan.distance < 1000}
                        {scan.distance}m
                      {:else}{scan.distance / 1000}km{/if}
                      {#if scan.track}
                        <a
                          href="../tracks"
                          class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-gray-100 text-gray-800">{scan.track.name}
                        </a>
                      {/if}
                    </div>
                  </td>
                  <td class="px-6 py-4 whitespace-nowrap">
                    {#if scan.responseType === "TRACKSCAN"}
                      <div class="text-sm font-medium text-gray-900">
                        {format_laptime(scan.lapTime)}
                      </div>
                    {:else}
                      <div class="text-sm font-medium text-gray-900">
                        {$_('scan-with-fixed-distance')}
                      </div>
                    {/if}
                  </td>
                  <td class="px-6 py-4 whitespace-nowrap">
                    <div class="flex items-center">
                      {#if scan.valid}
                        <span
                          class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-green-100 text-green-800">{$_('valid')}</span>
                      {:else}
                        <span
                          class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-red-100 text-red-800">{$_('invalid')}</span>
                      {/if}
                    </div>
                  </td>

                  {#if active_deletes[scan.id] === true}
                    <td
                      class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                      <button
                        on:click={() => {
                          active_deletes[scan.id] = false;
                        }}
                        tabindex="0"
                        class="ml-4 text-indigo-600 hover:text-indigo-900 cursor-pointer">{$_('cancel-delete')}</button>
                      <button
                        on:click={() => {
                          ScanService.scanControllerRemove(scan.id, false).then(
                            (resp) => {
                              current_scans = current_scans.filter(
                                (obj) => obj.id !== scan.id
                              );
                              Toastify({
                                text: 'Scan deleted',
                                duration: 500,
                                backgroundColor:
                                  'linear-gradient(to right, #00b09b, #96c93d)',
                              }).showToast();
                            }
                          );
                        }}
                        tabindex="0"
                        class="ml-4 text-red-600 hover:text-red-900 cursor-pointer">{$_('confirm-delete')}</button>
                    </td>
                  {:else}
                    <td
                      class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                      <a
                        href="./{scan.id}"
                        class="text-indigo-600 hover:text-indigo-900">{$_('details')}</a>
                      {#if store.state.jwtinfo.userdetails.permissions.includes('SCAN:DELETE')}
                        <button
                          on:click={() => {
                            active_deletes[scan.id] = true;
                          }}
                          tabindex="0"
                          class="ml-4 text-red-600 hover:text-red-900 cursor-pointer">{$_('delete')}</button>
                      {/if}
                    </td>
                  {/if}
                </tr>
              {/if}
            {/each}
          </tbody>
        </table>
      </div>
    {/if}
  {: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}
{/if}
