/*
 * Copyright (c) 2017. Vitus Lehner. UrbanLife+. Universität der Bundeswehr München.
 */

package org.sociotech.urbanlifeplus.microinforadiator.interfaces.audio.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sociotech.urbanlifeplus.microinforadiator.interfaces.audio.AudioInterface;
import org.springframework.stereotype.Component;

import javax.annotation.PreDestroy;
import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;

/**
 * @author vituslehner 26.08.17
 */
@Component
public class DefaultAudioInterface implements AudioInterface, LineListener {


    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    private boolean playCompleted;

    public DefaultAudioInterface() {
        this.playCompleted = true;
    }

    @Override
    public void playPositiveSound() {
        playSound("positive");
    }

    @Override
    public void playNegativeSound() {
        playSound("negative");
    }

    private void playSound(String name) {
        logger.debug("Playing sound {}.", name);
        waitForCompletion();
        logger.debug("Playing sound {} now.", name);

        try {
            File audioFile = new File(getClass().getResource("/media/sound_" + name + ".wav").toURI());

            AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);
            AudioFormat format = audioStream.getFormat();
            DataLine.Info info = new DataLine.Info(Clip.class, format);

            Clip clip = (Clip) AudioSystem.getLine(info);
            clip.addLineListener(this);
            clip.open(audioStream);
            clip.start();
            playCompleted = false;

            waitForCompletion();

            clip.close();

        } catch (UnsupportedAudioFileException e) {
            logger.error("The specified audio file is not supported.", e);
        } catch (LineUnavailableException e) {
            logger.error("Audio line for playing back is unavailable.", e);
        } catch (IOException e) {
            logger.error("Error playing the audio file.", e);
        } catch (URISyntaxException e) {
            logger.error("URI Syntax error.", e);
        }
    }

    private void waitForCompletion() {
        while (!playCompleted) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                logger.error("Thread sleep error.", e);
            }
        }
    }

    @Override
    public void update(LineEvent event) {
        LineEvent.Type type = event.getType();

        if (type == LineEvent.Type.START) {
            logger.debug("Sound started.");

        } else if (type == LineEvent.Type.STOP) {
            playCompleted = true;
            logger.debug("Sound stopped.");
        }

    }

    @PreDestroy
    private void disposeProcess() {
    }
}

@@ -427,29 +427,8 @@
 .*;%0A
-import java.io.File;%0A
 impo
@@ -467,30 +467,22 @@
 ava.
-net.URISyntaxException
+io.InputStream
 ;%0A%0A/
@@ -1184,20 +1184,27 @@
         
-File
+InputStream
  audioFi
@@ -1212,17 +1212,8 @@
 e = 
-new File(
 getC
@@ -1230,16 +1230,24 @@
 Resource
+AsStream
 (%22/media
@@ -1275,17 +1275,8 @@
 av%22)
-.toURI())
 ;%0A%0A 
@@ -2025,32 +2025,32 @@
 OException e) %7B%0A
+
             logg
@@ -2099,99 +2099,8 @@
 e);%0A
-        %7D catch (URISyntaxException e) %7B%0A            logger.error(%22URI Syntax error.%22, e);%0A
     

Fix audio sentiments bug