var express = require('express');
var router = express.Router();

var fs    = require('fs');
var nconf = require('nconf');

var request = require('request');

/* GET home page. */
router.get('/getServers', function(req, res, next) {

  nconf.file({ file:'config.json' });
  res.send({ servers: nconf.get('configuration').servers });
});

router.get('/list-zones/:index', function(req, res, next) {
  nconf.file({ file:'config.json' });
  ip = nconf.get('configuration').servers[req.params.index].ip;
  port = nconf.get('configuration').servers[req.params.index].port;
  //console.log('IP = '+ip);

  request('http://'+ip+':' + port + '/api/list-zones', function(err, response, body) {
    res.send(body);
  });
});

router.get('/list-zfs/:index', function(req, res, next) {
  nconf.file({ file:'config.json' });
  ip = nconf.get('configuration').servers[req.params.index].ip;
  port = nconf.get('configuration').servers[req.params.index].port;
  //console.log('IP = '+ip);

  request('http://'+ip+':' + port + '/api/list-zfs', function(err, response, body) {
    res.send(body);
  });
});

router.get('/show-phys/:index', function(req, res, next) {
  nconf.file({ file:'config.json' });
  ip = nconf.get('configuration').servers[req.params.index].ip;
  port = nconf.get('configuration').servers[req.params.index].port;
  console.log('IP = '+ip);

  request('http://'+ip+':' + port + '/api/show-phys', function(err, response, body) {
    res.send(body);
  });
});

router.get('/show-vnic/:index', function(req, res, next) {
  nconf.file({ file:'config.json' });
  ip = nconf.get('configuration').servers[req.params.index].ip;
  port = nconf.get('configuration').servers[req.params.index].port;
  //console.log('IP = '+ip);

  request('http://'+ip+':' + port + '/api/show-vnic', function(err, response, body) {
    res.send(body);
  });
});

router.get('/prstat/:index', function(req, res, next) {
  nconf.file({ file:'config.json' });
  ip = nconf.get('configuration').servers[req.params.index].ip;
  port = nconf.get('configuration').servers[req.params.index].port;
  //console.log('IP = '+ip);

  request('http://'+ip+':' + port + '/prstat', function(err, response, body) {
    res.send(body);
  });
});

router.get('/statsCPU/:index', function(req, res, next) {
  nconf.file({ file:'config.json' });
  ip = nconf.get('configuration').servers[req.params.index].ip;
  port = nconf.get('configuration').servers[req.params.index].port;
  //console.log('IP = '+ip);

  request('http://'+ip+':' + port + '/api/statsCPU', function(err, response, body) {
    res.send(body);
  });
});

router.get('/statsMEM/:index', function(req, res, next) {
  nconf.file({ file:'config.json' });
  ip = nconf.get('configuration').servers[req.params.index].ip;
  port = nconf.get('configuration').servers[req.params.index].port;
  //console.log('IP = '+ip);

  request('http://'+ip+':' + port + '/api/statsMEM', function(err, response, body) {
    res.send(body);
  });
});

router.get('/cpu-load/:index', function(req, res, next) {
  nconf.file({ file:'config.json' });
  ip = nconf.get('configuration').servers[req.params.index].ip;
  port = nconf.get('configuration').servers[req.params.index].port;
  //console.log('IP = '+ip);

  request('http://'+ip+':' + port + '/api/cpu-load', function(err, response, body) {
    res.send(body);
  });
});

router.get('/poweroff/:index/:zoneName', function(req, res, next) {
  nconf.file({ file:'config.json' });
  ip = nconf.get('configuration').servers[req.params.index].ip;
  port = nconf.get('configuration').servers[req.params.index].port;
  //console.log('IP = '+ip);
  console.log('Shutdown initiated for ' + req.params.index);

  request('http://'+ip+':' + port + '/api/poweroff/' + req.params.zoneName, function(err, response, body) {
    res.send('body');
  });
});

router.get('/boot/:index/:zoneName', function(req, res, next) {
  nconf.file({ file:'config.json' });
  ip = nconf.get('configuration').servers[req.params.index].ip;
  port = nconf.get('configuration').servers[req.params.index].port;
  //console.log('IP = '+ip);
  console.log('Boot initiated for ' + req.params.index);

  request('http://'+ip+':' + port + '/api/boot/' + req.params.zoneName, function(err, response, body) {
    res.send('body');
  });
});

router.get('/stats/:index', function(req, res, next) {
  nconf.file({ file:'config.json' });
  ip = nconf.get('configuration').servers[req.params.index].ip;
  port = nconf.get('configuration').servers[req.params.index].port;
  //console.log('IP = '+ip);

  request('http://'+ip+':'+ port + '/api/stats', function(err, response, body) {
    res.send(body);
  });
});

router.get('/createvnic/:index/:vnicName/:overName', function(req, res, next) {
  nconf.file({ file:'config.json' });
  ip = nconf.get('configuration').servers[req.params.index].ip;
  port = nconf.get('configuration').servers[req.params.index].port;
  //console.log('IP = '+ip);

  request('http://'+ip+':'+ port + '/api/createvnic/' + req.params.vnicName + '/' + req.params.overName, function(err, response, body) {
    res.send(body);
  });
});

module.exports = router;
