package it.playfellas.hicaptain.sounds;

import android.content.Context;
import android.media.MediaPlayer;

import it.playfellas.hicaptain.R;

/**
 * Created by affo on 21/09/15.
 */
public class Baraldi {
    private static MediaPlayer player = null;

    private static MediaPlayer getPlayer(Context ctxt, int soundID) {
        shutUp();
        player = MediaPlayer.create(ctxt, soundID);
        return player;
    }

    private static MediaPlayer setListener(MediaPlayer p, final Runnable onComplete) {
        if (onComplete != null) {
            p.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    onComplete.run();
                }
            });
        }
        return p;
    }

    public static void shutUp() {
        if (player != null) {
            player.stop();
            player.release();
        }
        player = null;
    }

    /**
     * @param ctxt
     * @param onComplete can be `null` if no callback is required
     */
    public static void welcome(Context ctxt, final Runnable onComplete) {
        setListener(getPlayer(ctxt, R.raw.welcome), onComplete).start();
    }

    public static void askHelp(Context ctxt, final Runnable onComplete) {
        setListener(getPlayer(ctxt, R.raw.help), onComplete).start();
    }

    public static void greet(Context ctxt, final Runnable onComplete) {
        setListener(getPlayer(ctxt, R.raw.greetings), onComplete).start();
    }
}

@@ -244,16 +244,55 @@
  = null;
+%0A    private static Runnable cb = null;
 %0A%0A    pr
@@ -351,16 +351,43 @@
  soundID
+, final Runnable onComplete
 ) %7B%0A    
@@ -397,16 +397,21 @@
  shutUp(
+false
 );%0A     
@@ -469,103 +469,12 @@
     
-return player;%0A    %7D%0A%0A    private static MediaPlayer setListener(MediaPlayer p, final Runnable 
+if (
 onCo
@@ -471,32 +471,40 @@
   if (onComplete
+ != null
 ) %7B%0A        if (
@@ -491,36 +491,41 @@
 null) %7B%0A        
-if (
+    cb = 
 onComplete != nu
@@ -514,35 +514,25 @@
 = onComplete
- != null) %7B
+;
 %0A           
@@ -533,16 +533,21 @@
        p
+layer
 .setOnCo
@@ -787,16 +787,21 @@
 return p
+layer
 ;%0A    %7D%0A
@@ -831,16 +831,30 @@
  shutUp(
+boolean runCBs
 ) %7B%0A    
@@ -932,24 +932,104 @@
 .release();%0A
+            if (runCBs && cb != null) %7B%0A                cb.run();%0A            %7D%0A
         %7D%0A  
@@ -1045,24 +1045,43 @@
 yer = null;%0A
+        cb = null;%0A
     %7D%0A%0A    /
@@ -1254,36 +1254,24 @@
 ) %7B%0A        
-setListener(
 getPlayer(ct
@@ -1287,17 +1287,16 @@
 .welcome
-)
 , onComp
@@ -1395,36 +1395,24 @@
 ) %7B%0A        
-setListener(
 getPlayer(ct
@@ -1425,17 +1425,16 @@
 raw.help
-)
 , onComp
@@ -1539,20 +1539,8 @@
     
-setListener(
 getP
@@ -1566,17 +1566,16 @@
 reetings
-)
 , onComp

enable callbacks run on shutup