/*
	Licensed Material - Property of IBM

	(C) Copyright 2016 IBM Corp.

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/


'use strict';

//private modules
var errors = require('mdo-errors');
//public modules
var	fs = require('fs');
var	log = require('npmlog');
var	path = require('path');
var q = require('q');

/** Function to create a file from a Object
		@param deploymentData - Object that represents properties in the deployment.data
		@param name - Name of the file to write
		@param filepath - filepath of where the file should be written
		@return - A promise of whether the file was written or not
**/
module.exports = function(deploymentData, name, filepath) {

	var d = q.defer(),
			filename = name;

	// build the contents
	// Date format looks like:  #Fri May 01 08:30:41 EDT 2015
	var contents = '#Deployment data.\n';
	contents += '# ' + new Date().toString() + '\n';
	Object.keys(deploymentData).forEach(function(key) {
			contents += key + '=' + deploymentData[key] + '\n';
	});

	fs.writeFile(path.join(filepath,filename), contents, function(err) {
		if(err) {
			log.verbose('createFile', 'Error creating file: '+err+' '+JSON.stringify(err));
			var zipFileErr = errors.getError('ERRWRITEFILE', err);
			d.reject(zipFileErr);
			return;
		}
		d.resolve();
	});

	return d.promise;
};
