<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()"
        >
          <fa icon="chevron-left" class="fa-fw"></fa>
        </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"
          >
            <fa 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"
          >
            <fa 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"
            @click="onClickNewPasskey()"
            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"
          >
            Generate one with a passkey
          </a>
          <a
            @click="onClickNewSeed()"
            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"
          >
            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
            @click="onClickNo()"
            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"
          >
            You have a seed
          </a>
          <a
            v-if="numAccounts > 0"
            @click="onClickDerive()"
            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"
          >
            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 } from "@/constants/app";
import { accountsDB, db } from "@/db/index";
import { MASTER_SETTINGS_KEY, Settings } from "@/db/tables/settings";
import { registerSaveAndActivatePasskey } from "@/libs/util";

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

  givenName = "";
  numAccounts = 0;

  async mounted() {
    await db.open();
    const settings = (await db.settings.get(MASTER_SETTINGS_KEY)) as Settings;
    this.givenName = settings?.firstName || "";

    await accountsDB.open();
    this.numAccounts = await accountsDB.accounts.count();
  }

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

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

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

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