'use strict';

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

/**
 * Create a featureMapper
 */
exports.create = function (req, res) {
  var featureMapper = new FeatureMapper(req.body);
  featureMapper.user = req.user;
  var now = new Date().toString();
  featureMapper.creation_date = now;

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

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

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

  //console.log('req.featureMapper -- %j', req.featureMapper);

  featureMapper.address = req.body.address;
  featureMapper.aic_version = req.body.aic_version;
  featureMapper.clli = req.body.clli;
  featureMapper.cluster = req.body.cluster;
  featureMapper.contrail_version = req.body.contrail_version;
  featureMapper.date_start = req.body.date_start;
  featureMapper.date_end = req.body.date_end;
  featureMapper.myLogins_access = req.body.myLogins_access;
  featureMapper.placement_exception = req.body.placement_exception;
  featureMapper.placement_waiver = req.body.placement_waiver;
  featureMapper.site_description = req.body.site_description;
  featureMapper.site_type = req.body.site_type;
  featureMapper.ush_access = req.body.ush_access;
  featureMapper.zone = req.body.zone;

  featureMapper.compute_features=req.body.compute_features;
  featureMapper.networks=req.body.networks;
  featureMapper.storages=req.body.storages;
  featureMapper.support_models=req.body.support_models;
  featureMapper.tools=req.body.tools;
  featureMapper.workloads=req.body.workloads;

  //console.log('featureMapper -- %j', featureMapper);
  featureMapper.save(function (err) {
    if (err) {
      return res.status(400).send({
        message: errorHandler.getErrorMessage(err)
      });
    } else {
      res.json(featureMapper);
    }
  });
};

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

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

/**
 * List of FeatureMappers
 */
exports.list = function (req, res) {
  FeatureMapper.find().sort('-created').populate('user', 'displayName').exec(function (err, featureMappers) {
    if (err) {
      return res.status(400).send({
        message: errorHandler.getErrorMessage(err)
      });
    } else {
      res.json(featureMappers);
    }
  });
};

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

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