/*

This demonstrates how POST request to our media server (see IP:Port in Options > Media Sharing)
catched bellow to start playing

Currently it accepts only POST requests including MMCustomRequest in the request header and valid JSON as request body

The test request available at: https://reqbin.com/uw0qc1ma

POST / HTTP/1.1
Host: 127.0.0.1
MMCustomRequest: true
Content-Type: application/json
Content-Length: 92

{
 "target":"remoteMMControlTestScript",
 "clientID":"remoteTestClient",
 "command":"play"
}


*/
app.listen(app, 'remoteRequest', (r) => {
    var content = JSON.parse(r.requestBody);
    if (content.target == 'remoteMMControlTestScript' && content.clientID == 'remoteTestClient') {
        if (content.command == 'play') {
            if (app.player.entriesCount > 0) {
                r.asyncResult = true;
                app.player.playAsync().then(() => {
                    var track = app.player.getCurrentTrack();
                    if (track)
                        r.responseBody = 'Playback of track ' + track.summary + ' started';
                    else
                        r.responseBody = 'Playback started';
                });
            } else {
                r.responseBody = 'No track in \'Playing\' list';
            }
        }
    }
});
