package ever.workflowRepresentation;

import java.util.EmptyStackException;
import java.util.Stack;

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

import ever.pipeline.Sentence;

public class And extends WorkflowElement {
	private String name = "And";
	/**
	 * Stack is used to store the different paths of an AND
	 */
	private Stack<Sequence> and;


	/**
	 * Standard constructor
	 * @param parent The parent element 
	 * @param s The sentence of which the AND was extracted
	 */
	public And(WorkflowElement parent,Sentence s) {
		super(parent,s);
		and = new Stack<Sequence>();
	}

	/**
	 * Adds new path to the and-node.
	 * 
	 * @param op
	 *            sequence to add to and node
	 */
	public void addOption(Sequence op) {
		and.add(op);
	}

	/**
	 * returns and deletes next sequence
	 * 
	 * @return next sequence in and-node
	 */
	public Sequence getNextSequence() throws EmptyStackException{		
		return and.pop();		
	}
	
	/**
	 * 
	 * @return the number of parallel sequences after the and node
	 */
	public int getOptionCount()
	{
		return and.size();
	}
	
	

	
	/**
	 * Writes and-node and all sequences to output-format
	 * @param writer stream-writer that is used to write out
	 */
	public void exportWf2Xml(XMLStreamWriter writer) {
		try {
			writer.writeStartElement("rwfl:Node");
			writer.writeAttribute("refID", this.getId() + "");
			writer.writeAttribute("type", "AND");
			writer.writeAttribute("status", "READY");
			for (Sequence seq : and) {
				seq.exportWf2Xml(writer);
			}
			writer.writeEndElement();
		} 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 + ((and == null) ? 0 : and.hashCode());
		result = prime * result + ((name == null) ? 0 : name.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;
		And other = (And) obj;
		if (and == null) {
			if (other.and != null)
				return false;
		} else if (!and.equals(other.and))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

	

//	/**
//	 * Special method to export an random artificial and-node
//	 * @param writer stream-writer that is used to write out
//	 */
//	public void exportWf2XmlArti(XMLStreamWriter writer) {
//		try {
//			writer.writeStartElement("rwfl:Node");
//			writer.writeAttribute("refID", this.getId() + "");
//			writer.writeAttribute("type", "AND");
//			writer.writeAttribute("status", "READY");
//			for (Sequence seq : and) {
//				seq.exportWf2XmlArti(writer);
//			}
//			writer.writeEndElement();
//		} catch (XMLStreamException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//
//	}

}