var express = require('express');
var router = express.Router();
var path = require("path")
var auth = require("../../modules/sub-auth")
var log = require("loguix")
var { __glob } = require("../../modules/global-variables")
var uuid = require("uuid")
const metric = require("webmetrik")


var fs = require("fs")

var md = require('markdown-it')({
    html:         true,        // Enable HTML tags in source
    xhtmlOut:     true,        // Use '/' to close single tags (<br />).
                                // This is only for full CommonMark compatibility.
    breaks:       true,        // Convert '\n' in paragraphs into <br>
    langPrefix:   'language-',  // CSS language prefix for fenced blocks. Can be
                                // useful for external highlighters.
    linkify:      true,        // Autoconvert URL-like text to links
  
    // Enable some language-neutral replacement + quotes beautification
    // For the full list of replacements, see https://github.com/markdown-it/markdown-it/blob/master/lib/rules_core/replacements.js
    typographer:  true,
  
    // Double + single quotes replacement pairs, when typographer enabled,
    // and smartquotes on. Could be either a String or an Array.
    //
    // For example, you can use '«»„“' for Russian, '„“‚‘' for German,
    // and ['«\xA0', '\xA0»', '‹\xA0', '\xA0›'] for French (including nbsp).
    quotes: '“”‘’',
  
    // Highlighter function. Should return escaped HTML,
    // or '' if the source string is not changed and should be escaped externally.
    // If result starts with <pre... internal wrapper is skipped.
    highlight: function (/*str, lang*/) { return ''; }
  });



router.get('/', function(req, res, next) {

    try{
        const visitorCounted = new metric.Metric("visitorCounted", "Nombre de visiteurs")
        visitorCounted.setValue(visitorCounted.getValue() + 1)
    } catch(err) {
        console.error(err)
    }
  

    if(!auth.checkUser(req.cookies.token)) {

        res.redirect("/login")
    } else {

        var fileMd = md.render(fs.readFileSync(__glob.README).toString())
        if(process.env.DEV) {
            res.render("index", {welcome: fileMd, dev: "<p class='yellow'>DEVELOPEMENT</p>"})
        } else {

            res.render("index", {welcome: fileMd, dev: ""})
        }
     
    }

  
});


module.exports = router;