const { LogType } = require("loguix")
const fs = require("fs")
const path = require("path")
const { __glob } = require("./global-variables")
const auth = require("./auth")
const files = require("./files")
const links = require("./links.js")
const service = require("./services")
const plog = new LogType("Web")
const cook = require("cookie")
const http = require("http")

/**
 * NOTE INTERNE
 * 
 * Changer les pictures de users autre part
 */

/**
 * 
 * @param {http.Server} server 
 */

module.exports.serverIO = function(server) {

    const io = require('socket.io')(server)

    io.on("connection", (socket) => {
        let token = cook.parse(socket.handshake.headers.cookie).token
        const user = auth.getUserByToken(token)

        if(user) {
            plog.log("Connexion au panel par '" + user.username + "' avec le socket : " + socket.id)
            user.setLastLogin(new Date())
            /**
             * GET REQUEST
             */ 

            // Get Users
            GetRequest("USERINFO", () => {

                GetAnswer("USERINFO", {username: user.username, display_name: user.display_name ,picture: user.picture, permission: user.permission})
            })

            /**
             * POST REQUEST
             */

            if(user.checkPermission("FILES_EXPLORER")) {

            PostRequest("FX_GET", (root) => {

                PostAnswer("FX_GET", files.getFiles(root))
            })

            PostRequest("FX_NEW_FOLDER", (root) => {

                PostAnswer("FX_NEW_FOLDER", files.createFolder(root))
            })

            PostRequest("FX_DELETE", (root) => {

                PostAnswer("FX_DELETE", files.deleteFile(root))
            } )

            PostRequest("FX_RENAME", (settings) => {
                    
                PostAnswer("FX_RENAME", files.renameFile(settings))
            }) 

            PostRequest("FX_SHARE", (settings) => {
                        
                PostAnswer("FX_SHARE", files.shareFile(settings))
            })

            PostRequest("FX_GETFILE", (root) => {
                    
                PostAnswer("FX_GETFILE", files.getFile(root))
            })

            PostRequest("FX_SAVEFILE", (settings) => {
                    
                PostAnswer("FX_SAVEFILE", files.saveFile(settings))
            })

            PostRequest("FX_NEW_FILE", (settings) => {
                        
                PostAnswer("FX_NEW_FILE", files.newFile(settings))
            }) 
            
            PostRequest("FX_UPLOAD", (settings) => {

                PostAnswer("FX_UPLOAD", files.uploadFile(settings))

            })

            } 

            if(user.checkPermission("SERVICES")) {

                PostRequest("SV_GET_SERVICE_STATUS", async (sv) => {
                    PostAnswer("SV_GET_SERVICE_STATUS", {answer: await service.getServiceStatus(sv), name: sv})
                })  

                PostRequest("SV_START_SERVICE", async (sv) => {
                    PostAnswer("SV_START_SERVICE", {answer: await service.startService(sv), name: sv})
                })

                PostRequest("SV_STOP_SERVICE", async (sv) => {
                    PostAnswer("SV_STOP_SERVICE", {answer: await service.stopService(sv), name: sv})
                })

                PostRequest("SV_RESTART_SERVICE", async (sv) => {
                    PostAnswer("SV_RESTART_SERVICE", {answer: await service.restartService(sv), name: sv})
                })

            }

            if(user.checkPermission("LINKS")) {
                PostRequest("LINKS_GET_ALL", () => {
                    PostAnswer("LINKS_GET_ALL", {answer: "OK", links: links.getLinks()})
                })

                PostRequest("LINKS_ADD", (settings) => {
                    PostAnswer("LINKS_ADD", {answer: links.addLink(settings)})
                })

                PostRequest("LINKS_DELETE", (id) => {
                    PostAnswer("LINKS_DELETE", links.removeLink(id))
                })

                PostRequest("LINKS_EDIT", (settings) => {
                    PostAnswer("LINKS_EDIT", links.updateLink(settings.id, settings))
                })


            }

            socket.on("disconnect", () => {

                plog.log("Déconnexion du panel par '" + user.username + "' avec le socket : " + socket.id)
            })

            function GetRequest(GQname, GQcallback) {
                socket.on("GET/" + GQname, () => {
                
                    plog.log(user.username + " - Socket : " + socket.id  + " - GET/" + GQname + " - [RECIEVED]") 
                    GQcallback()
                })
        
            }
    
            function GetAnswer(GRname, GRvalue) {
            
                plog.log(user.username + " - Socket : " + socket.id  + " - GET/" + GRname + " - [ANSWERED]") 
                socket.emit("ANSWER/GET/" + GRname, GRvalue)
    
            }

            function PostRequest(GQname, GQcallback) {
                socket.on("POST/" + GQname, (value) => {
                    plog.log(user.username + " - Socket : " + socket.id  + " - POST/" + GQname + " - [RECIEVED]") 
                    GQcallback(value)
                })
        
            }
    
            function PostAnswer(GRname, GRvalue) {
            
                plog.log(user.username + " - Socket : " + socket.id  + " - POST/" + GRname + " - [ANSWERED]") 
                socket.emit("ANSWER/POST/" + GRname, GRvalue)
    
            }

        } else {

            socket.disconnect()
            plog.warn("Connexion directe vers le panel avec un token inexistant : " + this.token)
        }
        

        
    
       
    })


}
