var express = require('express');
var app = express();
var server = require('http').createServer(app), io = require('socket.io').listen(server);
var osc = require('node-osc');

var host = process.argv.length > 2 ? process.argv[2] : 'distinto.lp-server.net';
var port = process.argv.length > 3 ? process.argv[3] : '5001';
var oscSender = new osc.Client(host, port);


var states = [];

server.listen(8080);
app.use(express.static('bootstrap'));

app.get('/', function (req, res) {
	res.sendfile(__dirname + '/index.html');
});

io.sockets.on('connection', function (socket) {
	//socket.emit('news', { hello: 'world' });

    var address = socket.handshake.address;
    console.log("New connection from " + address.address + ":" + address.port);

	socket.on('oscmessage', function (data) {

        var chan = data.chan;
        var value = data.val;

        if(chan == null) return;

        if(value == null) {
            if(states[chan] == null) {
                states[chan] = 1
            } else if(states[chan] == 1) {
                states[chan] = 0;
            } else if(states[chan] == 0) {
                states[chan] = 1;
            }
            value = states[chan];
        }

        // hier noch nicht einkommentieren, das sendet das sofort ins hackcenter
        //oscSender.send(chan, value);

        console.log(address, chan, value);
    });
});

