extends templates/base.pug

mixin generateUser(user, isPrimary)
  h2
    span.p-email #{user.userData.email}
    if isPrimary
      small.primary primary
  if user.userData.comment
    span.p-comment ⓘ #{user.userData.comment}
  each claim in user.claims
    if claim.matches.length > 0
      kx-claim.kx-item(data-claim=claim)
        details(aria-label="Claim")
          summary
            .info
              p.subtitle= claim.matches[0].serviceprovider.name
              p.title= claim.matches[0].profile.display
            .icons
              .verificationStatus(data-value='running')
                .inProgress
          .content
            .subsection
              img(src='/static/img/link.png')
              div
                if (claim.matches[0].profile.uri)
                  p Profile link: 
                    a(rel='me' href=claim.matches[0].profile.uri aria-label="Link to profile")= claim.matches[0].profile.uri
                else
                  p Profile link: not accessible from browser
                if (claim.matches[0].proof.uri)
                  p Proof link: 
                    a(href=claim.matches[0].proof.uri aria-label="Link to proof")= claim.matches[0].proof.uri
                else
                  p Proof link: not accessible from browser

block content
  script.
    kx = {
      key: {
        url: "!{data && data.key && data.key.fetchURL ? data.key.fetchURL : null}",
        object: null
      }
    }

  if (data && 'errors' in data && data.errors.length > 0)
    section
      h2 Something went wrong while viewing the profile
      .card 
        if data.errors.length === 1
          p The following error was reported:
        else
          p The following errors were reported:
        ul
        each error in data.errors
          li
            p= error
        p Please see the 
          a(href="https://docs.keyoxide.org/getting-started/something-went-wrong/") documentation
          |  for help.
  else
    section.profile.narrow.h-card
      noscript
        p Keyoxide requires JavaScript to function.

      if (enable_message_encryption)
        dialog#dialog--encryptMessage
          div
            form(method='post')
              label(for="encryptionInput") Message to encrypt 
              textarea#encryptionInput.input(name='message')
              input.no-margin(type='submit' name='submit' value='ENCRYPT MESSAGE')
              br
              br
              label(for="encryptionOutput") Encryption result 
              textarea#encryptionOutput.output(name='message' placeholder='Waiting for input' readonly)
            form(method="dialog")
              input(type="submit" value="Close")

      if (enable_signature_verification)
        dialog#dialog--verifySignature
          div
            form(method='post')
              label(for="sigVerInput") Signature 
              textarea#sigVerInput.input(name='signature')
              input.no-margin(type='submit' name='submit' value='VERIFY SIGNATURE')
              br
              br
              label(for="sigVerOutput") Verification result 
              textarea#sigVerOutput.output(name='message' placeholder='Waiting for input' readonly)
            form(method="dialog")
              input(type="submit" value="Close")

      dialog#dialog--qr
        div
          canvas#qr
          p
            a(href="" tabindex="0")#qr--altLink
          form(method="dialog")
            input(type="submit" value="Close")

      if (isSignature)
        #profileSigInput.form-wrapper.card
          h2 Signature profile
          form#formGenerateSignatureProfile(method='post')
            label(for="signature")  Please enter the raw profile signature below and press "Generate profile".
            textarea#signature(name='signature')= signature
            input(type='submit', name='submit', value='Generate profile')

      unless (isSignature && !signature)
        #profileHeader.card.card--transparent.card--profileHeader
          img#profileAvatar.u-logo(src=data.extra.avatarURL alt="avatar")

          p#profileName.p-name= data.keyData.users[data.keyData.primaryUserIndex].userData.name

          if (enable_message_encryption || enable_signature_verification)
            .button-wrapper
              if (enable_message_encryption)
                button(onClick="document.querySelector('#dialog--encryptMessage').showModal();") Encrypt message
              if (enable_signature_verification)
                button(onClick="document.querySelector('#dialog--verifySignature').showModal();") Verify signature

        +generateUser(data.keyData.users[data.keyData.primaryUserIndex], true)
        each user, index in data.keyData.users
          unless index == data.keyData.primaryUserIndex
            +generateUser(user, false)

        #profileProofs.card.card--transparent
          h2 Key
          kx-key.kx-item(data-keydata=data.keyData)
            details(aria-label="Key")
              summary
                .info
                  p.subtitle= data.keyData.key.fetchMethod
                  p.title= data.keyData.fingerprint
              .content
                .subsection
                  img(src='/static/img/link.png')
                  div
                    p Key link: 
                      a.u-key(href=data.keyData.key.uri rel="pgpkey" aria-label="Link to cryptographic key")= data.keyData.key.uri
                hr
                .subsection
                  img(src='/static/img/qrcode.png')
                  div
                    button(onClick=`showQR('${data.keyData.fingerprint}', 'fingerprint')` aria-label='Show QR code for cryptographic fingerprint') Show OpenPGP fingerprint QR