var api = require('digio-api')
, print = require('../print')
, tools = require('../tools')

var droplets = function (cmd, _config) {
  api = new api(tools.get_active_key(_config))

  cmd.register(
    'droplets',
    'list',
    [],
    'List all droplets',
    function () {
      api.droplets.list(function (err, data) {
        if (err) return print.error(err.message)
        console.log('\nTotal droplets: ' + data.meta.total + '\n')
        data.droplets.forEach(function (droplet) {
          console.log('- Name:        ' + droplet.name)
          console.log('    ID:        ' + droplet.id)
          console.log('    Size:      ' + droplet.size.slug)
          console.log('    Image:     ' + droplet.image.name)
          console.log('')
        })
      })
    }
  )

  cmd.register(
    'droplets',
    'get',
    ['<id>'],
    'Get details of a specific droplet',
    function (id) {
      api.droplets.get(id, function (err, data) {
        if (err) return print.error(err.message)
        console.log('')
        console.log('- Name:        ' + data.droplet.name)
        console.log('    ID:        ' + data.droplet.id)
        console.log('    Memory:    ' + data.droplet.memory)
        console.log('    VCPUs:     ' + data.droplet.vcpus)
        console.log('    Disk:      ' + data.droplet.disk)
        console.log('    Region:    ' + data.droplet.region.name)
        console.log('    Image:     ' + data.droplet.image.name)
        console.log('    Kernel:    ')
        console.log('    - Name:    ' + data.droplet.kernel.name)
        console.log('    - Version: ' + data.droplet.kernel.version)
        console.log('    Size:      ' + data.droplet.size.slug)
        console.log('    Locked:    ' + data.droplet.locked)
        console.log('    Status:    ' + data.droplet.status)
        console.log('    Networks:  ')
        console.log('      V4:      ')
        data.droplet.networks.v4.forEach(function (v4) {
          console.log('      - ' + v4.ip_address)
        })
        console.log('      V6:      ')
        data.droplet.networks.v6.forEach(function (v6) {
          console.log('      - ' + v6.ip_address)
        })
        console.log('    Created:   ' + data.droplet.created_at)
        console.log('    Features:  ' + data.droplet.features.join())
        console.log('    Backups:   ' + data.droplet.backup_ids.join())
        console.log('    Snapshots: ' + data.droplet.snapshot_ids.join())
        console.log('    Actions:   ' + data.droplet.action_ids.join())
        console.log('')
      })
    }
  )

  cmd.register(
    'droplets',
    'delete',
    ['<id>'],
    'Delete a droplet',
    function (id) {
      api.droplets.delete(id, function (err, data) {
        if (err) return print.error(err.message)
        print.success('Deleted droplet with ID: ' + id)
      })
    }
  )

  cmd.register(
    'droplets',
    'create',
    ['<name>','<region>','<size>','<image_id>','<ssh_keys>','<backups>','<ipv6>','<priv_net>'],
    'Create a new droplet',
    function (name, region, size, image_id, ssh_keys, backups, ipv6, priv_net) {
      if (!isNaN(image_id)) image_id = Number(image_id)
      if (ssh_keys === 'null') ssh_keys = null
      if (backups === 'null') backups = null
      if (ipv6 === 'null') ipv6 = null
      if (priv_net === 'null') priv_net = null

      if (ssh_keys) ssh_keys = ssh_keys.split(',')
      var ssh_keys_clean = ssh_keys.map(function (element) {
        if (!isNaN(element)) return Number(element)
        return element
      })

      api.droplets.create(name, region, size, image_id, ssh_keys_clean, backups, ipv6, priv_net, function (err, data) {
        if (err) print.error(err.message)
        print.success('Droplet ' + name + ' created!')
      })
    }
  )
}

module.exports = droplets

@@ -2920,19 +2920,16 @@
 ,'%3Cimage
-_id
 %3E','%3Cssh
@@ -3335,16 +3335,26 @@
 sh_keys)
+ %7B%0A       
  ssh_key
@@ -3383,19 +3383,17 @@
 )%0A      
-var
+ 
  ssh_key
@@ -3393,22 +3393,16 @@
 ssh_keys
-_clean
  = ssh_k
@@ -3434,24 +3434,26 @@
 ) %7B%0A        
+  
 if (!isNaN(e
@@ -3492,16 +3492,18 @@
         
+  
 return e
@@ -3515,18 +3515,28 @@
 t%0A      
+  
 %7D)
+%0A      %7D
 %0A%0A      
@@ -3597,14 +3597,8 @@
 keys
-_clean
 , ba

Fix an error in create droplet command