'use strict';

/** 
 * The default settings 
 * @private
 * @type {Object}
 */ 
const defaultConfig = {
	homePlugin: 'Overview',
	siadAddress: 'http://localhost:9980',
	zoom: 1,
};

/**
 * The config object derived from the config.json file used to store UI settings
 * @typedef {Object} config
 * @property {string} homePlugin - The name of the default plugin, usually 'Overview'
 * @property {string} siadAddress - Usually 'http://localhost:9980'
 * @property {string} siadCommand - The command to run siad
 * @property {number} zoom - The zoom factor for the UI
 */

/**
 * Holds all config.json related logic for the UI
 * @module UIConfig
 */
module.exports = {
	/**
	 * Writes the current config to defaultConfigPath
	 * @param {config} config - config in memory
	 * @param {string} path - UI's defaultConfigPath
	 */
	save: function(config, path) {
		Fs.writeFile(path, JSON.stringify(config, null, '\t'), function(err) {
			if (err) {
				console.log(err);
			} 
		});
	},

	/**
	 * Finds if a config file exists and uses default if not
	 * @param {string} path - UI's defaultConfigPath
	 * @param {callback} callback
	 */
	load: function(path, callback) {
		Fs.readFile(path, function(err, data) {
			if (err) {
				// no file found, use default config
				callback(defaultConfig);
			} else {
				// found config, use it
				callback(JSON.parse(data));
			}
		});
	},

	/**
	 * returns the defaultConfig
	 * @param {callback} callback
	 */
	reset: function(callback) {
		callback(defaultConfig);
	},
};

@@ -962,17 +962,16 @@
 r);%0A%09%09%09%7D
- 
 %0A%09%09%7D);%0A%09
@@ -1201,32 +1201,55 @@
 ta) %7B%0A%09%09%09if (err
+ %7C%7C data == 'undefined'
 ) %7B%0A%09%09%09%09// no fi

Revert to default configuration if config.json is `undefined`