'use strict'

const Promise = require('bluebird')
const { Order } = require('bfx-api-node-models')
const runExample = require('../util/run_example')

const UPDATE_DELAY_MS = 5 * 1000
const CANCEL_DELAY_MS = 10 * 1000

module.exports = runExample({
  name: 'rest-submit-order',
  rest: { env: true },
  readline: true,
  params: {
    // needed in order to pipe data to the process, until we can figure out a
    // workaround
    skipConfirm: false,
    onlySubmitOrder: false, // allows this script to be used only for submits
    symbol: 'tLEOUSD',
    price: 2,
    amount: -6,
    type: Order.type.LIMIT,
    affiliateCode: 'xZvWHMNR'
  }
}, async ({ rest, debug, params, readline }) => {
  const {
    symbol, price, amount, type, affiliateCode, onlySubmitOrder, skipConfirm,
    priceStop, distance
  } = params

  const o = new Order({
    cid: Date.now(),
    symbol,
    price,
    priceAuxLimit: priceStop,
    priceTrailing: distance,
    amount,
    type,
    affiliateCode
  }, rest)

  if (!skipConfirm) {
    const confirm = await readline.questionAsync([
      '>  Are you sure you want to submit this order?',
      `>  ${o.toString()}`,
      '>  '
    ].join('\n'))

    if (confirm.toLowerCase()[0] !== 'y') {
      return
    }
  }

  debug('submitting order: %s', o.toString())

  await o.submit()

  debug('order successfully submitted! (id %j, cid %j, gid %j)', o.id, o.cid, o.gid)

  if (onlySubmitOrder) {
    return // for bfx-cli
  }

  debug('')
  debug('will update price to $3.00 in %fs...', UPDATE_DELAY_MS / 1000)

  await Promise.delay(UPDATE_DELAY_MS)

  debug('')
  debug('updating order price...')

  const updateNotification = await o.update({ price: 3 })
  debug('successfully updated! (%s: %s)', updateNotification.status, updateNotification.text)
  debug('')
  debug('will cancel the order in %fs', CANCEL_DELAY_MS / 1000)

  await Promise.delay(CANCEL_DELAY_MS)

  debug('')
  debug('cancelling order...')

  const cancelNotification = await o.cancel()
  debug('successfully canceled! (%s: %s)', cancelNotification.status, cancelNotification.text)
})

@@ -98,26 +98,112 @@
 )%0Aconst 
-runExample
+%7B RESTv2 %7D = require('bfx-api-node-rest')%0Aconst %7B args: %7B apiKey, apiSecret %7D, debug, readline %7D
  = requi
@@ -218,19 +218,13 @@
 til/
-run_example
+setup
 ')%0A%0A
@@ -295,117 +295,212 @@
 00%0A%0A
-module.exports = runExample(%7B%0A  name: 'rest-submit-order',%0A  re
+async function execute () %7B%0A  const rest = new RESTv2(%7B%0A    apiKey,%0A    apiSecret%0A  %7D)%0A  con
 st
-:
  %7B
- env: true %7D,%0A  readline: true,%0A  params:
+%0A    symbol, price, amount, type, affiliateCode, onlySubmitOrder, skipConfirm,%0A    priceStop, distance%0A  %7D =
  %7B%0A 
@@ -814,183 +814,8 @@
 %0A  %7D
-%0A%7D, async (%7B rest, debug, params, readline %7D) =%3E %7B%0A  const %7B%0A    symbol, price, amount, type, affiliateCode, onlySubmitOrder, skipConfirm,%0A    priceStop, distance%0A  %7D = params
 %0A%0A  
@@ -1219,24 +1219,47 @@
  !== 'y') %7B%0A
+      readline.close()%0A
       return
@@ -1444,24 +1444,45 @@
 mitOrder) %7B%0A
+    readline.close()%0A
     return /
@@ -2127,11 +2127,40 @@
 n.text)%0A
-%7D
+  readline.close()%0A%7D%0A%0Aexecute(
 )%0A

update 'submit_order' example