// local
var Copper = require('../copper')
var copper = new Copper

module.exports = function (config, wallet, keys, args) {
  if (args.in) {
    var walletKey = copper.keyFind(wallet.keys, args.in)
    if (walletKey) {
      var inKey = copper.fromWIF(walletKey.priv)
      copper.unspents(walletKey)
        .then(function (unspents) {
          var fee = args.fee || 0.0001
          var satoshi_fee = fee * 100000000
          var satoshi_unspent_total = unspents.reduce(function (memo, u) { return memo += u.value }, 0)

          if (args.out) {
            var satoshi_send
            if (args.amount) {
              satoshi_send = args.amount * 100000000
            }
            if (args.sweep) {
              satoshi_send = satoshi_unspent_total - satoshi_fee
            }

            if (satoshi_send) {
              var satoshi_total = satoshi_send + satoshi_fee

              tx = copper.transaction()
              var available = 0
              unspents.map(function (u) {
                if (available < satoshi_total) {
                  tx.addInput(u.tx_hash_big_endian, u.tx_output_n)
                  available = available + u.value
                  console.log(' input:', walletKey.pub, u.value / 100000000 + 'btc')
                }
              })

              if (available >= satoshi_total) {
                tx.addOutput(args.out, satoshi_send)
                console.log('output:', args.out, (satoshi_send / 100000000) + 'btc')
                var change = available - satoshi_send - satoshi_fee
                if (change > 0) {
                  tx.addOutput(args.in, change)
                  console.log('       ', args.in, change / 100000000 + 'btc', '(change)')
                }
                tx.inputs.forEach(function (input, idx) {
                  tx.sign(idx, inKey)
                })
                var tx_hex = tx.build().toHex()
                var tx_len = new Buffer(tx_hex, 'hex').length
                console.log('   fee:                                   ', fee + 'btc')
                console.log('  rate:                                  ', (satoshi_fee / tx_len).toFixed(0), 'satoshis/byte')
                console.log('TX (' + tx_len + ' bytes before encoding):')
                console.log(tx_hex)
              } else {
                console.log('Existing balance of', available / 100000000 + 'btc',
                  'is insufficient to send', (satoshi_send / 100000000) + 'btc',
                  'with fee', (satoshi_fee / 100000000) + 'btc')
                console.log('hint: try --amount', (available - satoshi_fee)/100000000, 'or use --sweep')
              }
            } else {
              console.log('missing: amount to send. use --amount <amount of btc> or --sweep')
            }
          } else {
            console.log('missing: where to send with --out <address>')
          }
        })
    } else {
      console.log('no private key found for', args.in)
      if (args.wallet) {
        console.log('using wallet', args.wallet)
      }
    }
  } else {
    console.log('missing: send from --in <address>')
  }
}

@@ -59,16 +59,45 @@
 w Copper
+%0Avar chalk = require('chalk')
 %0A%0Amodule
@@ -1241,16 +1241,28 @@
 ey.pub, 
+chalk.green(
 u.value 
@@ -1264,32 +1264,33 @@
 alue / 100000000
+)
  + 'btc')%0A      
@@ -1469,16 +1469,27 @@
 gs.out, 
+chalk.green
 (satoshi
@@ -1697,23 +1697,23 @@
 le.log('
-       
+change:
 ', args.
@@ -1712,24 +1712,37 @@
 ', args.in, 
+chalk.yellow(
 change / 100
@@ -1747,24 +1747,25 @@
 00000000
+)
  + 'btc'
 , '(chan
@@ -1760,20 +1760,8 @@
 btc'
-, '(change)'
 )%0A  
@@ -2075,19 +2075,32 @@
      ', 
+chalk.green(
 fee
+)
  + 'btc'
@@ -2093,32 +2093,123 @@
 n(fee) + 'btc')%0A
+                console.log('  size:                                   ', tx_len, 'bytes')%0A
                 
@@ -2228,16 +2228,17 @@
 '  rate:
+ 
         
@@ -2351,49 +2351,19 @@
 og('
-TX (' + tx_len + ' bytes before
+Hex
  encod
-ing)
+ed
 :')%0A

use color, and rate in tx output