var express = require('express');
var router = express.Router();
var path = require("path")
var fs = require("fs")
var CryptoJS = require("crypto-js");
var uuid = require("uuid")
var ntr = require("../neutral-functions.js");
const Jimp = require("jimp");
const { compileString } = require('sass');

/* GET home page. */
router.get('/', function(req, res, next) {

  
  var check = ntr.checkToken(req, res) 

  if(check.name != false) {

    res.redirect(302, "/")
   
  } else {

    res.render('login', {error: ""});
  }

});

router.post("/", function(req, res, next) {

  var check = ntr.checkToken(req, res) 
  
  if(check.name == false) {

    res.send({"result":"failed", "content":"ERROR_TOKEN_NOT_VALID"})
    
    
  } else {

    const image = req.files.apic

    if(req.headers.uploadforuser != null) {

        if(check.permLevel == 4) {

            if(req.files.apic.mimetype == "image/png" | req.files.apic.mimetype == "image/jpeg" ) {
    
                var uploadPath = "";
                var uploadDir = __dirname.replace("routes","public" + path.sep + "images" + path.sep + "userspics" + path.sep)

               
                if(req.files.apic.mimetype == "image/png") {
        
                    uploadPath = req.headers.uploadforuser + ".png"
                }
        
                if(req.files.apic.mimetype == "image/jpeg") {
        
                    uploadPath =  req.headers.uploadforuser+ ".jpg"
                }
        
                image.mv(__dirname + path.sep + uploadPath, function(err) {
                    if(err) {
                        res.send({"result":"success", "content": "La photo de profil n'a pas pu être chargé."})
                        console.log(err)
                    } else {
            
                       
                        
                        if(fs.existsSync(uploadDir + req.headers.uploadforuser + ".png") | fs.existsSync(uploadDir + req.headers.uploadforuser + ".jpg")) {
                            
                            if(fs.existsSync(uploadDir + req.headers.uploadforuser+ ".png")) {
        
                                fs.rmSync(uploadDir + req.headers.uploadforuser + ".png")
                            }
        
                            fs.renameSync(__dirname.replace("routes", "") + uploadPath , uploadDir + uploadPath)
        
                            if(req.files.apic.mimetype == "image/jpeg") {
                                    Jimp.read(uploadDir + uploadPath, function (err, image) {
                                            image.resize(200,200, function(err){
                                                if (err) throw err;
                                    }).write(uploadDir + uploadPath.replace("jpg", "png"))
                                    fs.rmSync(uploadDir + req.headers.uploadforuser + ".jpg")
        
                                    res.send({"result":"success", "content": "<span style='color:rgb(130, 255, 163);'>La photo de profil a été changé avec succès.</span>",})
                                    
                                    })
                                  
                                
                                
                                
                                  
                            } else if(req.files.apic.mimetype == "image/png") {
                                    Jimp.read(uploadDir + uploadPath, function (err, image) {
                                        image.resize(200,200, function(err){
                                            if (err) throw err;
                                     }).write(uploadDir + uploadPath)
                                
                                        res.send({"result":"success", "content": "<span style='color:rgb(130, 255, 163);'>La photo de profil a été changé avec succès.</span>",})
        
                                     })
                
                                
        
                            } else { 
                                
                                res.send({"result":"success", "content": "<span style='color:rgb(130, 255, 163);'>La photo de profil a été changé avec succès.</span>",})
        
        
                            }
        
                           
                           
                          
                            
                        } else {
        
                            console.log(uploadDir + uploadPath)
                            res.send({"result":"success", "content": "La photo de profil n'a pas pu être chargé."})
                            console.log("ERROR - UPLOAD NOT WORK CORRECYLT")
                            
                        }
                        
                    
                    
                    }   
            
                })
            
        
            } else {
                res.send({"result":"success", "content": "Seul les images (JPEG et PNG) sont acceptés."})
        
            }
        }


    } else if(req.headers.uploadfiledirectory != null) {

       if(fs.existsSync(req.headers.uploadfiledirectory)) {
        image.mv(req.headers.uploadfiledirectory + path.sep + req.files.apic.name, (error) => {

            if(error) {
                res.send({"result":"ERROR"})

            } else {

                res.send({"result":"SUCCESS"})
            }
        })
    

       } else {
            
            res.send({"result":"ERROR"})
       }

        
    
    } else {

        
    if(req.files.apic.mimetype == "image/png" | req.files.apic.mimetype == "image/jpeg" ) {
    
        var uploadPath = "";
        var uploadDir = __dirname.replace("routes","public" + path.sep + "images" + path.sep + "userspics" + path.sep)

        if(req.files.apic.mimetype == "image/png") {

            uploadPath = ntr.checkToken(req, res).name + ".png"
        }

        if(req.files.apic.mimetype == "image/jpeg") {

            uploadPath =  ntr.checkToken(req, res).name + ".jpg"
        }

        image.mv(__dirname + path.sep + uploadPath, function(err) {
            if(err) {
                res.send({"result":"success", "content": "La photo de profil n'a pas pu être chargé."})
                console.log(err)
            } else {
    
               
                
                if(fs.existsSync(uploadDir + ntr.checkToken(req, res).name + ".png") | fs.existsSync(uploadDir + ntr.checkToken(req, res).name + ".jpg")) {
                    
                    if(fs.existsSync(uploadDir + ntr.checkToken(req, res).name + ".png")) {

                        fs.rmSync(uploadDir + ntr.checkToken(req, res).name + ".png")
                    }

                    fs.renameSync(__dirname.replace("routes", "") + uploadPath , uploadDir + uploadPath)

                    if(req.files.apic.mimetype == "image/jpeg") {
                            Jimp.read(uploadDir + uploadPath, function (err, image) {
                                    image.resize(200,200, function(err){
                                        if (err) throw err;
                            }).write(uploadDir + uploadPath.replace("jpg", "png"))
                            fs.rmSync(uploadDir + ntr.checkToken(req, res).name + ".jpg")

                            res.send({"result":"success", "content": "<span style='color:rgb(130, 255, 163);'>La photo de profil a été changé avec succès.</span>",})
                            
                            })
                          
                        
                        
                        
                          
                    } else if(req.files.apic.mimetype == "image/png") {
                            Jimp.read(uploadDir + uploadPath, function (err, image) {
                                image.resize(200,200, function(err){
                                    if (err) throw err;
                             }).write(uploadDir + uploadPath)
                        
                                res.send({"result":"success", "content": "<span style='color:rgb(130, 255, 163);'>La photo de profil a été changé avec succès.</span>",})

                             })
        
                        

                    } else { 
                        
                        res.send({"result":"success", "content": "<span style='color:rgb(130, 255, 163);'>La photo de profil a été changé avec succès.</span>",})


                    }

                   
                   
                  
                    
                } else {

                    console.log(uploadDir + uploadPath)
                    res.send({"result":"success", "content": "La photo de profil n'a pas pu être chargé."})
                    console.log("ERROR - UPLOAD NOT WORK CORRECYLT")
                    
                }
                
            
            
            }   
    
        })
    

    } else {
        res.send({"result":"success", "content": "Seul les images (JPEG et PNG) sont acceptés."})

    }

    
    }
    
  }


})

module.exports = router;


