#! /usr/bin/env node

const program = require('commander');

const Hs100Api = require('.');
const client = new Hs100Api.Client();

let debug = false;

let search = function (sysInfo, timeout) {
  console.log('Searching...');
  client.debug = debug;
  client.startDiscovery({discoveryInterval: 2500, discoveryTimeout: timeout})
    .on('device-new', (device) => {
      console.log(`${device.model} ${device.type} ${device.host} ${device.deviceId}`);
      if (sysInfo) {
        console.dir(device.sysInfo, {colors: program.color === 'on', depth: 10});
      }
    });
};

let send = function (host, port, payload, timeout) {
  console.log('Sending...');
  client.send({host, port, payload, timeout, debug}).then((data) => {
    console.log('response:');
    console.dir(data, {colors: program.color === 'on', depth: 10});
  }).catch((reason) => {
    console.log('no response: %s', reason);
  });
};

let details = function (host, port, timeout) {
  console.log('Getting details...');
  client.getDevice({host, port, debug}).then((device) => {
    device.getInfo().then((info) => {
      console.dir(info, {colors: program.color === 'on', depth: 10});
    });
  });
};

let blink = function (host, port, times, rate, timeout) {
  console.log('Sending blink commands...');
  client.getDevice({host, port, debug}).then((device) => {
    device.blink(times, rate).then(() => {
      console.log('Blinking complete');
    });
  });
};

program
  .option('-D, --debug', 'turn on debug level logging', () => { debug = true; })
  .option('-c, --color [on]', 'output will be styled with ANSI color codes', 'on');

program
  .command('search')
  .option('-s, --sysinfo', 'output sysInfo')
  .option('-t, --timeout [timeout]', 'timeout (ms)', 5000)
  .action(function (options) {
    search(options.sysinfo, options.timeout);
  });

program
  .command('send <host> <payload>')
  .option('-t, --timeout [timeout]', 'timeout (ms)', 5000)
  .action(function (host, payload, options) {
    let [hostOnly, port] = host.split(':');
    send(hostOnly, port, payload, options.timeout);
  });

program
  .command('details <host>')
  .option('-t, --timeout [timeout]', 'timeout (ms)', 5000)
  .action(function (host, options) {
    let [hostOnly, port] = host.split(':');
    details(hostOnly, port, options.timeout);
  });

program
  .command('blink <host> [times] [rate]')
  .option('-t, --timeout [timeout]', 'timeout (ms)', 5000)
  .action(function (host, times, rate, options) {
    let [hostOnly, port] = host.split(':');
    blink(hostOnly, port, times, rate, options.timeout);
  });

program.parse(process.argv);

@@ -132,21 +132,16 @@
 let 
-debug = false
+logLevel
 ;%0A%0Al
@@ -226,21 +226,30 @@
 ent.
-debug = debug
+log.setLevel(logLevel)
 ;%0A  
@@ -644,32 +644,65 @@
 ('Sending...');%0A
+  client.log.setLevel(logLevel);%0A
   client.send(%7Bh
@@ -728,23 +728,16 @@
  timeout
-, debug
 %7D).then(
@@ -1001,32 +1001,65 @@
 g details...');%0A
+  client.log.setLevel(logLevel);%0A
   client.getDevi
@@ -1064,39 +1064,32 @@
 vice(%7Bhost, port
-, debug
 %7D).then((device)
@@ -1094,24 +1094,31 @@
 e) =%3E %7B%0A    
+return 
 device.getIn
@@ -1209,32 +1209,84 @@
 : 10%7D);%0A    %7D);%0A
+  %7D).catch((reason) =%3E %7B%0A    console.error(reason);%0A
   %7D);%0A%7D;%0A%0Alet bl
@@ -1373,32 +1373,65 @@
  commands...');%0A
+  client.log.setLevel(logLevel);%0A
   client.getDevi
@@ -1448,15 +1448,8 @@
 port
-, debug
 %7D).t
@@ -1470,16 +1470,23 @@
 %3E %7B%0A    
+return 
 device.b
@@ -1560,24 +1560,76 @@
 ');%0A    %7D);%0A
+  %7D).catch((reason) =%3E %7B%0A    console.error(reason);%0A
   %7D);%0A%7D;%0A%0Apr
@@ -1702,20 +1702,26 @@
 %3E %7B 
-debug = true
+logLevel = 'debug'
 ; %7D)

use logLevel