'use strict';

var fs = require('fs');

/**
 *
 * @class Utils
 * @namespace node-mock-server
 * @constructor
 *
 * Swagger importer
 */
function Utils() {
	this.init();
}

Utils.prototype = {

	constructor : Utils,

	/**
	 *
	 * @method init
	 * called by constructor
	 * @returns void
	 * @public
	 */
	init: function () {
	},

	/**
	 *
	 * @method forIn
	 * @param {object} obj
	 * @param {function} callback
	 * @returns void
	 * @public
	 */
	forIn: function (obj, callback) {
		for (var key in obj) {
			if (obj.hasOwnProperty(key)) {
				callback.call(this, key, obj[key]);
			}
		}
	},

	/**
	 *
	 * @method for
	 * @param {Array} arr
	 * @param {function} callback
	 * @returns void
	 * @public
	 */
	for: function (arr, callback) {

		var i, len = arr.length;

		for (i = 0; i < len; i +=1) {
			callback.call({}, arr[i], i);
		}
	},

	/**
	 *
	 * @method isFilledString
	 * @param {string} value
	 * @returns {boolean} isFilledString
	 * @public
	 */
	isFilledString: function (value) {
		return (typeof value === 'string' && value !== '');
	},

	/**
	 *
	 * @method getStats
	 * @param {string} dirFileName
	 * @returns {object} getStats
	 * @public
	 */
	getStats: function (dirFileName) {
		return fs.statSync(dirFileName);
	},

	/**
	 *
	 * @method isDir
	 * @param {string} dirName
	 * @returns {boolean} isDir
	 * @public
	 */
	existDir: function (dirName) {

		var isDir = false;

		try {
			isDir = this.getStats(dirName).isDirectory();
		} catch (err) {}

		return isDir;
	},


	/**
	 *
	 * @method existFile
	 * @param {string} path
	 * @returns {boolean} isFile
	 * @public
	 */
	existFile: function (path) {

		var isFile = false;

		try {
			isFile = this.getStats(path).isFile();
		} catch (err) {}

		return isFile;
	},

	/**
	 *
	 * @method writeDir
	 * @param {string} dirName
	 * @public
	 */
	writeDir: function (dirName) {
		if (!this.existDir(dirName)) {
			fs.mkdirSync(dirName);
		}
	},

	/**
	 *
	 * @method writeFile
	 * @param {string} path
	 * @param {object} data
	 * @public
	 */
	writeFile: function (path, data) {
		fs.writeFileSync(path, data);
	},

	/**
	 *
	 * @method readFile
	 * @param {string} path
	 * @returns {string}
	 * @public
	 */
	readFile: function (path) {
		return fs.readFileSync(path, 'utf8');
	},

	/**
	 *
	 * @method removeFile
	 * @param {string} path
	 * @public
	 */
	removeFile: function (path) {
		try {
			fs.unlinkSync(path);
		} catch (err) {}
	},

	/**
	 *
	 * @method writeFile
	 * @param {string} dir
	 * @param {Array|undefined} ignoreFiles
	 * @public
	 */
	readDir: function (dir, ignoreFiles) {
		var results = [],
			list = [];

		ignoreFiles = ignoreFiles || [];

		try {
			list = fs.readdirSync(dir);
		} catch (err) {
			if (process.env.NODE_ENV !== 'test') {
				console.log('Folder "' + err.path + '" not found!');
			}
		}

		list.forEach(function(file) {
			if (ignoreFiles.indexOf(file) < 0) {
				results.push({
					path: dir + '/' + file,
					file: file
				});
			}
		});
		return results;
	},
	/**
	 * Capitalizes the first letter of the given string.
	 *
	 * @method capitalize
	 * @param {String} str
	 *      The original string
	 * @return {String}
	 *      The capitalized string
	 */
	capitalize: function(str) {
		// Capitalize the first letter
		return str.substr(0, 1).toUpperCase().concat(str.substr(1));
	},

	/**
	 * Camelizes the given string.
	 *
	 * @method toCamel
	 * @param {String} str
	 *      The original string
	 * @return {String}
	 *      The camelized string
	 */
	toCamel: function(str) {
		return str.replace(/(\-[A-Za-z])/g, function($1) {
			return $1.toUpperCase().replace('-', '');
		});
	},

	/**
	 *
	 * @method getPreferences
	 * @param {object} obj
	 * @returns {object} preferences
	 * @public
	 */
	getPreferences: function (obj) {
		var data = {},
			file = obj.restPath + '/preferences.json';

		try {
			data = JSON.parse(this.readFile(file));
		} catch (err) {}

		return data;
	}

};

module.exports = Utils;
@@ -3909,16 +3909,340 @@
 n data;%0A
+%09%7D,%0A%0A%09/**%0A%09 *%0A%09 * @method getMethodStore%0A%09 * @param %7Bstring%7D path%0A%09 * @returns %7Bobject%7D%0A%09 * @public%0A%09 */%0A%09getMethodStore: function (path) %7B%0A%0A%09%09var pathFull = path + '/.store.json';%0A%0A%09%09if (!this.existFile(pathFull)) %7B%0A%09%09%09return %7B%7D;%0A%09%09%7D%0A%0A%09%09try %7B%0A%09%09%09return JSON.parse(this.readFile(pathFull));%0A%09%09%7D catch (err) %7B%7D%0A%0A%09%09return %7B%7D;%0A
 %09%7D%0A%0A%7D;%0A%0A

add Utils method getMethodStore