const os = require('os');
const http = require('http');
const express = require('express');
const WebSocketServer = require('websocket').server;

const HttpPort = 80;
const WsPort = 81;

class LostMyNuts
{
    constructor(win)
    {
        this.win = win;
        win.showDevTools();
        const address = Object.values(os.networkInterfaces()).flatMap(i => i).filter(i => i.family == "IPv4" && !i.internal)[0].address

	var gameContainer = document.getElementById("gameContainer");
	Engine.InitEngine(gameContainer, { width: window.innerWidth, height: window.innerHeight, roundPixels: false} );

	var gameManager = new GameManager();
	gameManager.init("js/");

	window.addEventListener('resize', this.resize.bind(this));
        // start up express
        this.app = express();

        // ws server requires an http server (though we're not going to start it)
        this.httpServer = http.createServer(this.app);
        this.app.use(express.static('shared/'));
        this.app.use(express.static('client/'));

        // web socket server to handle the client/server websocket communications
        this.wsServer = new WebSocketServer({
            httpServer: this.httpServer,
            autoAcceptConnections: false,
            maxReceivedFrameSize : 1024000,
        });

        this.mode = new JoinMode();

        // start listening
        this.app.listen(HttpPort, () => console.log('HTTP server listening on port ' + HttpPort + '!'));
        this.httpServer.listen(WsPort, () => console.log('WebSocket Server started on port ' + WsPort + '!'));
        this.wsServer.on('request', this.connect.bind(this));
    }

    connect(req)
    {
        this.mode.connect(req);
    }
    resize()
    {
            // Resize the renderer
            Engine.Pixi.renderer.resize(window.innerWidth, window.innerHeight);
            Game.Game.resolutionChanged(window.innerWidth, window.innerHeight);
            //(app.screen.width, app.screen.height);
    }
}

let win = nw.Window.get();
console.log(win);
win.on("loaded", () => new LostMyNuts(win));
