/**
 * 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.Widgets;

import java.awt.Window;
import java.util.Map;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import org.lyra.Lyra;

/**
 * While Java Swing provides static functions to create advanced dialogs for
 * a myriad of purposes creating all the elements necessary may not always be
 * consistent if not centrally controlled.   This class generates language 
 * controlled dialogs for our use.
 * <p>
 * @author Martin Foster
 * @version 1.0.4, %Revision, 147%, %LastChangedDate, 06/11/11 4:57PM%
 */
public class RapidDialog {

	// Language element
	private Map<String,String> Lang;
	
	// Options button
	String[] Options;
	
	/**
	 * Basic constructor which will establish the elements necessary for
	 * the dialogs to be generated using less code.
	 * <p>
	 * @param lang Map of Strings containing language elements.
	 */
	public RapidDialog(Map<String,String> lang) {
		
		// Pass Up
		this.Lang = lang;
		
		/*
		 * The reason why we generate our own array instead of making use of
		 * the built-in Java version is due to the fact that it will maintain
 		 * our language capability and maintain consistency.
		 */
		Options = new String[1];
		Options[0] = Lang.get("Dialog.Confirm");
	}

	// End of Constructor Class
	
	
	/**
	 * This method actually creates the dialogs.  While the public methods form the
	 * required elements and pass on for the actual display.
	 * <p>
	 * @param parent Parent window to block.
	 * @param icon Icon to display.
	 * @param title Title of the dialog.
	 * @param message Message to display.
	 */
	private void createDialog(Window parent, ImageIcon icon, String title, String message) {

		// Create notice dialog
		JOptionPane.showOptionDialog(parent,
			    message,
			    title,
			    JOptionPane.OK_OPTION,
			    JOptionPane.WARNING_MESSAGE,
			    icon,
			    Options,
			    Options[0]
		  );
	}
	// End of createDialog Method
	
	
	/**
	 * Generates a Notice Dialog used in various components of the site.
	 * <p>
	 * @param parent Parent Window component.
	 * @param message Message to Display.
	 */
	public void noticeDialog(Window parent, String message) {
		
		// Generate Icon
		ImageIcon icon = new ImageIcon(
				getClass().getResource(
						Lyra.getIconByMap("Prompt-Notice")));

		// Create notice dialog
		createDialog(parent, icon, Lang.get("Dialog.Notice"), message);
	}
	// End of noticeDialog Method
	
	
	/**
	 * Generates a Successful Operation Dialog used in various components of the site.
	 * <p>
	 * @param parent Parent Window component.
	 * @param message Message to Display.
	 */
	public void noticePass(Window parent, String message) {
		
		// Generate Icon
		ImageIcon icon = new ImageIcon(
				getClass().getResource(
						Lyra.getIconByMap("Prompt-Pass")));

		// Create notice dialog
		createDialog(parent, icon, Lang.get("Dialog.Pass"), message);
	}
	// End of noticePass Method
	
	
	/**
	 * Generates a Failed Operation Dialog used in various components of the site.
	 * <p>
	 * @param parent Parent Window component.
	 * @param message Message to Display.
	 */
	public void noticeFail(Window parent, String message) {

		// Generate Icon
		ImageIcon icon = new ImageIcon(
				getClass().getResource(
						Lyra.getIconByMap("Prompt-Fail")));

		// Create notice dialog
		createDialog(parent, icon, Lang.get("Dialog.Fail"), message);
	}
	// End of noticeFail Method
	
	
	/**
	 * Creates more complex dialogs for use in the application.  This permits
	 * an array to be passed to the dialog and used to extract a reply from
	 * the user to determine flow.
	 * <p>
	 * @param parent Parent window to block.
	 * @param icon Icon to display.
	 * @param title Title of the dialog.
	 * @param message Message to display.
	 * @param options Array of strings containing possible choices.
	 * @return String value that was accepted.
	 */
	private String createComplex(Window parent, ImageIcon icon, String title, String message, String... options) {
		
		// Create notice dialog and extract answer
		int reply = JOptionPane.showOptionDialog(parent,
			    message,
			    title,
			    JOptionPane.OK_OPTION,
			    JOptionPane.WARNING_MESSAGE,
			    icon,
			    options,
			    options[0]
		  );
		
		// User selects a value or gets the default
		String pass = (reply > -1)
			? options[reply]
			: options[0];
			
		// Return the answer
		return pass;
	}
	// End of createComplex Method
	
	
	/**
	 * This notice is used exclusively when the application has failed to
	 * connect to the host and cannot continue from that point forward.  It
	 * will prompt the user to exit the application or create a new entry.
	 * <p>
	 * @param parent Parent Window component.
	 * @return Boolean indicating true to create or false to exit.
	 */
	public boolean noticeHostFail(Window parent) {
		
		// Generate Icon
		ImageIcon icon = new ImageIcon(
				getClass().getResource(
						Lyra.getIconByMap("Prompt-Fail")));

		// Create a new instance and extract answer
		String reply = createComplex(
			parent, 
			icon, 
			Lang.get("Dialog.Fail"), 
			Lang.get("Dialog.Host"),
			Lang.get("Dialog.Close"),
			Lang.get("Dialog.Create")
		  );
		
		// If the user wants to continue pass on true
		if (reply.equals(Lang.get("Dialog.Create")))
			return true;
		
		// Pass on false otherwise
		return false;
	}
	// End of noticeHostFail Method
	
	
	/**
	 * This notice is used exclusively when the application has failed to
	 * connect to the host and cannot continue from that point forward.  It
	 * will prompt the user to exit the application or create a new entry.
	 * <p>
	 * @param parent Parent Window component.
	 * @return Boolean indicating true to create or false to exit.
	 */
	public boolean noticeAlbumArt(Window parent) {
		
		// Generate Icon
		ImageIcon icon = new ImageIcon(
				getClass().getResource(
						Lyra.getIconByMap("Prompt-Question")));

		// Create a new instance and extract answer
		String reply =  createComplex(
			parent, 
			icon, 
			Lang.get("Dialog.Album.Art.Title"), 
			Lang.get("Dialog.Album.Art.Message"),
			Lang.get("Dialog.Album.Art.Pair"),
			Lang.get("Dialog.Album.Art.Global")
		  );
		
		// If the user wants to continue pass on true
		if (reply.equals(Lang.get("Dialog.Album.Art.Global")))
			return true;
		
		// Pass on false otherwise
		return false;
	}
	// End of noticeAlbumArt
	
}