import 'babel-core/polyfill';
import YamahaAPI from 'yamaha-nodejs';
import restify from 'restify';
import {Client} from 'node-ssdp';

var server = restify.createServer({
  name: 'yams',
  version: '0.1.0'
});

var receiver;
var PORT = process.env.PORT || 8081;
var err_msg = "I'm sorry, something went wrong";

function err(res, text) {
  return (err) => {
    if (err) {
      console.error(err.stack);
      res.send(400, text);
    }
  }
}

function success(res, text) {
  return () => {
    console.log(text);
    res.send(200, text);
  }
}

var err_msg = "I'm sorry, something went wrong";

function err(res, text) {
  return (err) => {
    if (err) {
      console.error(err.stack);
      res.send(400, text);
    }
  }
}

function success(res, text) {
  return () => {
    console.log(text);
    res.send(200, text);
  }
}

function setVolume(req, res, next) {
  let {body: {direction}} = req;

  if (direction && direction.length > 0) {
    switch (direction.toLowerCase()) {
      case "up": {
        console.log("going up");
        receiver.volumeUp(50)
          .then(success(res, "Going up"))
          .catch(err(res, err_msg))
          .done(next);
        break;
      }
      case "down": {
        console.log("going down");
        receiver.volumeDown(50)
          .then(success(res, "Going down"))
          .catch(err(res, err_msg))
          .done(next);
        break;
      }
      default: {
        err("Not sure about the direction of volume adjustment");
        return next();

      }
    }
  }
}

function sanitizeInput(input, number) {
  return `${input.replace(/\.|\s/g, '').toUpperCase()}${number}`
}

function setInput(req, res, next) {
  let {body: {input, number}} = req;
  let sanitized = sanitizeInput(input, number);
  let validInputs = receiver.getAvailableInputs();

  validInputs.done(inputs => {
    if (inputs.indexOf(sanitized) !== -1) {
      let output = `Switching input to ${input} ${number}`;
      console.log(output);
      receiver.setMainInputTo(sanitized)
        .then(success(res, output))
        .catch(err(res, err_msg))
        .done(next);
    } else {
      let output = `Input ${sanitized} not found`;
      err(output);
      return next();
    }
  });
}

function setState(req, res, next) {
  let {body: {state}} = req;

  console.log('Turning server ' + state);

  switch (state) {
    case 'on': {
      receiver.powerOn()
        .then(success(res, "Powering on"))
        .catch(err(res, err_msg))
        .done(next)
      break;
    }
    case 'off': {
      receiver.powerOff()
        .then(success(res, "Powering off"))
        .catch(err(res, err_msg))
        .done(next);
      break;
    }
    default: {
      err(res, err_msg);
      return next();
    }
  }
}

server.use(restify.queryParser());
server.use(restify.bodyParser());

server.post("/volume", setVolume);
server.post("/input", setInput);
server.post("/state", setState);

var listening = true;
var ssdpClient = new Client();
var search = 'urn:schemas-upnp-org:service:AVTransport:1';

ssdpClient.on('response', (headers, statusCode, rinfo) => {
  if (listening && headers.USN.endsWith(search)) {
    receiver = new YamahaAPI(rinfo.address);
    listening = false;
    server.listen(PORT, () => {
      console.log(`Listening on port ${PORT}, controlling ${rinfo.address}`)
    });
  }
});

ssdpClient.search(search);

@@ -885,25 +885,387 @@
 irection
-%7D%7D = req;
+, modifier%7D%7D = req;%0A  let upResp = %22Going up%22%0A  let downResp = %22Going down%22%0A%0A  if (modifier === %22a lot%22 %7C%7C modifier === %22a bunch%22) %7B%0A    var amount = 100;%0A    upResp += %60 $%7Bmodifier%7D%60;%0A    downResp += %60 $%7Bmodifier%7D%60;%0A  %7D else if (modifier === %22way%22) %7B%0A    var amount = 100;%0A    upResp = %22Going way up%22;%0A    downResp = %22Going way down%22;%0A  %7D else %7B%0A    var amount = 50;%0A  %7D
 %0A%0A  if (
@@ -1418,18 +1418,22 @@
 olumeUp(
-50
+amount
 )%0A      
@@ -1455,26 +1455,22 @@
 ss(res, 
-%22Going up%22
+upResp
 ))%0A     
@@ -1630,18 +1630,22 @@
 umeDown(
-50
+amount
 )%0A      
@@ -1667,28 +1667,24 @@
 ss(res, 
-%22Going down%22
+downResp
 ))%0A     
@@ -1839,24 +1839,24 @@
 justment%22);%0A
+
         retu
@@ -1866,17 +1866,16 @@
 next();%0A
-%0A
       %7D%0A

Support a lot/way/a bunch for volume modifiers