<script>
  import { AuthService } from "@odit/lfk-client-js";
  import { _ } from "svelte-i18n";
  import toast from "svelte-french-toast";
  import PasswordStrength, {
    password_strong_enough,
  } from "../auth/PasswordStrength.svelte";
  let state = "reset_in_progress";
  let password = "";
  export let params;
  function set_new_password() {
    if (password.trim() !== "") {
      toast.loading($_("password-reset-in-progress"));
      AuthService.authControllerResetPassword(atob(params.resetkey), {
        password,
      })
        .then((resp) => {
          toast.dismiss();
          toast($_("password-reset-successful"));
          state = "reset_success";
        })
        .catch((err) => {
          state = "reset_error";
        });
    } else {
      toast.dismiss();
      toast.error($_("please-provide-a-password"));
    }
  }
</script>

{#if state === "reset_success"}
  <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 style="height:10rem;" class="mx-auto" src="/lfk-logo.png" alt="" />
      <p class="mt-6 text-lg text-center font-bold text-gray-900">
        {$_("application_name")}
      </p>
      <p class="mt-2 mb-2 text-sm text-center text-gray-900 font-bold">
        {$_("successful-password-reset")}
      </p>
      <p class="mt-2 mb-2 text-sm text-center text-gray-900">
        {$_("you-can-now-use-your-new-password-to-log-in-to-your-account")}
      </p>
      <div class="mt-6">
        <div class="mt-6">
          <a
            href="/login/"
            class="text-center 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"
          >
            {$_("go-to-login")}
          </a>
        </div>
      </div>
    </div>
  </div>
{:else if state === "reset_error"}
  <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 style="height:10rem;" class="mx-auto" src="/lfk-logo.png" alt="" />
      <p class="mt-6 text-lg text-center font-bold text-gray-900">
        {$_("application_name")}
      </p>
      <p class="mt-2 mb-2 text-sm text-center text-gray-900 font-bold">
        {$_("password-reset-failed")}
      </p>
      <p class="mt-2 mb-2 text-sm text-center text-gray-900">
        {$_("please-request-a-new-reset-mail")}
      </p>
      <div class="mt-6">
        <div class="mt-6">
          <a
            href="/forgot_password/"
            class="text-center 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"
          >
            {$_("request-a-new-reset-mail")}
          </a>
        </div>
      </div>
    </div>
  </div>
{:else if state === "reset_in_progress"}
  <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 style="height:10rem;" class="mx-auto" src="/lfk-logo.png" alt="" />
      <p class="mt-6 text-lg text-center font-bold text-gray-900">
        {$_("application_name")}
      </p>
      <p class="mt-2 mb-4 text-md text-center text-gray-900">
        {$_("reset-password")}
      </p>
      <div>
        <div class="rounded-md shadow-sm">
          <div>
            <input
              aria-label={$_("new-password")}
              name="password"
              type="password"
              required=""
              class="border-gray-300 placeholder-gray-500 appearance-none rounded-md relative block w-full px-3 py-2 border text-gray-900 focus:outline-none focus:shadow-outline-blue focus:border-blue-300 focus:z-10 sm:text-sm"
              placeholder={$_("new-password")}
              bind:value={password}
            />
          </div>
          <PasswordStrength bind:password_change={password} />
        </div>

        <div class="mt-5">
          <button
            on:click={set_new_password}
            disabled={!password_strong_enough(password)}
            class:opacity-50={!password_strong_enough(password)}
            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>
            {$_("reset-my-password")}
          </button>
        </div>
      </div>
    </div>
  </div>
{/if}
