/**
 * Created by apizzimenti on 12/31/15.
 */


var args = require('./args'),
    clc = require('cli-color'),
    fs = require('fs'),
    parseArgs = require('minimist'),
    pathing = require('path');

var path = pathing.dirname(require.main.filename) + '/lib/config.json';

function Config(units, ip) {
    this.args = parseArgs(process.argv.slice(2));
    this.units = units;
    this.ip = ip;
}

Config.prototype.control = function () {

    if (this.args.save === true || this.args.s === true) {
        write(this.args, this.units, this.ip);
    } else if (!this.args.save || !this.args.s) {
        read(this.args, this.units, this.ip);
    } else {
        handleArgs(this.args, this.units, this.ip);
    }

};

function write(args, units, ip) {
    fs.writeFile(path, JSON.stringify(args), function (err) {
        if (err) {
            console.log(clc.red('✗ couldn\'t save preset'));
            handleArgs(args, units, ip);
        } else {
            console.log(clc.green('✓ saved preset'));
            handleArgs(args, units, ip);
        }
    });
}

function read(args, units, ip) {
    fs.readFile(path, function (err, data) {
        if (err) {
            console.log(clc.red('✗ couldn\'t read preset data: file not found or it doesn\'t exist'));
            handleArgs(args, units, ip);
        } else {
            console.log(clc.green('✓ got preset data'));
            handleArgs(JSON.parse(data), units, ip);
        }
    });
}

function handleArgs(argv, units, ip) {

    if (argv.c) {
        units = {
            type: 'si',
            tmp: '˚C',
            speed: 'mps'
        }
    }

    if (argv.address || argv.a) {
        args.address(address, args.weatherRequest, units);
    } else if (argv.lat && argv.long) {
        args.weatherRequest({lat: argv.lat, long: argv.long}, units);
    } else {
        args.automatic(ip, args.weatherRequest, units);
    }

}

function instance(argv, units, ip) {
    return new Config(argv, units, ip);
}

module.exports = instance;
@@ -744,24 +744,25 @@
 nits, ip) %7B%0A
+%0A
     fs.write
@@ -1054,32 +1054,33 @@
       %7D%0A    %7D);%0A
+%0A
 %7D%0A%0Afunction read
@@ -1095,24 +1095,25 @@
 nits, ip) %7B%0A
+%0A
     fs.readF
@@ -1231,48 +1231,195 @@
 ta: 
-file not found or it doesn%5C't exist
+you have no presets! run the ') + clc.bgBlack.white(' -s ' )%0A            + clc.red(' or the ') + clc.bgBlack.white('-save') + clc.red(' option after any commands to save preferences
 '));%0A
+%0A
     
@@ -1592,24 +1592,24 @@
       %7D%0A
-
     %7D);%0A
 %7D%0A%0Afunct
@@ -1600,16 +1600,17 @@
     %7D);%0A
+%0A
 %7D%0A%0Afunct

fix pathing, formatting