const requestWebSiteFromToken = require('../tokenUtilities').requestWebSiteFromToken;
const token2Session = require('../tokenUtilities').token2Session;
const os = require("os");
const logger = require('../logger');

module.exports = function attachRoutes(app, config) {

    // Experimental documentation using markdown
    // Hack for processing md files. Should modify ejs module 
    app.get('/study/with', (req, res) => {
        res.render('evaluation/instructions-with-evaluator.ejs', { account: req.session});
    })

    app.get('/study/without', (req, res) => {
        res.render('evaluation/instructions-without-evaluator.ejs', { account: req.session});
    })

    app.get('/study/without-proba', (req, res) => {
        res.render('evaluation/instructions-without-probabilities.ejs', { account: req.session});
    })

    app.get('/documentation', (req, res) => {
        res.render('documentation/doc.ejs', { account: req.session});
    })

    app.get('/documentation-website', (req, res) => {
        res.render('documentation/doc-website.ejs', { account: req.session});
    })

    app.get('/documentation-extension', (req, res) => {
        res.render('documentation/doc-extension.ejs', { account: req.session});
    })

    app.get('/us', (req, res) => {
        res.render('documentation/aboutus.ejs', { account: req.session});
    })

    app.get('/download', (req, res) => {
        let PLUGIN_INFO;
        if (process.env.PLUGIN_INFO) {
            PLUGIN_INFO = JSON.parse(process.env.PLUGIN_INFO);
        }
        const stores = config.stores
        res.render('documentation/download.ejs', { account: req.session, stores, PLUGIN_INFO});
    });

    app.get('/plugin-info', (req, res) => {
        logger.info('GET plugin info');
        if (!process.env.PLUGIN_INFO) {
            return res.send({})
        } else {
            try {
                const info = JSON.parse(process.env.PLUGIN_INFO)
                return res.send({
                    version: info.version,
                    name: info.name,
                    description: info.description
                })
            } catch(error) {
                logger.error(error);
                return res.send({})
            }
        }
    })

    app.get('/', (req, res) => {
        if (req.session.jwt) {
            requestWebSiteFromToken(req.session.jwt)
            .then(webSiteList => {
                let sessionList = token2Session(req.session.jwt);
                res.render('index.ejs', { account: req.session , webSiteList, sessionList});    
            })
        } else {
            res.render('index.ejs', { account: req.session , webSiteList: []});
        }
    });




}