/*
 * Copyright (C) 1999-2006 <a href="mailto:paschke@in.tum.de">Adrian Paschke</a>
 * 
 * The Utils Library is part of the Rule Based Service Level Agreement (RBSLA)
 * framework, available at 
 * <a href=" http://ibis.in.tum.de/staff/paschke/rbsla/index.htm">RBSLA</a>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package rbsla.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import org.mandarax.kernel.ComplexTerm;
import org.mandarax.kernel.Term;
import org.mandarax.kernel.VariableTerm;

import java.io.PrintWriter;
import java.io.Writer;
import java.io.PrintStream;

import ws.prova.RListUtils;





/**
 * Used to print prova results on the console
 */
public class ConsoleWriter extends PrintWriter {
	
	private boolean console = true; // default = true (= show console output)
	private boolean reactive = false; // default = false (= show yes/no output)
	
	public ConsoleWriter(PrintStream out) {		
		super(out);
	}
	
	public ConsoleWriter(Writer out) {
		super(out);
	}
	

	
	/**
	 * Write only results other than yes or no	
	 */
	public void write(String s) {
	  if (reactive) {
		if ((!s.equals("yes")) && (!s.equals("no")) &&(s.indexOf("@@")==-1) && (console)) {
			super.write(s);
		} 			
	  } else super.write(s); // show all outputs	
	}

	/**
	 * Show / Hide yes/no results in ECA processing mode
	 * @param _reactive boolean
	 */
	public void startReactive(boolean _reactive) {
		reactive = _reactive;
	}
	
	/**
	 * Show / Hide the console output
	 * @param _console boolean
	 */
	public void showConsole(boolean _console) {
		console = _console;
	}

	/**
	 * Is console hidden?
	 * @return boolean
	 */
	public boolean consoleHidden() {
		return console;
	}
	
	/**
	 *  parse prova resuls (lists and complex terms) 
	 */
	
	private static StringBuffer parse(Object r, String sep) {

		Collection list = null;
		if (r instanceof Collection) {
			list = (Collection) r;
		} else if (r instanceof ComplexTerm) {
			list = new ArrayList();
			RListUtils.rlist2List((ComplexTerm) r, list, 0);
		}
		StringBuffer sb = new StringBuffer();

		for (Iterator li = list.iterator(); li.hasNext();) {
			Object o = li.next();

			if (o instanceof Term) {
				Term t = (Term) o;
				if (t.isVariable()) {
					sb.append('<');
					sb.append(t.getType().getName());
					sb.append('.');
					sb.append(((VariableTerm) t).getName());
					sb.append('>');
					if (li.hasNext())
						sb.append(sep);
					continue;
				}
				if (t.isCompound()) {
					sb.append(RListUtils.term_as_string(t));
					if (li.hasNext())
						sb.append(sep);
					continue;
				}
			}

			sb.append(o);
			if (li.hasNext())
				sb.append(sep);

		}
		return sb;
	}
	

}