package basic;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

import uk.ac.bbk.dcs.automed.qproc.iql.ASG;
import uk.ac.bbk.dcs.automed.qproc.iql.ASGComprehensionMiniCon;
import uk.ac.bbk.dcs.automed.qproc.iql.ParseException;
import uk.ac.ic.doc.automed.wrappers.AutoMedInteger;
import uk.ac.ic.doc.automed.wrappers.AutoMedString;
import uk.ac.ic.doc.automed.wrappers.SQLQuery;
import uk.ac.ic.doc.rodex.qproc.QueryFunction;
import uk.ac.ic.doc.rodex.qproc.QueryVariable;

public class ASGExample {
	public static void main(String args[]) {
		try {
			PrintStream out = System.out;
			ASG g = new ASG("[{r,x,y} | {r,x} <- <<person,name>>; {r,x} <- <<person,dept>>; {r,y} <- <<person,dob>>; "
					+ "x > 'a'; y = 't'; x < 'z']");
      QueryVariable var = new QueryVariable("x");
      List funcs = new ArrayList();
      funcs.add(new QueryFunction(SQLQuery.LESSTHAN, var, new AutoMedString("t")));
      funcs.add(new QueryFunction(SQLQuery.GREATERTHAN, var, new AutoMedString("b")));
      funcs.add(new QueryFunction(SQLQuery.LESSTHAN, var, new AutoMedInteger(2, 32)));
      funcs.add(new QueryFunction(SQLQuery.GREATERTHAN, var, new AutoMedInteger(100, 32)));
      
			out.println("g before:\n");
			g.println();
      
      ASGComprehensionMiniCon.replaceFunctions(g, var, funcs);

      out.println("g after:\n");
			g.println();
      
			// wants to replace all x-related functions with a new set of functions
//			String myVar = "x";
//			// the new function set
//			// create an ASG from two or more functions
//			Cell f1 = createBinaryFunction("(>)", "x", "'b'");
//			Cell f2 = createBinaryFunction("(<=)", "x", "'t'");
//			List l = new LinkedList();
//			l.add(f2); // order is important
//			l.add(f1);
//			Cell funs = getCompFromFuns(l, "LComp");
//			Cell funsEnd = funs;
//			while (funsEnd.r() != null)
//				funsEnd = funsEnd.r();
//			// print out the ASG before replacement
//			out.println("g before:\n");
//			g.println();
//			// parse existing ASG, test each function cell against my variable
//			// if found replace with the new function set f2c
//			Cell c = g.root();
//			Cell p = null; // parent
//			boolean found = false;
//			while (c.representsComprehension()) {
//				if (c.l().r().representsFunction()) {
//					if (containsVar(c.l().r(), "x")) {
//						if (!found) {
//							//Remove and replace function
//							funsEnd.set_r(c.r());
//							p.set_r(funs);
//							c = funsEnd;
//							found = true;
//						} else {
//							//Just remove function
//							p.set_r(c.r());
//						}
//					}
//				} else {
//					// Do nothing
//				}
//				p = c;
//				c = c.r();
//			}
//			// print ASG after replacement
//			out.println("g after:\n");
//			g.println();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

//	private static boolean containsVar(Cell c, String var) {
//		// Works only for binary functions, needs extension to become generic - easily done.
//		if (c.l().r().isVarOrSysVar() && c.l().r().text().equals(var))
//			return true;
//		else if (c.r().isVarOrSysVar() && c.r().text().equals(var))
//			return true;
//		else
//			return false;
//	}
//
//	private static Cell createBinaryFunction(String op, String var1, String var2) {
//		// Works only with binary functions where first item is variable and second is string - otherwise needs to be extended,
//		// easily done.
//		return new Cell(new Cell(new Cell(Cell.function, op), new Cell(Cell.variable, var1)), new Cell(Cell.stringType, var2));
//	}
//
//	private static Cell getCompFromFuns(List l, String compType) {
//		Cell c = null;
//		Iterator lit = l.listIterator();
//		while (lit.hasNext()) {
//			c = new Cell(new Cell(new Cell(compType), (Cell) lit.next()), c);
//		}
//		return c;
//	}
}
