#!/usr/bin/env node
'use strict'

var argv = require('minimist')(process.argv.slice(2))
var keypress = require('keypress')
var MonsterDrift = require('./')
var pkg = require('./package')

var drive, timer

if (argv.h || argv.help) help()
else if (argv.v || argv.version) version()
else run()

function run () {
  console.log('Connecting to radio...')

  drive = new MonsterDrift({swaplr: argv.swaplr})

  console.log('Found radio!')
  console.log('Use keys to drive - Run with --help for details')

  keypress(process.stdin)
  process.stdin.on('keypress', function (ch, key) {
    if (!key) return
    if (key && key.ctrl && key.name === 'c') return exit()

    clearTimeout(timer)

    switch (key.name) {
      case 'w': drive.forward(); break
      case 'q': drive.forwardRight(); break
      case 'e': drive.forwardLeft(); break
      case 's': drive.reverse(); break
      case 'a': drive.reverseRight(); break
      case 'd': drive.reverseLeft(); break
      case 'z': drive.left(); break
      case 'c': drive.right(); break
      case 'u': return drive.turn180(stop) // 180 stops automatically
    }

    timer = setTimeout(stop, 150)

    function stop () {
      drive.stop()
    }
  })
  process.stdin.setRawMode(true)
  process.stdin.resume()
}

function help () {
  console.log(`
Usage:
  monster-drift [options]

Options:
  -h, --help     Show this message
  -v, --version  Show version
  --swaplr       Invert left/right stearing

Use the keyboard to drive.

Basic driving:
  w: Forward
  q: Forward/Right
  e: Forward/Left
  s: Reverse
  a: Reverse/Right
  d: Reverse/Left
  z: Wheels Left
  c: Wheels Right

Tricks:
  u: Turn 180

Exit by pressing ctrl+c

P.S. A high key-repeat will improve the experience :)
  `)
}

function version () {
  console.log(pkg.version)
}

function exit () {
  drive.close(function () {
    process.exit()
  })
}

@@ -377,27 +377,211 @@
 ft(%7B
-swaplr: argv.swaplr
+%0A    channel: argv.c %7C%7C argv.channel,%0A    freq: argv.f %7C%7C argv.freq,%0A    speed: argv.speed,%0A    swaplr: argv.swaplr,%0A    id: argv.d %7C%7C argv.id,%0A    gain: argv.gain,%0A    sampleRate: argv%5B'sample-rate'%5D%0A  
 %7D)%0A%0A
@@ -1530,16 +1530,18 @@
 elp     
+  
 Show thi
@@ -1567,16 +1567,18 @@
 ersion  
+  
 Show ver
@@ -1589,48 +1589,399 @@
 %0A  -
--swaplr       Invert left/right stearing
+c, --channel=N  Set transmit channel (default: 19)%0A  --freq=Hz        Set transmit frequency (overwrites --channel)%0A  --speed=N        Set default forward speed (1-3, default: 1)%0A  --swaplr         Invert left/right stearing%0A  -d, --id         HackRF device id (default: 0)%0A  --gain           HackRF TX gain (default: 30)%0A  --sample-rate    Sample rate used when transmitting (default: 10e6)
 %0A%0AUs

Allow to overwrite options when using CLI