import {initialSetup} from "./setup";

const fs = window.require("fs");
const mkdirp = window.require("mkdirp");

export default class File {

    constructor() {
        this.setupFileName = "requesto.json";
        this.setupFilePath = `/Users/${window.process.env.USER}/.requesto/`;
        this.setupFileURL = `${this.setupFilePath}${this.setupFileName}`
        this.initialSetup = initialSetup;
        this.createSetupFolder = this.createSetupFolder.bind(this);
        this.createSetupFile = this.createSetupFile.bind(this);
        this.loadSetupFile = this.loadSetupFile.bind(this);
    }

    createSetupFolder(callback){
        mkdirp(this.setupFilePath, function (err) {
            console.log("createSetupFolder: ","An error ocurred creating the file " + err);
            return callback();
        });
    }

    createSetupFile(callback) {
        const initialSetup = this.initialSetup;
        const setupFileURL = this.setupFileURL;

        this.createSetupFolder(function(){
            fs.writeFile(setupFileURL,JSON.stringify(initialSetup), function(err) {
                if (err) {
                    console.log("createSetupFile: ","An error ocurred creating the file " + err);
                    return;
                }
                callback(initialSetup);
                console.log("createSetupFile: ","The file has been succesfully saved");
            });
        })
    }

    loadSetupFile(callback) {
        const createSetupFile = this.createSetupFile;
        fs.readFile(this.setupFileURL, 'utf-8', function(err, data) {
            if (err) {
                console.log("loadSetupFile: ",err.message);
                // alert("An error ocurred reading the file :" + err.message);
                createSetupFile(callback);
                return;
            }
            // Change how to handle the file content
            console.log("loadSetupFile: ","The file content is : " + data);
            // alert("The file has been read succesfully");

            callback(JSON.parse(data));
        })
    }

    updateSetupFile(content) {
        const setupFileURL = this.setupFileURL;

        this.createSetupFolder(function(){
            fs.writeFile(setupFileURL,JSON.stringify(content), function(err) {
                if (err) {
                    console.log("updateSetupFile: ","An error ocurred creating the file " + err);
                    return;
                }
                console.log("updateSetupFile: ","The file has been succesfully updated");
            });
        })
    }
}

@@ -856,105 +856,8 @@
 ) %7B%0A
-        const initialSetup = this.initialSetup;%0A        const setupFileURL = this.setupFileURL;%0A%0A
     
@@ -879,34 +879,30 @@
 SetupFolder(
-function()
+() =%3E 
 %7B%0A          
@@ -908,32 +908,38 @@
    fs.writeFile(
+this.s
 setupFileURL,JSO
@@ -950,16 +950,21 @@
 ringify(
+this.
 initialS
@@ -970,29 +970,24 @@
 Setup), 
-function
 (err)
+ =%3E
  %7B%0A     
@@ -1160,32 +1160,37 @@
                 
+this.
 callback(initial
@@ -1182,16 +1182,21 @@
 allback(
+this.
 initialS
@@ -1358,62 +1358,8 @@
 ) %7B%0A
-        const createSetupFile = this.createSetupFile;%0A
     
@@ -1402,24 +1402,16 @@
 utf-8', 
-function
 (err, da
@@ -1413,16 +1413,19 @@
 r, data)
+ =%3E
  %7B%0A     
@@ -1593,24 +1593,29 @@
             
+this.
 createSetupF
@@ -1950,57 +1950,8 @@
 ) %7B%0A
-        const setupFileURL = this.setupFileURL;%0A%0A
     
@@ -2014,16 +2014,21 @@
 iteFile(
+this.
 setupFil
@@ -2061,21 +2061,16 @@
 t), 
-function
 (err)
+ =%3E
  %7B%0A 

refactor to es6