<script>
  import { _ } from "svelte-i18n";
  import store from "../store.js";
  //
  import { OpenAPI, AuthService, TrackService } from "@odit/lfk-client-js";
  OpenAPI.BASE = "http://localhost:4010";
  //
  store.init();
  //
  import Toastify from "toastify-js";
  import "toastify-js/src/toastify.css";
  let usersUsername = "";
  let usersPassword = "";
  let last_loginclick_processed = true;

  const login = async () => {
    // prevent login button spamming
    if (last_loginclick_processed) {
      last_loginclick_processed = false;
      Toastify({
        text: $_("login_is_checked"),
        duration: 1500,
      }).showToast();
      console.log(usersUsername);
      console.log(usersPassword);
      AuthService.authControllerLogin({
        username: "demo",
        password: "demo",
        // username: usersUsername,
        // password: usersPassword,
      })
        .then((result) => {
          OpenAPI.TOKEN = result.access_token;
          store.login(result.access_token);
          Toastify({
            text: $_("welcome_wavinghand"),
            duration: 1500,
            backgroundColor: "linear-gradient(to right, #00b09b, #96c93d)",
          }).showToast();
        })
        .catch((err) => {
          Toastify({
            text: $_("error_on_login"),
            duration: 1500,
            backgroundColor:
              "linear-gradient(90deg, hsla(281, 37%, 45%, 1) 0%, hsla(1, 62%, 48%, 1) 100%)",
          }).showToast();
        })
        .finally(() => {
          last_loginclick_processed = true;
        });
      // last login was not processed yet
    } else {
      Toastify({
        text: "chill...",
        duration: 1500,
        backgroundColor:
          "linear-gradient(90deg, hsla(281, 37%, 45%, 1) 0%, hsla(1, 62%, 48%, 1) 100%)",
      }).showToast();
    }
  };
</script>

<div class="min-h-screen flex items-center justify-center bg-gray-100">
  <div class="max-w-md w-full py-12 px-6">
    <img
      class="mx-auto h-20 w-auto"
      src="https://lauf-fuer-kaya.de/Bilder/kaya-logo-quadrat.png"
      alt="" />
    <p class="mt-6 text-lg text-center font-bold text-gray-900">
      {$_('log_in_to_your_account')}
    </p>
    <p class="mt-6 text-sm text-center text-gray-900">
      {$_('log_in_to_your_account')}
    </p>
    <div>
      <div class="rounded-md shadow-sm">
        <div>
          <input
            aria-label={$_('email_address_or_username')}
            name="email"
            type="email"
            required=""
            class="border-gray-300 placeholder-gray-500 appearance-none rounded-none relative block w-full px-3 py-2 border text-gray-900 rounded-t-md focus:outline-none focus:shadow-outline-blue focus:border-blue-300 focus:z-10 sm:text-sm"
            placeholder={$_('email_address_or_username')}
            bind:value={usersUsername} />
        </div>
        <div class="-mt-px relative">
          <input
            aria-label={$_('password')}
            name="password"
            type="password"
            required=""
            bind:value={usersPassword}
            class="border-gray-300 placeholder-gray-500 appearance-none rounded-none relative block w-full px-3 py-2 border text-gray-900 rounded-b-md focus:outline-none focus:shadow-outline-blue focus:border-blue-300 focus:z-10 sm:text-sm"
            placeholder={$_('password')} />
        </div>
      </div>

      <div class="mt-5">
        <button
          on:click={login}
          type="submit"
          class="relative block w-full py-2 px-3 border border-transparent rounded-md text-white font-semibold bg-gray-800 hover:bg-gray-700 focus:bg-gray-900 focus:outline-none focus:shadow-outline sm:text-sm">
          <span class="absolute left-0 inset-y pl-3">
            <svg
              class="h-5 w-5 text-gray-500"
              fill="currentColor"
              viewBox="0 0 20 20">
              <path
                fill-rule="evenodd"
                d="M5 9V7a5 5 0 0110 0v2a2 2 0 012 2v5a2 2 0 01-2 2H5a2 2 0 01-2-2v-5a2 2 0 012-2zm8-2v2H7V7a3 3 0 016 0z"
                clip-rule="evenodd" />
            </svg>
          </span>
          {$_('log_in')}
        </button>
      </div>
    </div>
    <div class="mt-2">
      <a
        href="/forgot_password"
        class="block w-full text-center py-2 px-3 border border-gray-300 rounded-md text-gray-900 font-medium hover:border-gray-400 focus:outline-none focus:border-gray-400 sm:text-sm">
        {$_('forgot_password?')}
      </a>
    </div>
  </div>
</div>
