#!/usr/bin/env node

require('../lib/environment-helper')

const settingsLoader = require('../lib/new-settings-loader')
const configManager = require('../lib/new-config-manager')
const wallet = require('../lib/wallet')
const { utils: coinUtils } = require('@lamassu/coins')
const BN = require('../lib/bn')
const inquirer = require('inquirer')
const ticker = require('../lib/ticker')

const [toAddress, cryptoValue, cryptoCode] = process.argv.slice(2)

function computeCrypto (cryptoCode, value) {
  try {
    const cryptoRec = coinUtils.getCryptoCurrency(cryptoCode)
    const unitScale = cryptoRec.unitScale

    return new BN(value).shiftedBy(unitScale)
  } catch (err) {
    return null
  }
}

if (!toAddress || !cryptoValue || !cryptoCode) {
  console.log('Usage: lamassu-send-coins <address> <amount> <coin>')
  console.log('Example: lamassu-send-coins 3FUv7vKaP11idnsUKyQ2pxdWxCDMyr5HKJ 0.009 BTC')
  process.exit(1)
}

const cryptoAtoms = computeCrypto(cryptoCode, cryptoValue)

if (!cryptoAtoms) {
  console.log(`Unsupported coin: ${cryptoCode}.\n`)
  console.log('Usage: lamassu-send-coins <address> <amount> <coin>')
  console.log('Example: lamassu-send-coins 3FUv7vKaP11idnsUKyQ2pxdWxCDMyr5HKJ 0.009 BTC')
  process.exit(2)
}

console.log('Loading ticker...')

settingsLoader.loadLatest()
  .then(settings => {
    const fiatCode = configManager.getGlobalLocale(settings.config).fiatCurrency

    return wallet.isStrictAddress(settings, cryptoCode, toAddress)
      .then(isValid => {
        if (!isValid) {
          console.log(`Invalid ${cryptoCode} address: ${toAddress}.`)
          console.log('Please check your command.\n')
          console.log('Usage: lamassu-send-coins <address> <amount> <coin>')
          console.log('Example: lamassu-send-coins 3FUv7vKaP11idnsUKyQ2pxdWxCDMyr5HKJ 0.009 BTC')
          process.exit(3)
        }
      })
      .then(() => ticker.getRates(settings, fiatCode, cryptoCode))
      .then(rates => {
        const fiatAmount = rates.rates.ask.times(cryptoValue).toFixed(2)

        const questions = [
          {
            type: 'confirm',
            name: 'confirm',
            message: `Are you sure you want to send ${cryptoValue} ${cryptoCode} (${fiatAmount} ${fiatCode}) to the address ${toAddress}?`,
            default: false
          }
        ]

        console.log('\nPlease look over this transaction carefully!')

        return inquirer.prompt(questions)
          .then(answers => {
            if (!answers.confirm) {
              console.log('Transaction cancelled.')
              process.exit(0)
            }

            console.log('Sending...')
            return wallet.sendCoins(settings, { toAddress, cryptoAtoms, cryptoCode })
              .then(() => {
                console.log('Success.')
                process.exit(0)
              })
              .catch(e => console.log(e.message))
          })
          .catch(e => console.log(e.message))
      })
      .catch(e => console.log(e.message))
  })
