'use strict';

/**
 * Module dependencies.
 */
var path = require('path'),
  mongoose = require('mongoose'),
  HostName = mongoose.model('HostName'),
  errorHandler = require(path.resolve('./modules/core/server/controllers/errors.server.controller'));

/**
 * Create a hostName item
 */
exports.create = function (req, res) {
  console.log('exports.create');
  var hostName = new HostName(req.body);
  console.log('hostName.name: '+ hostName.name+ ' req.body: %j', req.body);
  hostName.user = req.user;
  var now = new Date().toString();
  hostName.hostName_date = now;
  
  hostName.save(function (err) {
    if (err) {
      return res.status(400).send({
        message: errorHandler.getErrorMessage(err)
      });
    } else {
      res.json(hostName);
    }
  });
};

/**
 * Show the current hostName
 */
exports.read = function (req, res) {
  res.json(req.hostName);
};

/**
 * Update a hostName
 */
exports.update = function (req, res) {  
  var hostName = req.hostName;

  hostName.semaphore = req.body.semaphore;
  hostName.name = req.body.name;
  hostName.hostType = req.body.hostType;
  hostName.envType = req.body.envType;
  hostName.originatingSource = req.body.originatingSource;
  hostName.originatingID = req.body.originatingID;
  hostName.note = req.body.note;
  hostName.PBB = req.body.PBB;
  hostName.address = req.body.address;
  hostName.relatedApplications = req.body.relatedApplications;
   
  hostName.save(function (err) {
    if (err) {
      return res.status(400).send({
        message: errorHandler.getErrorMessage(err)
      });
    } else {
      res.json(hostName);
    }
  });
};

/**
 * Delete a hostName
 */
exports.delete = function (req, res) {
  var hostName = req.hostName;

  hostName.remove(function (err) {
    if (err) {
      return res.status(400).send({
        message: errorHandler.getErrorMessage(err)
      });
    } else {
      res.json(hostName);
    }
  });
};

/**
 * Find host names
 */
exports.list = function (req, res) {
  if ((typeof req.query.hostType !== 'undefined') && (req.query.hostType !== null)) {
    console.log('Look up host by HEC');
    /**
    * List of HostNames  with specified hostType, envType and commonName
    */
    HostName.find({ 'hostType': req.query.hostType, 'envType': req.query.envType, 'address.commonName': req.query.commonName }).exec(function (err, hostNames) {
      if (err) {
        return res.status(400).send({
          message: errorHandler.getErrorMessage(err)
        });
      } else {
        res.json(hostNames);
      }
    }); 
  }
  else {
    console.log('Read all records');
    /**
    * List of All HostNames
    */
    HostName.find().sort('-created').populate('user', 'displayName').exec(function (err, hostNames) {
      if (err) {
        return res.status(400).send({
          message: errorHandler.getErrorMessage(err)
        });
      } else {
        res.json(hostNames);
      }
    });
  }
};

/**
 * HostName middleware
 */
exports.hostNameByID = function (req, res, next, id) {
  if (!mongoose.Types.ObjectId.isValid(id)) {
    return res.status(400).send({
      message: 'HostName is invalid'
    });
  }

  HostName.findById(id).populate('user', 'displayName').exec(function (err, hostName) {
    if (err) {
      return next(err);
    } else if (!hostName) {
      return res.status(404).send({
        message: 'No hostName with that identifier has been found'
      });
    }
    req.hostName = hostName;
    next();
  });
};