const { LogType } = require("loguix")
const fs = require("fs")
const path = require("path")
const { __glob } = require("./global-variables")
const clog = new LogType("Fichier")
const os = require("os");
const uuid = require('uuid');
var mime = require('mime-types');
const { set } = require("../main")

// check if shared folder exist, if not create it
if(!fs.existsSync(__glob.SHARED)) {
    fs.mkdirSync(__glob.SHARED)
}

module.exports.getFiles = function(root) {

    var response = new Object()
    response.content = new Array()


    try{

        if(root == "homepath") {

            root = os.homedir()
        
        }

        if(root == "sharepath") {

            root = __glob.SHARED
        
        }

        
        if(!fs.existsSync(root)) {
           response.content = "NOT_EXIST" 
        
        } else {
            
            for(var file of fs.readdirSync(root)) {
    
                const stat = fs.statSync(root + path.sep + file)
                                  
                response.content.push({"name":file, id: uuid.v4().toString() ,"fileDirectory" : root + path.sep + file  , "type":mime.lookup(file), "size":stat.size, "lastedition":stat.mtimeMs, "directory":stat.isDirectory()})
            }
            
        }

       

       
        
    }catch(err) {

        response.content = "NOT_PERMITTED"
    }

    response.root = root
    response.parent = path.dirname(root)


    return response
   
}

module.exports.createFolder = function(root) {

    // Check if folder already exist

    if(!fs.existsSync(root)) {

        try {
            fs.mkdirSync(root)
            return "OK"
        } catch(err) {
          
            return "NOT_PERMITTED"
        }
        
    } else {

        return 'EXIST'
    }

}

module.exports.deleteFile = function(root) {
    
    if(fs.existsSync(root)) {
    
        try {
    
            fs.rmSync(root, { recursive: true, force: true })
            return "OK"
        } catch(err) {
            console.log(err)
            return "NOT_PERMITTED"
        }
    } else {
    
        return "NOT_EXIST"
    }
}

module.exports.renameFile = function(settings) {

    if(fs.existsSync(settings.root)) {

        try {

            fs.renameSync(settings.root + path.sep + settings.oldName, settings.root + path.sep + settings.newName)
            return "OK"
        } catch(err) {
          
            return "NOT_PERMITTED"
        }
    } else {

        return "NOT_EXIST"
    }
}

module.exports.shareFile = function(settings) {
    

    if(fs.existsSync(settings.root)) {

        try {
            //Create a sybolic link to shared folder

            fs.symlinkSync(settings.root + path.sep + settings.name, __glob.SHARED + path.sep + settings.name)
            
            // return the link of the file shared
            if(process.env.DEV) {
                return "http://localhost:3001/shared/" + settings.name

            } else {
                return "https://neutral.raphix.fr/shared/" + settings.name

            }

        } catch(err) {
            console.log(err)
            return "NOT_PERMITTED"
        }
    } else {

        return "NOT_EXIST"
    }
}


module.exports.getFile = function(root) {
    
        if(fs.existsSync(root)) {
    
            try {
    
                return fs.readFileSync(root, "utf-8")
            } catch(err) {
                console.log(err)
                return "NOT_PERMITTED"
            }
        } else {
    
            return "NOT_EXIST"
        }
    
}

module.exports.saveFile = function(settings) {

    try {

        fs.writeFileSync(settings.root + path.sep + settings.name, settings.content)
        return "OK"
    } catch(err) {
        console.log(err)
        return "NOT_PERMITTED"
    }
}

module.exports.downloadFile = function(root) {

    if(fs.existsSync(root)) {

        try {

            return fs.readFileSync(root)
        } catch(err) {
            console.log(err)
            return "NOT_PERMITTED"
        }
    } else {

        return "NOT_EXIST"
    }
}



module.exports.uploadFile = function(settings) {

    try {
    
        fs.writeFileSync(settings.root + path.sep + settings.name, settings.file)
        return "OK"
    } catch(err) {
        console.log(err)
        return "NOT_PERMITTED"
    }
}

module.exports.newFile = function(settings) {
    
        if(!fs.existsSync(settings)) {
    
            try {
    
                fs.writeFileSync(settings, "")
                return "OK"
            } catch(err) {
                console.log(err)
                return "NOT_PERMITTED"
            }
        } else {
    
            return "EXIST"
        }
    }