// Description:
//   Control spotify on linux via DBus
//
// Dependencies:
//   dbus: ~0.2
//   pidof: ~1.0
//   properties: ~1.2
//   q: ~1.0
//
// Configuration:
//   None
//
//// Commands:
//   hubot sp pause
//   hubot sp play
//   hubot sp next
//   hubot sp prev
//   hubot sp open <URI>
//   hubot sp radio <URI>
//
//  Notes:
//    Hubot should be running on the same machine and user as the spotify process
//
// Author:
//   Eluinhost

var InterfaceFetcher = require('./spotify-interface-fetcher');

module.exports = function(robot) {

    var interfaceFetcher = new InterfaceFetcher('spotify', 'org.mpris.MediaPlayer2.spotify', '/org/mpris/MediaPlayer2', 'org.mpris.MediaPlayer2.Player');

    robot.respond(/sp pause$/i, function(msg) {
        interfaceFetcher.getInterface().then(
            function(iface) {
                iface.Pause();
                msg.send(":pause:");
            },
            function(err) {
                msg.send("Error: " + err);
            }
        );
    });

    robot.respond(/sp play$/i, function(msg) {
        interfaceFetcher.getInterface().then(
            function(iface) {
                iface.PlayPause();
                msg.send(":playpause:");
            },
            function(err) {
                msg.send("Error: " + err);
            }
        );
    });

    robot.respond(/sp next$/i, function(msg) {
        interfaceFetcher.getInterface().then(
            function(iface) {
                iface.Next();
                msg.send(":next:");
            },
            function(err) {
                msg.send("Error: " + err);
            }
        );
    });

    robot.respond(/sp prev$/i, function(msg) {
        interfaceFetcher.getInterface().then(
            function(iface) {
                iface.Previous();
                iface.Previous();
                msg.send(":previous");
            },
            function(err) {
                msg.send("Error: " + err);
            }
        );
    });

    robot.respond(/sp open (.*?)$/i, function(msg) {
        interfaceFetcher.getInterface().then(
            function(iface) {
                iface.OpenUri(msg.match[1]);
            },
            function(err) {
                msg.send("Error: " + err);
            }
        );
    });

    robot.respond(/sp radio (.*?)$/i, function(msg) {
        var seed = msg.match[1];
        var seedArray = seed.split(":");
        var uriLength = seedArray.length;
        if ((uriLength > 1) && (seedArray[0] === "spotify")) {
            var identifier = seedArray[uriLength - 1];
            var type = seedArray[uriLength - 2];
            var link = "spotify:app:radio:" + type + ":" + identifier;
            interfaceFetcher.getInterface().then(
                function(iface) {
                    iface.OpenUri(link);
                    msg.send("Started the :radio:");
                },
                function(err) {
                    msg.send("Error: " + err);
                }
            );
        } else {
            msg.send('Not a valid spotify URL, must start with `spotify:`');
        }
    });
};



@@ -878,17 +878,17 @@
 end(
-%22
+'
 :pause:
-%22
+'
 );%0A 
@@ -950,33 +950,33 @@
    msg.send(
-%22
+'
 Error: 
-%22
+'
  + err);%0A   
@@ -1189,17 +1189,17 @@
 sg.send(
-%22
+'
 :playpau
@@ -1201,17 +1201,17 @@
 aypause:
-%22
+'
 );%0A     
@@ -1269,33 +1269,33 @@
    msg.send(
-%22
+'
 Error: 
-%22
+'
  + err);%0A   
@@ -1507,16 +1507,16 @@
 end(
-%22
+'
 :next:
-%22
+'
 );%0A 
@@ -1578,33 +1578,33 @@
    msg.send(
-%22
+'
 Error: 
-%22
+'
  + err);%0A   
@@ -1850,17 +1850,17 @@
 sg.send(
-%22
+'
 :previou
@@ -1860,17 +1860,18 @@
 previous
-%22
+:'
 );%0A     
@@ -1929,33 +1929,33 @@
    msg.send(
-%22
+'
 Error: 
-%22
+'
  + err);%0A   
@@ -2223,33 +2223,33 @@
    msg.send(
-%22
+'
 Error: 
-%22
+'
  + err);%0A   
@@ -2401,19 +2401,19 @@
 d.split(
-%22:%22
+':'
 );%0A     
@@ -2502,17 +2502,17 @@
 === 
-%22
+'
 spotify
-%22
+'
 )) %7B
@@ -2639,17 +2639,17 @@
  link = 
-%22
+'
 spotify:
@@ -2658,17 +2658,17 @@
 p:radio:
-%22
+'
  + type 
@@ -2673,11 +2673,11 @@
 e + 
-%22:%22
+':'
  + i
@@ -2841,17 +2841,17 @@
 sg.send(
-%22
+'
 Started 
@@ -2861,17 +2861,17 @@
  :radio:
-%22
+'
 );%0A     
@@ -2949,17 +2949,17 @@
 end(
-%22
+'
 Error: 
-%22
+'
  + e

use single quotes for all strings