package sounddata;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
   
/**
 *This enum encapsulates all of the different wav files associated with the card matching game.
 * 
 * @author Brian Dennis
 * @version 3-27-2014
 */
public enum SoundEffect {
    
    /** chime. */
    CHIME("chime.wav"),  
    /** card flip. */
    FLIP("flip.wav"),
    /** applause. */
    APPLAUSE("applause.wav"),
    /** card shuffle. */
    SHUFFLE("shuffle.wav");
   
    /**
     * Nested class for specifying volume.
     * 
     * @author Brian Dennis
     * @version 3-27-2014
     */
    public static enum Volume {
        
       /** the volumes. */
       MUTE, LOW, MEDIUM, HIGH
    }
       
    /** setting default volume to low i think. */
    protected static Volume volume = Volume.LOW;
       
    /** Each sound effect has its own clip, loaded with its own sound file.*/
    private Clip myClip;
       
    /** 
     * Constructor to construct each element of the enum with its own sound file.
     * 
     * @param theSoundFileName file name
     */
    SoundEffect(final String theSoundFileName) {

        try {
            
            final AudioInputStream stream = AudioSystem.getAudioInputStream(
                                                    getClass().getResource(theSoundFileName));
            myClip = AudioSystem.getClip();
            myClip.open(stream);

        } catch (final UnsupportedAudioFileException e) {
            e.printStackTrace();
        } catch (final IOException e) {
            e.printStackTrace();
        } catch (final LineUnavailableException e) {
            e.printStackTrace();
        }
    }
       
   /** 
    * Play or Re-play the sound effect from the beginning, by rewinding.
    */
    public void play() {
        
        if (volume != Volume.MUTE && !myClip.isRunning()) {
    
            myClip.stop();   // Stop the player if it is still running
            myClip.setFramePosition(0); // rewind to the beginning
            myClip.start();     // Start playing
            
        } else if (myClip.isRunning()) {
            
            myClip.stop();   // Stop the player if it is still running
            myClip.setFramePosition(0); // rewind to the beginning
            myClip.start();
        } 
    }
       
   /** 
    * Optional static method to pre-load all the sound files. 
    */
    static void init() {
        values(); // calls the constructor for all the elements
    }
}