/*
 *  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.bridge;

import static org.animotron.Expression._;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.animotron.AbstractExpression;
import org.animotron.Expression;
import org.animotron.Statements;
import org.animotron.exception.EBuilderTerminated;
import org.animotron.graph.CommonBuilder;
import org.animotron.operator.AN;
import org.animotron.operator.THE;
import org.animotron.serializer.ResultSerializer;
import org.neo4j.graphdb.Relationship;

/**
 * @author <a href="mailto:shabanovd@gmail.com">Dmitriy Shabanov</a>
 * 
 */
public class AnimoServlet extends HttpServlet {

	private static final long serialVersionUID = 7276574723383015880L;
	
	private void writeResponse(Relationship r, HttpServletResponse res) throws IOException {
		res.setContentType("text/html");
		OutputStream out = res.getOutputStream();
		try {
			ResultSerializer.serialize(r, out);
		} catch (InterruptedException e) {
			e.printStackTrace();
			throw new IOException(e);
		}
	}

	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		try {
			AnimoRequest r = new AnimoRequest(req.getRequestURI());
			
	        Expression s = new Expression(
                _(THE._, "s", _(AN._, "service", _(AN._, AN._.name(r)), _(AN._, "test-site")))
            );

			writeResponse(s, res);
		} catch (EBuilderTerminated e) {
			throw new IOException(e);
		}
	}

	@Override
	public void doPut(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		Relationship r = CommonBuilder.build(req.getInputStream(), req.getRequestURI());
		writeResponse(r, res);
	}
	
	private static AN op = AN._;
	
	class AnimoRequest extends AbstractExpression {
		
		public AnimoRequest(String uri) throws EBuilderTerminated {
			String[] parts = uri.split("/");
			System.out.println(Arrays.toString(parts));
			
			startGraph();
			for (String part : parts) {
				if (part.isEmpty()) continue;
				
				String prefix, ns, name;
				
				String[] s = part.split(":", 1);
				if (s.length == 1) {
					prefix = op.name();
					ns = op.namespace();
					name = s[0];
				} else {
					prefix = s[0];
					ns = Statements.prefix(prefix).namespace();
					name = part.substring(prefix.length());
				}

				start(prefix, ns, name, null);
			}
			for (String part : parts) {
				if (part.isEmpty()) continue;
				end();
			}
			endGraph();
			
		}
		
	}
	
}

@@ -868,17 +868,17 @@
 ression.
-_
+*
 ;%0A%0Aimpor
@@ -1396,16 +1396,105 @@
 or.THE;%0A
+import org.animotron.operator.relation.HAVE;%0Aimport org.animotron.operator.relation.USE;%0A
 import o
@@ -2218,16 +2218,18 @@
 ry %7B%0A%09%09%09
+//
 AnimoReq
@@ -2272,24 +2272,392 @@
 uestURI());%0A
+%09%09%09new Expression(%0A    %09%09%09_(THE._, %22request%22, //TODO: make uniq?%0A    %09%09%09%09_(HAVE._, %22uri%22, text(req.getRequestURI())),%0A    %09%09%09%09_(HAVE._, %22method%22, text(%22GET%22)),%0A    %09%09%09%09_(HAVE._, %22host%22, text(%22localhost%22)),%0A    %09%09%09%09_(USE._, %22theme-concrete%22), //why do we need this two here? theme def @test-site & layout @root-service%0A    %09%09%09%09_(USE._, %22root-layout%22)%0A    %09%09%09)%0A    %09%09);%0A
 %09%09%09%0A%09       
@@ -2749,20 +2749,17 @@
 ._, 
-AN._.name(r)
+%22request%22
 ), _

add 'request'