import { Component, inject, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import {
  IconButtonComponent,
  NavComponent,
  NostrHelper,
  StorageService,
  ToastComponent,
} from '@common';
import { FormsModule } from '@angular/forms';

interface CustomIdentity {
  id: string;
  nick: string;
  privkeyNsec: string;
  privkeyHex: string;
  pubkeyNpub: string;
  pubkeyHex: string;
}

@Component({
  selector: 'app-keys',
  imports: [IconButtonComponent, FormsModule, ToastComponent],
  templateUrl: './keys.component.html',
  styleUrl: './keys.component.scss',
})
export class KeysComponent extends NavComponent implements OnInit {
  identity?: CustomIdentity;

  readonly #activatedRoute = inject(ActivatedRoute);
  readonly #storage = inject(StorageService);
  readonly #router = inject(Router);

  ngOnInit(): void {
    const identityId = this.#activatedRoute.parent?.snapshot.params['id'];
    if (!identityId) {
      return;
    }

    this.#initialize(identityId);
  }

  copyToClipboard(text: string) {
    navigator.clipboard.writeText(text);
  }

  toggleType(element: HTMLInputElement) {
    if (element.type === 'password') {
      element.type = 'text';
    } else {
      element.type = 'password';
    }
  }

  navigateToNcryptsec() {
    if (!this.identity) return;
    this.#router.navigateByUrl(`/edit-identity/${this.identity.id}/ncryptsec`);
  }

  async #initialize(identityId: string) {
    const identity = this.#storage
      .getBrowserSessionHandler()
      .browserSessionData?.identities.find((x) => x.id === identityId);

    if (!identity) {
      return;
    }

    const pubkey = NostrHelper.pubkeyFromPrivkey(identity.privkey);

    this.identity = {
      id: identity.id,
      nick: identity.nick,
      privkeyHex: identity.privkey,
      privkeyNsec: NostrHelper.privkey2nsec(identity.privkey),
      pubkeyHex: pubkey,
      pubkeyNpub: NostrHelper.pubkey2npub(pubkey),
    };
  }
}
