const {Client: AMQPClient, Policy} = require('amqp10')
const urlencode = require('urlencode')
const inquirer = require('inquirer')

async function connectAndHandle () {
  try {
    const {action} = await inquirer.prompt(
      {
        message: 'Vad vill du göra?',
        name: 'action',
        choices: [
          {name: 'Bara läsa meddelanden', value: 'read'},
          {name: 'Läsa meddelanden och rensa kön', value: 'delete'}
        ],
        type: 'list'
      })

    const {queue} = await inquirer.prompt(
      {
        message: 'Vilken miljö?',
        name: 'queue',
        choices: [
            {name: 'produktion', value: {name: `ug-infoclass-2/Subscriptions/canvas-prod/$DeadLetterQueue`, url: 'amqps://canvas-prod'}},
            {name: 'referens', value: {name: `ug-infoclass-2/Subscriptions/canvas-ref/$DeadLetterQueue`, url: 'amqps://canvas-ref'}}
        ],
        type: 'list'
      })

    const {sharedAccessKey} = await inquirer.prompt({
      message: 'Klistra in en access key till valda kön i Azure. Den finns här: https://tinyurl.com/ydfquezj',
      name: 'sharedAccessKey'
    })

    const client = await new AMQPClient(Policy.Utils.RenewOnSettle(1, 1, Policy.ServiceBusQueue))
    await client.connect(`${queue.url}:${urlencode(sharedAccessKey)}@kth-integral.servicebus.windows.net`)
    const receiver = await client.createReceiver(queue.name)
    console.log('receiver created:', receiver.id)

    receiver.on('message', message => {
      console.log('new message', JSON.stringify(message, null, 4))
      if (action === 'delete') {
        receiver.accept(message)
      } else {
        receiver.release(message)
      }
    })
  } catch (e) {
    console.error('error:', e)
  }
}

connectAndHandle()

@@ -622,18 +622,19 @@
 e: '
-produktion
+canvas-prod
 ', v
@@ -703,38 +703,36 @@
 tterQueue%60, 
-url: 'amqps://
+shortName: '
 canvas-prod'
@@ -759,16 +759,18 @@
 e: '
-referens
+canvas-ref
 ', v
@@ -846,22 +846,20 @@
 e%60, 
-url: 'amqps://
+shortName: '
 canv
@@ -979,17 +979,17 @@
 essage: 
-'
+%60
 Klistra 
@@ -1014,17 +1014,26 @@
 ill 
-valda k%C3%B6n
+$%7Bqueue.shortName%7D
  i A
@@ -1081,17 +1081,17 @@
 ydfquezj
-'
+%60
 ,%0A      
@@ -1250,19 +1250,33 @@
 ct(%60
-$%7Bqueue.url
+amqps://$%7Bqueue.shortName
 %7D:$%7B

change display text a little