/**
 * Makes sounds, mostly verbal error messages.
 */
package com.mindprod.vercheck;

import java.applet.Applet;
import java.applet.AudioClip;
import java.net.URL;

/**
 * Plays a variety of sounds
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 Created by IntelliJ IDEA.
 */
public enum Sound
    {
    // durations calculated by GoldWave.
        BAD_DATE( "baddate.au", 6278 ),
        BAD_URL( "badurl.au", 6203 ),
        CLICK( "click.au", 64 ),
        FUTURE_DATE( "futuredate.au", 2412 ),
        INVALID_REGEX( "invalidregex.au", 1042 ),
        NEW_VERSION( "newversion.au", 909 ),
        UNABLE_TO_CONNECT( "unabletoconnect.au", 1161 );

// ------------------------------ FIELDS ------------------------------

    /**
     * time safe to make next sound
     */
    @SuppressWarnings( { "WeakerAccess" } )
    static long nextSound;

    /**
     * audioclip to play this sound
     */
    private AudioClip clip;

    /**
     * name of this sound file in the jar
     */
    private final String resource;

    /**
     * duration of the sound in milliseconds
     */
    private final int duration;
// -------------------------- PUBLIC STATIC METHODS --------------------------

    /**
     * Convert resources into AudioClips, ready to play.
     *
     * @param applet apply that will be playing the sounds.
     */
    public static void init( Applet applet )
        {
        try
            {
            if ( applet.getAppletContext() == null )
                {
                // we don't have Audio clip support.  We will have to be silent.
                return;
                }
            }
        catch ( NullPointerException e )
            {
            // we don't have Audio clip support.  We will have to be silent.
            return;
            }
        for ( Sound sound : values() )
            {
            final URL url = VerCheck.class.getResource( "sound/" + sound.resource );
            sound.clip = applet.getAudioClip( url );
            }
        }

    // -------------------------- PUBLIC INSTANCE  METHODS --------------------------
    /**
     * play sound matching this enum
     */
    @SuppressWarnings( { "EmptyCatchBlock" } )
    public void play()
        {
        if ( clip != null )
            {
            // don't start a new sound until the last one has had a chance to play.
            // We can't determine its status.
            final long now = System.currentTimeMillis();
            if ( now < nextSound )
                {
                try
                    {
                    Thread.sleep( nextSound - now );
                    }
                catch ( InterruptedException e )
                    {
                    }
                }
            clip.play();
            nextSound = now + duration;
            Thread.yield();
            }
        }

// --------------------------- CONSTRUCTORS ---------------------------

    /**
     * constructor
     *
     * @param resource name of resource sound file
     * @param duration time in ms to play the sound.
     */
    Sound( String resource, int duration )
        {
        this.resource = resource;
        this.duration = duration;
        }
    }