'use strict';

angular.module('mean.icu.data.notificationsservice', [])
    .service('NotificationsService', function($http, ApiUri, WarningsService, $state) {

        // audio file to play on push notification
        // audio source: https://notificationsounds.com/sound-effects/just-like-magic-506
        var audio = document.createElement('audio');
        audio.type = 'audio/ogg';
        audio.src = '/dist/icu/assets/audio/just-like-magic.mp3';

        if (Notification.permission !== "denied") {
            Notification.requestPermission(permission => {
            });
        }

        function notify(data) {
            console.log('New notification:', data);
            if(Notification.permission === 'denied') {
                console.log('Notification aborted - permission denied');
                return;
            }

            let { title, body } = parseNotification(data);
            let notification = new Notification(title, {
                body,
                icon: '/favicon.ico'
            });

            notification.onclick = function(event) {
                this.close();
                window.focus();
                $state.go(`main.${data.entityType}s.all.details.${window.config.defaultTab}`, {
                    entity: 'all',
                    id: data.entity._id,
                    nameFocused: true
                });
            }
            audio.play();
            setTimeout(notification.close.bind(notification), 4000);
        }

        function parseNotification(data) {
            let { entity, creator, updateField, current } = data;
            let title = entity.title;
            let body = '';
            switch(updateField) {
                case 'assign':
                    body = 'You were assigned by ' + creator.name;
                    break;
                case 'due':
                    body = 'Due date changed to ' + moment(current).format('DD/MM/YYYY');
                    break;
                case 'status':
                    body = 'Status changed to ' + current.toUpperCase();
                    break;
                case 'comment':
                    body = creator.name + ' commented: ' + current;
                    break;
            }
            return { title, body };
        }

        return {
            notify
        };
    });

@@ -166,38 +166,27 @@
     
-//
+let
  audio 
-file to play on push 
+= window.
 noti
@@ -193,16 +193,23 @@
 fication
+Audio;%0A
 %0A       
@@ -213,103 +213,80 @@
     
-// audio source: https://notificationsounds.com/sound-effects/just-like-magic-506%0A        var a
+if(!window.notificationAudio) %7B%0A            audio = window.notificationA
 udio
@@ -317,24 +317,28 @@
 t('audio');%0A
+    
         audi
@@ -359,16 +359,113 @@
 o/ogg';%0A
+%0A            // Taken from: https://notificationsounds.com/sound-effects/just-like-magic-506%0A    
         
@@ -522,16 +522,27 @@
 c.mp3';%0A
+        %7D%0A%0A
 %0A       
@@ -644,28 +644,16 @@
 ion =%3E %7B
-%0A           
  %7D);%0A   
@@ -696,59 +696,8 @@
 ) %7B%0A
-            console.log('New notification:', data);
 %0A   
@@ -919,16 +919,17 @@
 (data);%0A
+%0A
         
@@ -1416,33 +1416,199 @@
   %7D%0A
-            audio.play();
+%0A            // Start from beginning in case of consecutive when previous audio didn't yet finished%0A            audio.currentTime = 0;%0A%0A            audio.play();%0A%0A            // Hide after 4s
 %0A   

Fix restart sound if already playing