<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">
        Start Here
      </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">
          Do you want a new identifier of your own?
        </p>
        <p class="text-center font-light">
          If you haven't used this before, click "Yes" to generate a new
          identifier.
        </p>
        <p class="text-center mb-4 font-light">
          Only click "No" if you have a seed of 12 or 24 words generated
          elsewhere.
        </p>
        <input type="text" v-model="savedCredentialId" class="border" />
        <a
          @click="onClickYes()"
          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"
        >
          Yes, generate one
        </a>
        <div class="grid grid-cols-1 sm:grid-cols-2 gap-2">
          <a
            @click="onClickNo()"
            class="block w-full text-center text-md uppercase bg-gradient-to-b from-slate-400 to-slate-700 shadow-[inset_0_-1px_0_0_rgba(0,0,0,0.5)] text-white px-1.5 py-2 rounded-md"
          >
            No, I 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-slate-400 to-slate-700 shadow-[inset_0_-1px_0_0_rgba(0,0,0,0.5)] text-white px-1.5 py-2 rounded-md"
          >
            Derive new address from existing seed
          </a>
        </div>
      </div>
    </div>
  </section>
</template>

<script lang="ts">
import { Base64URLString } from "@simplewebauthn/types";
import { Component, Vue } from "vue-facing-decorator";

import { accountsDB } from "@/db/index";
import { generateRandomBytes } from "@/libs/crypto";
import {
  createPeerDid,
  JWK,
  PeerSetup,
  registerCredential,
  verifyJwt,
} from "@/libs/didPeer";

@Component({
  components: {},
})
export default class StartView extends Vue {
  authenticatorData?: ArrayBuffer;
  credId?: Base64URLString;
  numAccounts = 0;
  publicKeyJwk?: JWK;
  publicKeyBytes?: Uint8Array;
  rawId?: Uint8Array;
  savedCredentialId = "";
  userId?: ArrayBuffer;

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

  public async onClickYes() {
    this.userId = generateRandomBytes(16).buffer;
    const encodedUserId = new TextDecoder("utf-8").decode(this.userId);
    console.log("encodedUserId", encodedUserId);

    const challenge = generateRandomBytes(32);
    const cred = await registerCredential(
      this.userId as Uint8Array,
      challenge as Uint8Array,
    );
    console.log("public key", cred);
    this.publicKeyJwk = cred.publicKeyJwk;
    this.publicKeyBytes = cred.publicKeyBytes;
    this.credId = cred.credId as string;
    this.rawId = cred.rawId as Uint8Array;
    this.savedCredentialId = this.credId;
    //this.$router.push({ name: "new-identifier" });
  }

  public async onClickNo() {
    const did = createPeerDid(this.publicKeyBytes as Uint8Array);
    console.log("did", did);
    const payload = { a: 1 };
    const peerSetup = new PeerSetup();
    const jwt = await peerSetup.createJwt(payload, did, this.credId as string);
    console.log("jwt", jwt);
    const jwt4url = jwt
      .replace(/\+/g, "-")
      .replace(/\//g, "_")
      .replace(/=+$/, "");
    console.log("jwt4url", jwt4url);

    const signature = jwt4url.split(".")[2];
    const decoded = await verifyJwt(
      jwt4url,
      this.credId as Base64URLString,
      this.rawId as Uint8Array,
      peerSetup.authenticatorData as ArrayBuffer,
      peerSetup.clientDataJsonDecoded,
      this.publicKeyBytes as Uint8Array,
      signature,
    );
    console.log("decoded", decoded);
    //this.$router.push({ name: "import-account" });
  }

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