var ObjectID = require('mongodb').ObjectID;
var moment = require('moment');

var dateFormat = 'MM/DD/YYYY';

var startDateParam = 'startDate';
var endDateParam = 'endDate';
var projectIdParam = 'projectId';
var userIdParam = 'userId';
var companyIdParam ='companyId';
var timelogIdParam = 'timelogId';

exports.getStartDate = function(req, res) {
    return getDateParam(req, res, startDateParam);
};

exports.getEndDate = function(req, res) {
    return getDateParam(req, res, endDateParam);
}; 

exports.getProjectId = function(req, res) {
    return getObjectIdParam(req, res, projectIdParam)
};

exports.getUserId = function(req, res) {
    return getObjectIdParam(req, res, userIdParam);
};

exports.getCompanyId = function(req, res) {
    return getObjectIdParam(req, res, companyIdParam);
};

exports.getTimelogId = function(req, res) {
    return getObjectIdParam(req, res, timelogIdParam);
};

//private section
function getObjectIdParam(req, res, name) {
    var entityObjectId = getParameter(req, res, name);
    if(entityObjectId && ObjectID.isValid(entityObjectId)) {
        entityObjectId = new ObjectID(entityObjectId);
    } else {
        res.status(500).json({error: 'Invalid ' + name + ' format!'});
        entityObjectId = false;
    }
    return entityObjectId;
}

function getDateParam(req, res, name) {
    var date = getParameter(req, res, name);
    if(date) {
        return moment(date, dateFormat).toDate();
    }
};

function getParameter(req, res, name) {
    var param = req.params[name] || req.query[name];
    if(!param) {
        res.status(400).json({ error: name + ' is not specified!' });
    }
    return param;
}

//parse json. Date and ObjectId
exports.jsonParse = function(key, value) {
    if (typeof value === 'string') {
        if (key.toLowerCase().indexOf('date') > -1
                && moment(value, dateFormat).isValid()) {
            console.log(value);
            return moment(value, dateFormat).toDate();
        }
        var isIdField = key === '_id' //maybe should be some prefix/postfix ???
                        || key === 'userId' 
                        || key === 'projectId'
                        || key === 'companyId';
        if(isIdField && ObjectID.isValid(value)) {
            return new ObjectID(value);
        }
    }
    return value;
};

exports.jsonStringify = function(key, value) {
    var keyName = key.toLowerCase();
    var isDateField = keyName.indexOf('date') > -1
        || keyName === 'start'
        || keyName === 'end'
    if (isDateField
            && moment(value).isValid()) {
        return moment(value).format(dateFormat);
    }
    return value;
};

//Common Rest API
exports.restSaveObject = function(collection) {
    return function(req, res) {
        if(req.body) {
            var currentDate = new Date();
            var object = req.body;
            if(!object.createdOn) {
                object.createdOn = currentDate;
            }
            object.updatedOn = currentDate;
            var col = collection();
            col.save(object, {safe:true}, function (err, result) {
               sendSavedObject(err, res, object, result);
            });
        } else {
            res.status(500).json({error: "Empty body"});
        }
    };
};

function sendSavedObject(err, res, object, result) {
    if(err) {
        res.status(500).json(err);
    } else { 
        if(result.result.ok) {
            if(result.ops) {
                res.json(result.ops[0]);
            } else {
                res.json(object);
            }
        }    
    }
}
@@ -900,760 +900,8 @@
 %7D;%0A%0A
-//private section%0Afunction getObjectIdParam(req, res, name) %7B%0A    var entityObjectId = getParameter(req, res, name);%0A    if(entityObjectId && ObjectID.isValid(entityObjectId)) %7B%0A        entityObjectId = new ObjectID(entityObjectId);%0A    %7D else %7B%0A        res.status(500).json(%7Berror: 'Invalid ' + name + ' format!'%7D);%0A        entityObjectId = false;%0A    %7D%0A    return entityObjectId;%0A%7D%0A%0Afunction getDateParam(req, res, name) %7B%0A    var date = getParameter(req, res, name);%0A    if(date) %7B%0A        return moment(date, dateFormat).toDate();%0A    %7D%0A%7D;%0A%0Afunction getParameter(req, res, name) %7B%0A    var param = req.params%5Bname%5D %7C%7C req.query%5Bname%5D;%0A    if(!param) %7B%0A        res.status(400).json(%7B error: name + ' is not specified!' %7D);%0A    %7D%0A    return param;%0A%7D%0A%0A
 //pa
@@ -1121,40 +1121,8 @@
 ) %7B%0A
-            console.log(value);%0A
     
@@ -2477,16 +2477,768 @@
  %7D;%0A%7D;%0A%0A
+//private section%0Afunction getObjectIdParam(req, res, name) %7B%0A    var entityObjectId = getParameter(req, res, name);%0A    if(entityObjectId && ObjectID.isValid(entityObjectId)) %7B%0A        entityObjectId = new ObjectID(entityObjectId);%0A    %7D else %7B%0A        res.status(500).json(%7Berror: 'Invalid ' + name + ' format!'%7D);%0A        entityObjectId = false;%0A    %7D%0A    return entityObjectId;%0A%7D%0A%0Afunction getDateParam(req, res, name) %7B%0A    var date = getParameter(req, res, name);%0A    if(date) %7B%0A        return moment(date, dateFormat).toDate();%0A    %7D%0A%7D;%0A%0Afunction getParameter(req, res, name) %7B%0A    var param = req.params%5Bname%5D %7C%7C req.query%5Bname%5D;%0A    if(!param) %7B%0A        res.status(400).json(%7B error: name + ' is not specified!' %7D);%0A    %7D%0A    return param;%0A%7D%0A%0A
 function

Remove old Date logger