const { ipcMain } = require('electron');
const mpris = require('mpris-service');

function executeMediaKey(win, key) {
  win.webContents.executeJavaScript(`
    window.electronConnector.emit('${key}')
  `);
}

module.exports = (win, player) => {

  const mprisPlayer = mpris({
    name: 'headset',
    identity: 'Headset',
    canRaise: true,
    supportedInterfaces: ['player'],
    desktopEntry: 'headset',
  });

  mprisPlayer.playbackStatus = 'Stopped'
  mprisPlayer.rate = 1+1e-15  //to avoid storing 1 as byte (nodejs dbus bug)
  mprisPlayer.minimumRate = 1+1e-15
  mprisPlayer.maximumRate = 1+1e-15
  mprisPlayer.volume = 0.75

  mprisPlayer.on('raise', () => {
    win.show();
  });

  mprisPlayer.on('quit', () => {
    exec('kill -9 $(pgrep headset) &> /dev/null')
  });

  mprisPlayer.on('rate', () => {
    mprisPlayer.rate = 1+1e-15
  });

  mprisPlayer.on('playpause', () => {
    if (mprisPlayer.playbackStatus == 'Playing' ||
        mprisPlayer.playbackStatus == 'Paused') {
      executeMediaKey(win, 'play-pause');
    }
  });

  mprisPlayer.on('play', () => {
    if (mprisPlayer.playbackStatus == 'Paused') {
      executeMediaKey(win, 'play-pause');
    }
  });

  mprisPlayer.on('pause', () => {
    if (mprisPlayer.playbackStatus == 'Playing') {
      executeMediaKey(win, 'play-pause');
    }
  });

  mprisPlayer.on('next', () => {
    if (mprisPlayer.playbackStatus == 'Playing' ||
        mprisPlayer.playbackStatus == 'Paused') {
          executeMediaKey(win, 'play-next');
    }
  });

  mprisPlayer.on('previous', () => {
    if (mprisPlayer.playbackStatus == 'Playing' ||
        mprisPlayer.playbackStatus == 'Paused') {
          executeMediaKey(win, 'play-previous')
    }
  });

  mprisPlayer.on('volume', (volume) => {
    if (volume > 1) { volume = 1 }
    if (volume < 0) { volume = 0 }
    player.webContents.send('win2Player', ['setVolume', volume*100]);
    mprisPlayer.volume = volume + 1e-15;
  });

  mprisPlayer.on('seek', (args) => {
    if (mprisPlayer.playbackStatus == 'Playing' ||
        mprisPlayer.playbackStatus == 'Paused') {
      player.webContents.send('win2Player', ['seekTo', args.position/1e6]);
    }
  });

  ipcMain.on('win2Player', (e, args) => {
    if (args[0] == 'playVideo') {
      mprisPlayer.playbackStatus = 'Playing';
    } else if (args[0] == 'pauseVideo') {
      mprisPlayer.playbackStatus = 'Paused';
    } else if (args[0] == 'setVolume') {
      mprisPlayer.volume = args[1]/100 + 1e-15;
    } else if (args[0] == 'trackInfo') {
      mprisPlayer.metadata = {
        'xesam:artist': [ args[1]['artist'] ],
        'xesam:title': args[1]['title'],
        'xesam:url': "https://www.youtube.com/watch?v=" + args[1]['id'],
        'mpris:artUrl': args[1]['thumbnail'],
        'mpris:length': args[1]['duration'] * 1e6 //in microseconds
      };
    } else if (args[0] == 'seekTo') {
      const delta = Math.round(args[1]*1e6) - mprisPlayer.position;
      mprisPlayer.seeked(delta);
    }
  });

  ipcMain.on('player2Win', (e, args) => {
    if (args[0] == 'currentTime') {
      // int64 in microseconds. nodejs dbus doesn't support int64 (bug)
      mprisPlayer.position = Math.round(args[1] * 1e6)
    }
  });

}

@@ -1944,293 +1944,81 @@
 %0A%0A  
-mprisPlayer.on('seek', (args) =%3E %7B%0A    if (mprisPlayer.playbackStatus == 'Playing' %7C%7C%0A        mprisPlayer.playbackStatus == 'Paused') %7B%0A      player.webContents.send('win2Player', %5B'seekTo', args.position/1e6%5D);%0A    %7D%0A  %7D);%0A%0A  ipcMain.on('win2Player', (e, args) =%3E %7B%0A    if (args%5B0%5D ==
+ipcMain.on('win2Player', (e, args) =%3E %7B%0A    switch (args%5B0%5D) %7B%0A      case
  'pl
@@ -2021,28 +2021,28 @@
  'playVideo'
-) %7B%0A
+:%0A  
       mprisP
@@ -2077,35 +2077,34 @@
 ing'
-;
 %0A    
-%7D else if (args%5B0%5D ==
+    break;%0A      case
  'pa
@@ -2112,20 +2112,20 @@
 seVideo'
-) %7B%0A
+:%0A  
       mp
@@ -2163,35 +2163,34 @@
 sed'
-;
 %0A    
-%7D else if (args%5B0%5D ==
+    break;%0A      case
  'se
@@ -2197,20 +2197,20 @@
 tVolume'
-) %7B%0A
+:%0A  
       mp
@@ -2251,57 +2251,92 @@
 e-15
-;
 %0A    
-%7D else if (args%5B0%5D == 'trackInfo') %7B
+    break;%0A      case 'trackInfo':%0A        mprisPlayer.canSeek = false
 %0A      
+  
 mpri
@@ -2356,16 +2356,18 @@
 ata = %7B%0A
+  
         
@@ -2409,24 +2409,26 @@
  %5D,%0A        
+  
 'xesam:title
@@ -2452,24 +2452,26 @@
 '%5D,%0A        
+ 
+ 
 'xesam:url':
@@ -2527,24 +2527,26 @@
 '%5D,%0A        
+  
 'mpris:artUr
@@ -2575,24 +2575,26 @@
 '%5D,%0A        
+ 
+ 
 'mpris:lengt
@@ -2651,36 +2651,37 @@
     
+  
 %7D
-;
 %0A    
-%7D else if (args%5B0%5D ==
+    break;%0A      case
  'se
@@ -2685,20 +2685,20 @@
 'seekTo'
-) %7B%0A
+:%0A  
       co
@@ -2759,24 +2759,26 @@
 tion;%0A      
+  
 mprisPlayer.
@@ -2792,16 +2792,61 @@
 delta);%0A
+        break;%0A      default:%0A        break;%0A
     %7D%0A  

Remove mpris seek and change if statements to switch/case