/**
 * 
 */
package org.etsi.saref.server.outputformatter;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import org.apache.jena.sparql.expr.nodevalue.NodeValueString;
import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;

/**
 * @author Omar Qawasmeh
 *
 *
 */

@XmlRootElement(name = "testcase")
@XmlType(propOrder = { "systemErr", "systemOut", "info" })
public class TestCase {

	public enum Status {
		SUCCESS, WARNING, DANGER, UNKNOWN;
	}

	private String name;
	private String className;
	private String systemErr;
	private String systemOut;
	private String status; // either pass or failure
	private String info;
	private Status statusObj;

	/**
	 * @return the status
	 */
	@XmlAttribute
	public String getStatus() {
		return status;
	}

	/**
	 * @param status
	 *            the status to set
	 */
	public void setStatus(String status) {
		this.status = status;
	}

	public boolean isSuccess() {
		return statusObj == Status.SUCCESS;
	}

	public boolean isDanger() {
		return statusObj == Status.DANGER;
	}

	public boolean isWarning() {
		return statusObj == Status.WARNING;
	}


	public boolean isUnknown() {
		return statusObj == Status.UNKNOWN;
	}

	/**
	 * @param status
	 *            the status to set
	 */
	public void setStatusFlag(String status) {
		switch (status) {
		case "danger":
			this.statusObj = Status.DANGER;
			break;
		case "success":
			this.statusObj = Status.SUCCESS;
			break;
		case "warning":
			this.statusObj = Status.WARNING;
			break;
		default:
			this.statusObj = Status.UNKNOWN;
		}
	}

	@XmlAttribute
	public String getClassName() {
		return className;
	}

	/**
	 * @param className
	 *            the className to set
	 */
	public void setClassName(String className) {
		this.className = className;
	}

	@XmlAttribute
	public String getName() {
		if(name != null) {
	        Parser parser = Parser.builder().build();
	        Node document = parser.parse(name);
	        HtmlRenderer renderer = HtmlRenderer.builder().build();
	        return renderer.render(document);
		}
		return null;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the systemErr
	 */
	public String getSystemErr() {
		return systemErr;
	}

	/**
	 * @param systemErr
	 *            the systemErr to set
	 */
	public void setSystemErr(String systemErr) {
		this.systemErr = systemErr;
	}

	/**
	 * @return the systemOut
	 */
	public String getSystemOut() {
		return systemOut;
	}

	/**
	 * @param systemOut
	 *            the systemOut to set
	 */
	public void setSystemOut(String systemOut) {
		this.systemOut = systemOut;
	}

	/**
	 * @return the info
	 */
	public String getInfo() {
		if(info != null) {
	        Parser parser = Parser.builder().build();
	        Node document = parser.parse(info);
	        HtmlRenderer renderer = HtmlRenderer.builder().build();
	        return renderer.render(document);
		}
		return null;
	}

	/**
	 * @param info
	 *            the info to set
	 */
	public void setInfo(String info) {
		this.info = info;
	}

}
