<template>
  <section
    id="Content"
    class="p-6 pb-24 min-h-screen flex flex-col justify-center"
  >
    <!-- Breadcrumb -->
    <div>
      <!-- Back -->
      <div class="text-lg text-center font-light relative px-7">
        <h1
          class="text-lg text-center px-2 py-1 absolute -left-2 -top-1"
          @click="$router.back()"
        >
          <font-awesome icon="chevron-left" class="fa-fw"></font-awesome>
        </h1>
      </div>

      <!-- Heading -->
      <h1 id="ViewHeading" class="text-4xl text-center font-light pt-4 mb-8">
        Generate an Identity
      </h1>
    </div>

    <!-- id used by puppeteer test script -->
    <div id="start-question" class="mt-8">
      <div class="max-w-3xl mx-auto">
        <p class="text-center text-xl font-light">
          How do you want to create this identifier?
        </p>
        <p v-if="PASSKEYS_ENABLED" class="text-center font-light mt-6">
          A <strong>passkey</strong> is easy to manage, though it is less
          interoperable with other systems for advanced uses.
          <a
            href="https://www.perplexity.ai/search/what-are-passkeys-v2SHV3yLQlyA2CYH6.Nvhg"
            target="_blank"
          >
            <font-awesome icon="info-circle" class="fa-fw text-blue-500" />
          </a>
        </p>
        <p class="text-center font-light mt-4">
          A <strong>new seed</strong> allows you full control over the keys,
          though you are responsible for backups.
          <a
            href="https://www.perplexity.ai/search/what-is-a-seed-phrase-OqiP9foVRXidr_2le5OFKA"
            target="_blank"
          >
            <font-awesome icon="info-circle" class="fa-fw text-blue-500" />
          </a>
        </p>
        <div class="grid grid-cols-1 sm:grid-cols-2 gap-2 mt-4">
          <a
            v-if="PASSKEYS_ENABLED"
            class="block w-full text-center text-lg uppercase bg-gradient-to-b from-blue-400 to-blue-700 shadow-[inset_0_-1px_0_0_rgba(0,0,0,0.5)] text-white px-2 py-3 rounded-md mb-2 cursor-pointer"
            @click="onClickNewPasskey()"
          >
            Generate one with a passkey
          </a>
          <a
            class="block w-full text-center text-lg uppercase bg-gradient-to-b from-blue-400 to-blue-700 shadow-[inset_0_-1px_0_0_rgba(0,0,0,0.5)] text-white px-2 py-3 rounded-md mb-2 cursor-pointer"
            data-testId="newSeed"
            @click="onClickNewSeed()"
          >
            Generate one with a new seed
          </a>
        </div>
        <p class="text-center font-light mt-4">
          You can also import an existing seed or derive a new address from an
          existing seed.
        </p>
        <div class="grid grid-cols-1 sm:grid-cols-2 gap-2 mt-2">
          <a
            class="block w-full text-center text-md uppercase bg-gradient-to-b from-blue-400 to-blue-700 shadow-[inset_0_-1px_0_0_rgba(0,0,0,0.5)] text-white px-1.5 py-2 rounded-md cursor-pointer"
            @click="onClickNo()"
          >
            You have a seed
          </a>
          <a
            v-if="numAccounts > 0"
            class="block w-full text-center text-md uppercase bg-gradient-to-b from-blue-400 to-blue-700 shadow-[inset_0_-1px_0_0_rgba(0,0,0,0.5)] text-white px-1.5 py-2 rounded-md cursor-pointer"
            @click="onClickDerive()"
          >
            Derive new address from existing seed
          </a>
        </div>
      </div>
    </div>
  </section>
</template>

<script lang="ts">
import { Component, Vue } from "vue-facing-decorator";
import { Router } from "vue-router";

import { AppString, PASSKEYS_ENABLED, USE_DEXIE_DB } from "../constants/app";
import * as databaseUtil from "../db/databaseUtil";
import { retrieveSettingsForActiveAccount } from "../db/index";
import {
  registerSaveAndActivatePasskey,
  retrieveAccountCount,
} from "../libs/util";

@Component({
  components: {},
})
export default class StartView extends Vue {
  $router!: Router;
  PASSKEYS_ENABLED = PASSKEYS_ENABLED;

  givenName = "";
  numAccounts = 0;

  async mounted() {
    let settings = await databaseUtil.retrieveSettingsForActiveAccount();
    if (USE_DEXIE_DB) {
      settings = await retrieveSettingsForActiveAccount();
    }
    this.givenName = settings.firstName || "";

    this.numAccounts = await retrieveAccountCount();
  }

  public onClickNewSeed() {
    this.$router.push({ name: "new-identifier" });
  }

  public async onClickNewPasskey() {
    const keyName =
      AppString.APP_NAME + (this.givenName ? " - " + this.givenName : "");
    await registerSaveAndActivatePasskey(keyName);
    this.$router.push({ name: "account" });
  }

  public onClickNo() {
    this.$router.push({ name: "import-account" });
  }

  public onClickDerive() {
    this.$router.push({ name: "import-derive" });
  }
}
</script>
