var fs = require('fs');
var path = require("path");
var filepath = path.join(__dirname, '..', 'config', 'global.json');


var Gconfig = function(){
    
    var loadJson = function (callback) {
        fs.readFile(filepath, { encoding: 'utf8' }, function (err, data) {
            if (err) return callback(err); // file reading error
            try {
                // parse and return json to callback
                var json = JSON.parse(data);
                return callback(null, json.global);
            } catch (ex) {
                // catch JSON parsing errors so your app doesn't crash
                console.log(ex);
                return callback(ex);
            }
        });
    }
    
    var writeJson = function (json, callback) {
        fs.writeFile(filepath, JSON.stringify(json), callback);
    }
    
    return {
        loadJson: loadJson
        ,writeJson: writeJson
    }
    
}();

module.exports = Gconfig;
