
import org.apache.tools.ant.Task;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.BuildListener;
import org.apache.tools.ant.BuildEvent;
import org.apache.tools.ant.Project;

import java.io.IOException;
import java.io.File;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;

/**
 * This is an example of an AntTask that makes of use of the AntSoundPlayer.
 *
 * There are four attributes to be set:
 *
 * <code>source</code>: the location of the audio file to be played
 * <code>background</code>: "true" will play the sound file continuously until the end of the build
 * <code>duration</code>: play the sound file continuously until "duration" milliseconds has expired
 * <code>loops</code>: the number of times the sound file should be played until stopped
 *
 *
 * I have only tested this with .WAV and .AIFF sound file formats. Both seem to work fine.
 *
 * plans for the future:
 * - specifiy differrent sounds to be played during different build events (A bad sound if the build fails!)
 * - use the midi api to define a sounds (or drum beat etc) in xml and have Ant play them back
 *
 * @author Nick Pellow
 * @version $Revision$, $Date$
 */

public class SoundTask extends Task {

    private SoundTask.Success  success = null;
    private SoundTask.Fail fail = null;

    public SoundTask.Success createSuccess() {
        success = new SoundTask.Success();
        return success;
    }

    public SoundTask.Fail createFail() {
        fail = new SoundTask.Fail();
        return fail;
     }

    public SoundTask() {
    }

    public void init(){
    }

    public void execute() throws BuildException {

        if ( success == null ) {
            throw new BuildException("No nested success element found.");
        }

        AntSoundPlayer soundPlayer = new AntSoundPlayer();
        soundPlayer.addBuildSuccesfulSound(success.getSource(), success.getLoops(), success.getDuration());
        if (fail != null) {
            soundPlayer.addBuildFailedSound(fail.getSource(), fail.getLoops(), fail.getDuration());
        }
        getProject().addBuildListener(soundPlayer);
        // kick off a new thread for the sound player
        new Thread(soundPlayer).start();
    }

    /**
     * A static class to be extended by any BuildAlert's that require the output of sound.
     */
    public static class BuildAlert {
        private File file = null;
        private int loops = 1;
        private Long duration = null;

        /**
         * Sets the duration in milliseconds the file should be played.
         */
        public void setDuration(Long duration) {
            this.duration = duration;
        }

        /**
         * Sets the location of the file to get the audio.
         *
         * @param fileName the location of the audio file
         */
        public void setSource(File file) {
            this.file = file;
        }

        /**
         * This attribute sets the number of times the source file should
         * be played.
         *
         * @param loops the number of loops to play the source file
         */
        public void setLoops(int loops) {
            this.loops = loops;
        }

        /**
         * Gets the duration in milliseconds the file should be played.
         */
        public Long getDuration() {
            return this.duration;
        }

        /**
         * Gets the location of the file to get the audio.
         */
        public File getSource() {
            return this.file;
        }

        /**
         * This attribute sets the number of times the source file should
         * be played.
         *
         * @return the number of loops to play the source file
         */
        public int getLoops() {
            return this.loops;
        }
    }

    /**
     * To be played when a build fails
     */
    public static class Fail extends SoundTask.BuildAlert {
    }
    /**
     * To be played when a build is succesful.
     */
    public static class Success extends SoundTask.BuildAlert {
    }
}

