/**
 * Copyright (c) 2011, Martin Foster <martin@ethereal-realms.org>
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice, 
 *       this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright 
 *       notice, this list of conditions and the following disclaimer in the 
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the Ethereal-Realms.org nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.lyra.Host;

import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;

/**
 * Event encapsulating an update from the host element.  By default the only
 * element that will trigger this element will come from the <i>Updater</i> 
 * class.
 * <p>
 * @author Martin Foster
 * @version 1.0.1, %Revision, 99%, %LastChangedDate, 17/03/11 8:17PM%
 * @see org.lyra.Host.Updater
 * @see org.lyra.Host.UpdateListener
 */
public class UpdateEvent {
	
	// Storage of command elements
	private Set<String> Commands;
	
	// Source element
	private Object Source;
	
	// Time stamp
	private Long Timestamp;
	
	/**
	 * Primary constructor for the event generated.   Takes the source of the
	 * caller and all of the updates that have happened at server level.
	 * <p>
	 * @param source Source object triggering the event.
	 * @param updates String commands representing updates.
	 */
	public UpdateEvent(Object source, String...updates) {
		
		// Pass up elements for later use
		Commands  = new TreeSet<String>(Arrays.asList(updates));
		Source    = source;
		
		// Calculate the time
		Timestamp = System.currentTimeMillis();
	}
	// End of Constructor Class
	
	
	/**
	 * The object on which the Event initially occurred. 
	 * <p>
	 * @return The object on which the Event initially occurred.
	 */
	public Object getSource() {
		
		// Straight forward passage
		return Source;
	}
	// End of getSource Method

	
	/**
	 * Has the Library been triggered. The song database has been modified 
	 * after update. 
	 * <p>
	 * @return Boolean indicating trigger.
	 */
	public boolean isLibrary() {
		
		// Direct pass through
		return Commands.contains(Terminus.UPDATES.get("Library"));
	}
	// End of isLibrary Method
	
	
	/**
	 * Has a library update process been triggered.  THis differs from a
	 * Library trigger since this implies the process is still ongoing.
	 * Typically to be avoided.
	 * <p>
	 * @return Boolean indicating trigger.
	 */
	public boolean isUpdate() {
		
		// Direct pass through
		return Commands.contains(Terminus.UPDATES.get("Update"));
	}
	// End of isUpdate Method
	
	
	/**
	 * Has the Playlist been triggered. A stored playlist has been modified, 
	 * renamed, created or deleted. 
	 * <p>
	 * @return Boolean indicating trigger.
	 */
	public boolean isPlaylist() {
		
		// Direct pass through
		return Commands.contains(Terminus.UPDATES.get("Playlist"));
	}
	// End of isPlaylist Method

	
	/**
	 * Has the Current been triggered. The current playlist has been modified. 
	 * <p>
	 * @return Boolean indicating trigger.
	 */
	public boolean isCurrent() {
		
		// Direct pass through
		return Commands.contains(Terminus.UPDATES.get("Current"));
	}
	// End of isCurrent Method

	
	/**
	 * Has the Database been triggered. The database has received an update.
	 * <p>
	 * @return Boolean indicating trigger.
	 */
	public boolean isDatabase() {
		
		// Direct pass through
		return Commands.contains(Terminus.UPDATES.get("Database"));
	}
	// End of isDatabase Method
	
	/**
	 * Has the Player been triggered. The player has been started, stopped or
	 * seeked. 
	 * <p>
	 * @return Boolean indicating trigger.
	 */
	public boolean isPlayer() {
		
		// Direct pass through
		return Commands.contains(Terminus.UPDATES.get("Player"));
	}
	// End of isPlayer Method

	
	/**
	 * Has the Mixer been triggered.  The volume has been changed. 
	 * <p>
	 * @return Boolean indicating trigger.
	 */
	public boolean isMixer() {
		
		// Direct pass through
		return Commands.contains(Terminus.UPDATES.get("Mixer"));
	}
	// End of isMixer Method
	
	
	/**
	 * Has the Output been triggered. An audio output has been enabled or 
	 * disabled. 
	 * <p>
	 * @return Boolean indicating trigger.
	 */
	public boolean isOutput() {
		
		// Direct pass through
		return Commands.contains(Terminus.UPDATES.get("Output"));
	}
	// End of isOutput Method

	
	/**
	 * Has the Options been triggered. Options like <i>repeat</i>, <i>random</i>,
	 * <i>crossfade</i>, <i>replay</i> and <i>gain</i>. 
	 * <p>
	 * @return Boolean indicating trigger.
	 */
	public boolean isOptions() {
		
		// Direct pass through
		return Commands.contains(Terminus.UPDATES.get("Options"));
	}
	// End of isOptions Method


	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		
		/*
		 * Simply returns a string containing all core elements of this event
		 * and wherever possible makes use of the built-in toString capability
		 * of the instances to make it work.
		 */
		return Source.toString() + "->" 
		     + Long.toString(Timestamp) + "->"
		     + Commands.toString();
	}
	// End of getString Method
}
