 fs          = require 'fs'
 path        = require 'path'
+
+chalk       = require 'chalk'
 
 configPath  = path.join __dirname, '../config.json'
 


module.exports =

    normalize : ( config ) ->
        
        for key, value of defaultConfig
            if !config[key]?
                config[key] = value
        config


    read : ->

        if fs.existsSync configPath

            file = fs.readFileSync configPath
            try
                return JSON.parse file
            catch
                console.log chalk.red 'Could not parse config file. Writing default config..'
                return @resetConfig()

        else

            console.log chalk.red 'Config does not exist. Writing default config..'
            @resetConfig()


    write : ( newConfig ) ->

        config = @read()
        config = @normalize config, newConfig
        config = @normalize defaultConfig, config

        fs.writeFileSync configPath, JSON.stringify config, null, '\t'

