package org.xqqunit.plugin.ant;

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

/**
 * Base task containing general attributes used by XqqUnit Ant tasks. 
 */
public abstract class BaseTask extends Task {
	
	/**
	 * Message priority of "failure". 
	 */
	public static final int MSG_FAIL = Project.MSG_ERR-1;
	/**
	 * Message priority of "error". 
	 */
	public static final int MSG_ERR = Project.MSG_ERR;
	/**
	 * Message priority of "warning". 
	 */
	public static final int MSG_WARN = Project.MSG_WARN;
	/**
	 * Message priority of "info". 
	 */
	public static final int MSG_INFO = Project.MSG_INFO;
	/**
	 * Message priority of "debug". 
	 */
	public static final int MSG_DEBUG = Project.MSG_DEBUG;

	/**
	 * Set task default values
	 */
	protected void setBaseDefaultValues() {
		printSummary    = false;
		haltOnError     = false;
		haltOnFailure   = false;
	}

	/**
	 * Copy task values from other instance of BaseTask
	 */
	protected void setBaseDefaultValues(BaseTask baseTask) {
		printSummary    = baseTask.isPrintSummary();
		haltOnError     = baseTask.isHaltOnError();
		haltOnFailure   = baseTask.isHaltOnFailure();
	}
	
	private boolean printSummary;

	/**
	 * Set Ant task attribute "haltonerror"
	 * @param printSummaryValue Ant task attribute "haltonerror"
	 */
	public void setPrintSummary(String printSummaryValue) {
		printSummary = getStringAttributeAsBoolean(printSummaryValue);
	}
	
	/**
	 * Get Ant task attribute "printsummary" as boolean
	 * @return Ant task attribute value as boolean
	 */
	public boolean isPrintSummary() {
		return printSummary;
	}
	
	private boolean haltOnError;
	
	/**
	 * Set Ant task attribute "haltonerror"
	 * @param haltOnErrorValue Ant task attribute "haltonerror"
	 */
	public void setHaltOnError(String haltOnErrorValue) {
		haltOnError = getStringAttributeAsBoolean(haltOnErrorValue);
	}
	
	/**
	 * Get Ant task attribute "haltonerror" as boolean
	 * @return Ant task attribute value as boolean
	 */
	public boolean isHaltOnError() {
		return haltOnError;
	}
	
	private boolean haltOnFailure;
	
	/**
	 * Set Ant task attribute "haltonfailure"
	 * @param haltOnFailureValue Ant task attribute "haltonfailure"
	 */
	public void setHaltOnFailure(String haltOnFailureValue) {
		haltOnFailure = getStringAttributeAsBoolean(haltOnFailureValue);
	}
	
	/**
	 * Get Ant task attribute "haltonfailure" as boolean
	 * @return Ant task attribute value as boolean
	 */
	public boolean isHaltOnFailure() {
		return haltOnFailure;
	}
	
	/**
	 * Convert Ant task attribute string value to boolean
	 * @param value Ant task attribute string value
	 * @return boolean value of the string value; "yes", "on" and "true" is true otherwise false
	 */
	protected boolean getStringAttributeAsBoolean(String value) {
		if ("yes".equals(value)
				|| "on".equals(value)
				|| "true".equals(value)) {
			return true;
		}
		
		return false;
	}

	/**
	 * Do basic Ant logging via "xqqunit" task.
	 * @param message the Ant log message.
	 */
	public void log(String message) {
		log(message, BaseTask.MSG_INFO);
	}

	/**
	 * Do basic Ant logging via "xqqunit" task.
	 * @param message the Ant log message.
	 */
	public void log(String message, int msgLevel) {
		int messageLevel = msgLevel;
		if (messageLevel == BaseTask.MSG_FAIL) {
			messageLevel = BaseTask.MSG_ERR;
		}
		super.log(message, messageLevel);
		
		if (isHaltOnFailure() && msgLevel == BaseTask.MSG_FAIL) {
			throw new BuildException(message);
		}

		if (isHaltOnError() && msgLevel == BaseTask.MSG_ERR) {
			throw new BuildException(message);
		}
	}
}
