package nl.wehkamp.prodconf.ui.web.components;

import wicket.markup.html.link.PopupSettings;

/**
 * Adds extra functions to the popup. Some of it might be javascript 1.1 only!
 */
public class PopupSpecification extends PopupSettings
{
	private String jsVariableName = null;

	private String jsRegistrationFunction = null;

	private String jsPopupCondition = null;

	private boolean focus = false;

	/**
	 * Construct.
	 */
	public PopupSpecification()
	{
		super();
	}

	/**
	 * Construct.
	 * @param hasScrollBars
	 * @param hasLocationbar
	 * @param isResizable
	 * @param hasStatusBar
	 * @param hasToolbar
	 */
	public PopupSpecification(boolean hasScrollBars, boolean hasLocationbar, boolean isResizable,
			boolean hasStatusBar, boolean hasToolbar)
	{
		super(hasScrollBars, hasLocationbar, isResizable, hasStatusBar, hasToolbar);
	}

	/**
	 * Gets the function that will assign this popup to a (global) variable. e.g. register
	 * will result in something like onClick="register(window.open(...)); ...".
	 * @return jsRegistrationFunction
	 */
	public String getJsRegistrationFunction()
	{
		return jsRegistrationFunction;
	}

	/**
	 * Sets jsRegistrationFunction.
	 * @param jsRegistrationFunction jsRegistrationFunction
	 */
	public void setJsRegistrationFunction(String jsRegistrationFunction)
	{
		this.jsRegistrationFunction = jsRegistrationFunction;
	}

	/**
	 * Gets the name of the variable that will be used to register this popup. e.g.
	 * myPopup will result in something like onClick="myPopup=window.open(...); ...".
	 * @return jsVariableName
	 */
	public String getJsVariableName()
	{
		return jsVariableName;
	}

	/**
	 * Sets jsVariableName.
	 * @param jsVariableName jsVariableName
	 */
	public void setJsVariableName(String jsVariableName)
	{
		this.jsVariableName = jsVariableName;
	}

	/**
	 * Gets focus.
	 * @return focus
	 */
	public boolean isFocus()
	{
		return focus;
	}

	/**
	 * Wheter or not to give focus to a popup if it is opened again. Note only popups with
	 * the same name can be opened again. If JsVariableName is not set a local variable
	 * will be used.
	 * @param focus focus
	 */
	public void setFocus(boolean focus)
	{
		this.focus = focus;
	}

	/**
	 * @see PopupSettings#getPopupJavaScript()
	 */
	public String getPopupJavaScript()
	{
		String windowTitle = getWindowName();
		if (windowTitle == null)
			windowTitle = "";
		else
			windowTitle = windowTitle.replace('.', '_'); // Fix for IE bug.

		String target = getTarget();
		String condition = "";
		if (getJsPopupCondition() != null && !"".equals(getJsPopupCondition().trim()))
			condition = " && (" + getJsPopupCondition() + ")";
		StringBuffer script = new StringBuffer("if (!window.focus");
		script.append(condition).append(") return true; ");
		String variable = getJsVariableName();
		boolean local = false;
		if (variable == null && isFocus())
		{
			variable = "local_popup";
			local = true;
		}
		boolean function = false;
		if (getJsRegistrationFunction() != null && !"".equals(getJsRegistrationFunction().trim()))
		{
			function = true;
			if (variable == null)
			{
				variable = "local_popup";
				local = true;
			}
		}
		if (variable != null)
		{
			if (local)
				script.append("var ");
			script.append(variable).append("=");
		}
		script.append("window.open(" + target + ", '").append(windowTitle).append("', '");
		script.append("scrollbars=").append(getScrollBars() ? "yes" : "no");
		script.append(", location=").append(getLocationBar() ? "yes" : "no");
		script.append(", menubar=").append(getMenuBar() ? "yes" : "no");
		script.append(", resizable=").append(isResizable() ? "yes" : "no");
		script.append(", status=").append(getStatusBar() ? "yes" : "no");
		script.append(", toolbar=").append(getToolbar() ? "yes" : "no");

		if (getWidth() != -1)
		{
			script.append(", width=").append(getWidth());
		}

		if (getHeight() != -1)
		{
			script.append(", height=").append(getHeight());
		}

		if (getLeft() != -1)
		{
			script.append(", left=").append(getLeft());
		}

		if (getTop() != -1)
		{
			script.append(", top=").append(getTop());
		}

		script.append("'); ");
		if (function)
			script.append(getJsRegistrationFunction()).append("(").append(variable).append(")")
					.append("; ");
		if (isFocus())
			script.append(variable).append(".focus();");
		script.append(" return false;");

		return script.toString();
	}

	/**
	 * Gets jsPopupCondition.
	 * @return jsPopupCondition
	 */
	public String getJsPopupCondition()
	{
		return jsPopupCondition;
	}

	/**
	 * Sets a condition that will be checked before the popup is opened. if the check
	 * fails no popup will be opened. The check can either be a function like check() or a
	 * boolean expression like popups < 5.
	 * @param jsPopupCondition jsPopupCondition
	 */
	public void setJsPopupCondition(String jsPopupCondition)
	{
		this.jsPopupCondition = jsPopupCondition;
	}
}
