const express = require("express");
const path = require("path");
const logger = require("../logger");

const flDir = require("./fldir-router");
const connectArea = require("./connect-router");
const api = require("./web-api/api-router");

module.exports = (address, port) => {
    const app = express();
    logger.log("Static in", path.join(__dirname, "public"));
	
	app.use((req, res, next) => {
		// logger.log("HTTP headers:", req.headers);
		next();
	});
	
    app.use(express.static(path.join(__dirname, "public")));

    app.use("/fl_dir", flDir);
    app.use("/api", api);
	
	app.get("/connect-area", connectArea);
	
    app.use("/*", (req, res) => {
        res.send(`<!DOCTYPE html>
<html>
    <head>
        <title>AI Adventure Labs</title>
        <meta charset="utf-8">
    </head>
    <body class="bg text-white">
        <div id="js-required">
            <h1>Error: Javascript off</h1><hr/>
            <p>Javascript is required for the site to work, enable it in your browser settings</p>
        </div>
        <script>
            document.getElementById("js-required").innerHTML = "<p>Loading...</p>";
        </script>
        <script src="/bootstrap/bootstrap.bundle.min.js" defer></script>
		<script src="/fl_framework/index.js" defer></script>
		<script src="/js/translate.js" defer></script>
		<script src="/js/jsencrypt.js" defer></script>
		<script src="/js/main.js" defer></script>
    </body>
</html>`);
    });

    app.listen(port, address, (err) => {
        if (err)
            throw err;
        logger.log(`HTTP page successful runned at port ${port}`);
    });
};
