/*
 *  Copyright (C) 2011 The Animo Project
 *  http://animotron.org
 *
 *  This program 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 3
 *  of the License, or (at your option) any later version.
 *
 *  This program 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 program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
package org.animotron.instruction.string;

import static org.animotron.graph.AnimoGraph.getORDER;

import java.io.IOException;

import org.animotron.instruction.AbstractInstruction;
import org.animotron.manipulator.PFlow;
import org.animotron.operator.Evaluable;
import org.animotron.serializer.StringResultSerializer;
import org.jetlang.channels.Subscribable;
import org.neo4j.graphdb.Relationship;

/**
 * String instruction 'after-last'.
 * 
 * Return last chars from input string after last-found defined pattern.
 * 
 * @author <a href="mailto:shabanovd@gmail.com">Dmitriy Shabanov</a>
 */
public class AfterLast extends AbstractInstruction implements Evaluable {
	
	public static final AfterLast _ = new AfterLast();
	
	private AfterLast() { super("after-last", STRING._); }
	
	public void eval(Relationship op, PFlow ch) throws InterruptedException, IOException {
		
		//UNDERSTAND: if we have more that 2 params, what to do?
		
		Relationship[] params = getORDER().first(2, op.getEndNode());
		
		//pattern
		StringResultSerializer szer = new StringResultSerializer();
		szer.serialize(params[0]);
		String pattern = szer.getString();

		szer.serialize(params[1]);
		String source = szer.getString();
		
		int index = source.lastIndexOf(pattern);
		String result = source.substring(index);
		
		System.out.println(result);
	}

	@Override
	public Subscribable<PFlow> onCalcQuestion() {
		// TODO Auto-generated method stub
		return null;
	}
}

@@ -908,30 +908,34 @@
 ort 
-java.io.IOExcept
+org.animotron.Express
 ion;%0A
-%0A
 impo
@@ -980,24 +980,69 @@
 nstruction;%0A
+import org.animotron.manipulator.OnQuestion;%0A
 import org.a
@@ -1635,99 +1635,189 @@
 %0A%09%0A%09
-public void eval(Relationship op, PFlow ch) throws InterruptedException, IOException
+@Override%0A%09public Subscribable%3CPFlow%3E onCalcQuestion() %7B%0A%09%09return question;%0A%09%7D%0A%09%0A%09private OnQuestion question = new OnQuestion() %7B%0A%09%09@Override%0A%09%09public void onMessage(PFlow pf)
  %7B%0A%09%09
-%0A%09
 %09//U
@@ -1872,17 +1872,19 @@
 o do?%0A%09%09
-%0A
+%09%0A%09
 %09%09Relati
@@ -1925,17 +1925,16 @@
 (2, 
-o
 p
+f
 .get
-End
+OP
 Node
@@ -1940,17 +1940,71 @@
 e());%0A%09%09
+%09
 %0A
+%09%09%09String pattern;%0A%09%09%09String source;%0A%09%09%09%0A%09%09%09try %7B%0A%0A%09%09
 %09%09//patt
@@ -2007,16 +2007,18 @@
 pattern%0A
+%09%09
 %09%09String
@@ -2071,16 +2071,18 @@
 izer();%0A
+%09%09
 %09%09szer.s
@@ -2104,23 +2104,18 @@
 %5B0%5D);%0A%09%09
-String 
+%09%09
 pattern 
@@ -2134,17 +2134,20 @@
 ring();%0A
-%0A
+%09%0A%09%09
 %09%09szer.s
@@ -2169,23 +2169,18 @@
 %5B1%5D);%0A%09%09
-String 
+%09%09
 source =
@@ -2200,17 +2200,21 @@
 ng();%0A%09%09
-%0A
+%09%0A%0A%09%09
 %09%09int in
@@ -2248,16 +2248,40 @@
 ttern);%0A
+%09%09%09%09if (index %3E 0) %7B%0A%09%09%09
 %09%09String
@@ -2316,14 +2316,16 @@
 ndex
++1
 );%0A%09%09
+%09
-%0A
 %09%09Sy
@@ -2355,122 +2355,184 @@
 );%0A%09
-%7D%0A%0A%09@Override%0A%09public Subscribable%3CPFlow%3E onCalcQues
+%09%09%09%09%0A%09%09%09%09%09pf.sendAnswer(new Expression(Expression.text(result))) ;%0A%09%09%09%09%7D%0A%09%09%09%0A%09%09%09%7D catch (Excep
 tion
-(
+ e
 ) %7B%0A%09%09
-// TODO Auto-generated method stub%0A%09%09return null
+%09%09//XXX: log?%0A%09%09%09%09pf.done();%0A%09%09%09%09return;%0A%09%09%09%7D%0A%0A%09%09%09pf.done()
 ;%0A
+%09
 %09%7D%0A
+%09%09%0A%09%7D;%0A
 %7D%0A

implement 'after-last' function