var express = require('express');
var router = express.Router();

var mpd = require('mpd');
var cmd = mpd.cmd;

function connect() {
    return mpd.connect({
        port: 6600,
        host: 'localhost'
    });
}

router.get('/currentsong', function(req, res) {
    var client = connect();
    client.on('ready', function() {
        client.sendCommand(cmd('currentsong', []), function(err, msg) {
            if (err) throw err;

            var result = {};
            var lines = msg.split('\n');

            lines.forEach(function(statusline) {
                var statuslineArray = statusline.split(': ');
                var key = statuslineArray[0];
                var value = statuslineArray[1];
                result[key] = value;
            });

            res.send(result);
        });
    });
});

router.get('/status', function(req, res) {
    var client = connect();
    client.on('ready', function() {
        client.sendCommand(cmd('status', []), function(err, msg) {
            if (err) throw err;
            var result = {};
            var lines = msg.split('\n');
            lines.forEach(function(statusline) {
                var statuslineArray = statusline.split(': ');
                var key = statuslineArray[0];
                var value = statuslineArray[1];
                result[key] = value;
            });

            res.send(result);
        });
    });
});

router.post('/stop', function(req, res) {
    var client = connect();
    client.on('ready', function() {
        client.sendCommand(cmd('stop', []), function(err, msg) {
            if (err) throw err;
            res.sendStatus(200);
        });
    });
});

router.post('/next', function(req, res) {
    var client = connect();
    client.on('ready', function() {
        client.sendCommand(cmd('next', []), function(err, msg) {
            if (err) throw err;
            res.sendStatus(200);
        });
    });
});

router.post('/pause', function(req, res) {
    var client = connect();
    client.on('ready', function() {
        client.sendCommand(cmd('pause', []), function(err, msg) {
            if (err) throw err;
            res.sendStatus(200);
        });
    });
});

router.post('/play', function(req, res) {
    var client = connect();
    client.on('ready', function() {
        client.sendCommand(cmd('play', []), function(err, msg) {
            if (err) throw err;
            res.sendStatus(200);
        });
    });
});

module.exports = router;

@@ -806,24 +806,996 @@
    %7D);%0A%7D);%0A%0A
+router.get('/playlists', function(req, res) %7B%0A    var client = connect();%0A    client.on('ready', function() %7B%0A        client.sendCommand(cmd('listplaylists', %5B%5D), function(err, msg) %7B%0A            if (err) throw err;%0A            var result = %5B%5D;%0A            var lines = msg.split('%5Cn');%0A%0A            lines.forEach(function(statusline) %7B%0A                var statuslineArray = statusline.split(': ');%0A                var key = statuslineArray%5B0%5D;%0A                var value = statuslineArray%5B1%5D;%0A                if (key == 'playlist') %7B%0A                    result.push(value);%0A                %7D%0A            %7D);%0A%0A            res.send(result);%0A        %7D);%0A    %7D);%0A%7D);%0A%0A%0Arouter.post('/playlists/:playlistname/load', function(req, res) %7B%0A    var client = connect();%0A    client.on('ready', function() %7B%0A        client.sendCommand(cmd('load', %5Breq.params.playlistname%5D), function(err, msg) %7B%0A            if (err) throw err;%0A            res.sendStatus(200);%0A        %7D);%0A    %7D);%0A%7D);%0A%0A
 router.get('
@@ -3149,32 +3149,295 @@
 );%0A    %7D);%0A%7D);%0A%0A
+router.post('/clear', function(req, res) %7B%0A    var client = connect();%0A    client.on('ready', function() %7B%0A        client.sendCommand(cmd('clear', %5B%5D), function(err, msg) %7B%0A            if (err) throw err;%0A            res.sendStatus(200);%0A        %7D);%0A    %7D);%0A%7D);%0A%0A
 router.post('/pl

add playlists feature