package ever.workflowRepresentation;

import java.util.Stack;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import ever.pipeline.Sentence;
/**
 * This class represents an XOR element. It can represent disjunctiv paths in a control flow. It contains a set of options
 * which are used to store the different paths
 * @author Pol Schumacher, Wirtschaftsinformatik, Institut fuer Informatik, Goethe Universitaet Frankfurt
 *
 */
public class Xor extends WorkflowElement {

	private Stack<Option> xor;
	private String name = "Xor";

	/**
	 * 
	 * @param parent element
	 * @param s sentence of which the element was extracted
	 */
	public Xor(WorkflowElement parent, Sentence s) {
		super(parent,s);
		xor = new Stack<Option>();
	}
/**
 * Add an option/path to xor
 * @param op
 */
	public void addOption(Option op) {
		xor.add(op);
	}
/**
 * Returns an option of the xor
 * @return
 */
	public Option popOption() {
		return xor.pop();
	}
/**
 * Returns name (no real use)
 * @return
 */
	public String getName() {
		return name;
	}

	
	/**
	 * Writes the task, its products and facet into the stream. It uses the
	 * xml-format which was defined.
	 * 
	 * @param writer
	 *            is the stream writer which is used to write
	 */
	public void exportWf2Xml(XMLStreamWriter writer) {
		try {
			writer.writeStartElement("rwfl:Node");
			writer.writeAttribute("type", "XOR");
			writer.writeAttribute("refID", this.getId() + "");
			writer.writeAttribute("status", "READY");
			for (Option op : xor) {
				op.exportWf2Xml(writer);
				writer.writeCharacters("\n");
			}
			writer.writeEndElement();
			writer.writeCharacters("\n");
		} catch (XMLStreamException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((xor == null) ? 0 : xor.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		Xor other = (Xor) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (xor == null) {
			if (other.xor != null)
				return false;
		} else if (!xor.equals(other.xor))
			return false;
		return true;
	}


//	@Override
//	public void exportWf2XmlArti(XMLStreamWriter writer) {
//		try {
//			writer.writeStartElement("rwfl:Node");
//			writer.writeAttribute("type", "XOR");
//			writer.writeAttribute("refID", this.getId() + "");
//			writer.writeAttribute("status", "READY");
//			for (Option op : xor) {
//				op.exportWf2XmlArti(writer);
//				writer.writeCharacters("\n");
//			}
//			writer.writeEndElement();
//			writer.writeCharacters("\n");
//		} catch (XMLStreamException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//
//	}

}
