text: /* eslint no-console:0 */
  /* global QRLLIB, XMSS_OBJECT, LocalStore, QrlLedger, isElectrified, selectedNetwork,loadAddressTransactions, getTokenBalances, updateBalanceField, refreshTransferPage */
  /* global pkRawToB32Address, hexOrB32, rawToHexOrB32, anyAddressToRawAddress, stringToBytes, binaryToBytes, bytesToString, bytesToHex, hexToBytes, toBigendianUint64BytesUnsigned, numberToString, decimalToBinary */
  /* global getMnemonicOfFirstAddress, getXMSSDetails, isWalletFileDeprecated, waitForQRLLIB, addressForAPI, binaryToQrlAddress, toUint8Vector, concatenateTypedArrays, getQrlProtoShasum */
  /* global resetWalletStatus, passwordPolicyValid, countDecimals, supportedBrowser, wrapMeteorCall, getBalance, otsIndexUsed, ledgerHasNoTokenSupport, resetLocalStorageState, nodeReturnedValidResponse */
  /* global POLL_TXN_RATE, POLL_MAX_CHECKS, DEFAULT_NETWORKS, findNetworkData, SHOR_PER_QUANTA, WALLET_VERSION, QRLPROTO_SHA256,  */
  
  import { isElectrified, createTransport, ledgerReturnedError } from '../../../../startup/client/functions'
  import './githubConfirm.html'
  
  async function getLedgerCreateMessageTx(sourceAddr, fee, message, callback) {
    console.log('-- Getting QRL Ledger Nano App createMessageTx --')
    if (isElectrified()) {
      const retry = Meteor.setInterval(() => {
        Meteor.call(
          'ledgerCreateMessageTx',
          sourceAddr,
          fee,
          message,
          (err, data) => {
            if (data.error_message !== 'Timeout') {
              console.log('> Got Ledger Nano createMessageTx from USB')
              console.log(data)
              Meteor.clearInterval(retry)
              callback(null, data)
            }
          }
        )
      }, 2000)
    } else {
      const QrlLedger = await createTransport()
      QrlLedger.createMessageTx(sourceAddr, fee, message).then(data => {
        console.log('> Got Ledger Nano createMessageTx from WebUSB')
        console.log(data)
        callback(null, data)
      })
    }
  }
  async function getLedgerRetrieveSignature(request, callback) {
    console.log('-- Getting QRL Ledger Nano App Signature --')
    if (isElectrified()) {
      const retry = Meteor.setInterval(() => {
        Meteor.call('ledgerRetrieveSignature', request, (err, data) => {
          if (data.error_message !== 'Timeout') {
            console.log('> Got Ledger Nano retrieveSignature from USB')
            console.log(data)
            Meteor.clearInterval(retry)
            callback(null, data)
          }
        })
      }, 2000)
    } else {
      const QrlLedger = await createTransport()
      QrlLedger.retrieveSignature(request).then(data => {
        console.log('> Got Ledger Nano retrieveSignature from WebUSB')
        console.log(data)
        callback(null, data)
      })
    }
  }
  
  function confirmGithubCreation() {
    const tx = Session.get('messageCreationConfirmationResponse')
  
    if ((getXMSSDetails().walletType === 'seed') && (XMSS_OBJECT === null)) {
      // session ended before confirmation was completed: show as failure
      $('#messageCreationConfirmation').hide()
      $('#transactionFailed').show()
      Session.set('transactionFailed', 'Session ended before transaction was confirmed')
    }
  
    // Set OTS Key Index in XMSS object
    if (getXMSSDetails().walletType === 'seed') {
      XMSS_OBJECT.setIndex(parseInt(Session.get('messageCreationConfirmation').otsKey, 10))
    }
  
    // Concatenate Uint8Arrays
    const tmptxnhash = concatenateTypedArrays(
      Uint8Array,
      // tx.extended_transaction_unsigned.addr_from,
      toBigendianUint64BytesUnsigned(tx.extended_transaction_unsigned.tx.fee),
      tx.extended_transaction_unsigned.tx.message.message_hash // eslint-disable-line
    )
  
    // Convert Uint8Array to VectorUChar
    const hashableBytes = toUint8Vector(tmptxnhash)
  
    // Create sha256 sum of hashableBytes
    const shaSum = QRLLIB.sha2_256(hashableBytes)
  
    if (getXMSSDetails().walletType === 'seed') {
      // Show relaying message
      $('#relaying').show()
  
      // Sign the sha sum
      tx.extended_transaction_unsigned.tx.signature = binaryToBytes(XMSS_OBJECT.sign(shaSum))
  
      // Calculate transaction hash
      const txnHashConcat = concatenateTypedArrays(
        Uint8Array,
        binaryToBytes(shaSum),
        tx.extended_transaction_unsigned.tx.signature,
        hexToBytes(XMSS_OBJECT.getPK()) // eslint-disable-line
      )
  
      const txnHashableBytes = toUint8Vector(txnHashConcat)
  
      const txnHash = QRLLIB.bin2hstr(QRLLIB.sha2_256(txnHashableBytes))
  
      console.log('Txn Hash: ', txnHash)
  
      tx.network = selectedNetwork()
  
      wrapMeteorCall('confirmGithubCreation', tx, (err, res) => {
        if (res.error) {
          $('#messageCreationConfirmation').hide()
          $('#transactionFailed').show()
  
          Session.set('transactionFailed', res.error)
        } else {
          Session.set('transactionHash', txnHash)
          Session.set('transactionSignature', res.response.signature)
          Session.set('transactionRelayedThrough', res.relayed)
  
          // Send to result page.
          const params = { }
          const path = FlowRouter.path('/tools/github/result', params)
          FlowRouter.go(path)
        }
      })
    } else if (getXMSSDetails().walletType === 'ledger') {
      // Reset ledger sign modal view state
      $('#awaitingLedgerConfirmation').show()
      $('#signOnLedgerRejected').hide()
      $('#signOnLedgerTimeout').hide()
      $('#signOnLedgerError').hide()
      $('#ledgerHasConfirmed').hide()
      $('#relayLedgerTxnButton').hide()
      $('#noRemainingSignatures').hide()
  
      // Show ledger sign modal
      $('#ledgerConfirmationModal').modal({
        closable: false,
        onDeny: () => {
          // Clear session state for transaction
          Session.set('ledgerTransaction', '')
          Session.set('ledgerTransactionHash', '')
        },
        onApprove: () => {
          // Hide modal, and show relaying message
          $('#ledgerConfirmationModal').modal('hide')
          $('#relaying').show()
  
          // Relay the transaction
          wrapMeteorCall('confirmMessageCreation', Session.get('ledgerTransaction'), (err, res) => {
            if (res.error) {
              $('#messageCreationConfirmation').hide()
              $('#transactionFailed').show()
  
              Session.set('transactionFailed', res.error)
            } else {
              Session.set('transactionHash', Session.get('ledgerTransactionHash'))
              Session.set('transactionSignature', res.response.signature)
              Session.set('transactionRelayedThrough', res.relayed)
  
              // Send to result page.
              const params = { }
              const path = FlowRouter.path('/tools/github/result', params)
              FlowRouter.go(path)
            }
          })
        },
      }).modal('show')
  
      // Create a transaction
      const sourceAddr = hexToBytes(QRLLIB.getAddress(getXMSSDetails().pk))
      const fee = toBigendianUint64BytesUnsigned(tx.extended_transaction_unsigned.tx.fee, true)
  
      // eslint-disable-next-line max-len
      getLedgerCreateMessageTx(sourceAddr, fee, Buffer.from(tx.extended_transaction_unsigned.tx.message.message_hash), function (err, txn) {
        getLedgerRetrieveSignature(txn, function (err, sigResponse) {
          // Hide the awaiting ledger confirmation spinner
          $('#awaitingLedgerConfirmation').hide()
  
          // Check if ledger rejected transaction
          if (sigResponse.return_code === 27014) {
            $('#signOnLedgerRejected').show()
            // Show no signatures remaining message if there are none remaining.
            if (Session.get('transactionConfirmation').otsKey >= 256) {
              $('#noRemainingSignatures').show()
            }
          // Check if the the request timed out waiting for response on ledger
          } else if (sigResponse.return_code === 14) {
            $('#signOnLedgerTimeout').show()
          // Check for unknown errors
          } else if ((sigResponse.return_code === 1) && (sigResponse.error_message == 'Unknown error code')) {
            $('#signOnLedgerError').show()
          } else {
            // Show confirmation message
            $('#ledgerHasConfirmed').show()
  
            tx.extended_transaction_unsigned.tx.signature = sigResponse.signature
  
            // Calculate transaction hash
            const txnHashConcat = concatenateTypedArrays(
              Uint8Array,
              binaryToBytes(shaSum),
              tx.extended_transaction_unsigned.tx.signature,
              hexToBytes(getXMSSDetails().pk) // eslint-disable-line
            )
  
            const txnHashableBytes = toUint8Vector(txnHashConcat)
  
            const txnHash = QRLLIB.bin2hstr(QRLLIB.sha2_256(txnHashableBytes))
  
            console.log('Txn Hash: ', txnHash)
  
            // Prepare gRPC call
            tx.network = selectedNetwork()
  
            // Set session values for later relaying
            Session.set('ledgerTransaction', tx)
            Session.set('ledgerTransactionHash', txnHash)
  
            // Show relay button
            $('#relayLedgerTxnButton').show()
          }
        })
      })
    }
  }
  
  function cancelTransaction() {
    Session.set('messageCreationConfirmation', '')
    Session.set('messageCreationConfirmationResponse', '')
  
    Session.set('transactionFailed', 'User requested cancellation')
  
    $('#messageCreationConfirmation').hide()
    $('#transactionFailed').show()
  }
  
  Template.appGithubConfirm.onRendered(() => {
    $('.ui.dropdown').dropdown()
  })
  
  Template.appGithubConfirm.events({
    'click #confirmMessage': () => {
      $('#signOnLedgerRejected').hide()
      $('#signOnLedgerTimeout').hide()
      setTimeout(() => { confirmGithubCreation() }, 200)
    },
    'click #cancelMessage': () => {
      cancelTransaction()
    },
  })
  
  Template.appGithubConfirm.helpers({
    transferFrom() {
      const transferFrom = {}
      transferFrom.balance = Session.get('transferFromBalance')
      transferFrom.address = hexOrB32(Session.get('transferFromAddress'))
      return transferFrom
    },
    messageCreationConfirmation() {
      const confirmation = Session.get('messageCreationConfirmation')
      return confirmation
    },
    transactionFailed() {
      const failed = Session.get('transactionFailed')
      return failed
    },
    nodeExplorerUrl() {
      if ((Session.get('nodeExplorerUrl') === '') || (Session.get('nodeExplorerUrl') === null)) {
        return DEFAULT_NETWORKS[0].explorerUrl
      }
      return Session.get('nodeExplorerUrl')
    },
    githubOperation() {
      const githubOperation = Session.get('githubOperation')
      if (githubOperation.addorremove === 'AA') { githubOperation.addorremove = 'ADD' }
      if (githubOperation.addorremove === 'AF') { githubOperation.addorremove = 'REMOVE' }
      return githubOperation
    },
    isSeedWallet() {
      if (getXMSSDetails().walletType === 'seed') {
        return true
      }
      return false
    },
    isLedgerWallet() {
      if (getXMSSDetails().walletType === 'ledger') {
        return true
      }
      return false
    },
    ledgerVerificationMessage() {
      const message = Session.get('messageCreationConfirmation').message_hex
      return message
    },
  })
  