/***************************************************************************
 *   Copyright (C) 2008-2015 by Fabrizio Montesi <famontesi@gmail.com>     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 *                                                                         *
 *   For details about the authors of this software, see the AUTHORS file. *
 ***************************************************************************/

package jolie.embedding.js;

import java.io.IOException;
import java.io.StringReader;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.script.Invocable;
import javax.script.ScriptException;
import jolie.Interpreter;
import jolie.js.JsUtils;
import jolie.net.CommChannel;
import jolie.net.CommMessage;
import jolie.net.PollableCommChannel;
import jolie.runtime.Value;
import jolie.runtime.typing.Type;

/**
 * @author Fabrizio Montesi
 * 
 * TODO: this shouldn't be polled
 */
public class JavaScriptCommChannel extends CommChannel implements PollableCommChannel
{
	private final Invocable invocable;
	private final Map< Long, CommMessage > messages = new ConcurrentHashMap< Long, CommMessage >();
	private final Object json;
	
	public JavaScriptCommChannel( Invocable invocable, Object json )
	{
		this.invocable = invocable;
		this.json = json;
	}

	@Override
	public CommChannel createDuplicate()
	{
		return new JavaScriptCommChannel( invocable, json );
	}

	@Override
	protected void sendImpl( CommMessage message )
		throws IOException
	{
		Object returnValue = null;
		try {
			StringBuilder builder = new StringBuilder();
			JsUtils.valueToJsonString( message.value(), true, Type.UNDEFINED, builder );
			Object param = invocable.invokeMethod( json, "parse", builder.toString() );
			returnValue = invocable.invokeFunction( message.operationName(), param );
		} catch( ScriptException e ) {
			throw new IOException( e );
		} catch( NoSuchMethodException e ) {
			throw new IOException( e );
		}
		
		CommMessage response;
		if ( returnValue != null ) {
			Value value = Value.create();
			
			if ( returnValue instanceof Value ) {
				value.refCopy( (Value)returnValue );
			} else {
				try {
					Object s = invocable.invokeMethod( json, "stringify", returnValue );
					JsUtils.parseJsonIntoValue( new StringReader( (String)s ), value, true );
				} catch( ScriptException e ) {
					// TODO: do something here, maybe encode an internal server error
				} catch( NoSuchMethodException e ) {
					// TODO: do something here, maybe encode an internal server error
				}
				
				value.setValue( returnValue );
			}
			
			response = new CommMessage(
				message.id(),
				message.operationName(),
				message.resourcePath(),
				value,
				null
			);
		} else {
			response = CommMessage.createEmptyResponse( message );
		}
		
		messages.put( message.id(), response );
	}
	
	@Override
	protected CommMessage recvImpl()
		throws IOException
	{
		throw new IOException( "Unsupported operation" );
	}
	
	@Override
	public CommMessage recvResponseFor( CommMessage request )
		throws IOException
	{
		return messages.remove( request.id() );
	}

	@Override
	protected void disposeForInputImpl()
		throws IOException
	{
		Interpreter.getInstance().commCore().registerForPolling( this );
	}

	@Override
	protected void closeImpl()
	{}

	@Override
	public boolean isReady()
	{
		return( !messages.isEmpty() );
	}
}

@@ -2295,16 +2295,135 @@
 json;%0A%09%0A
+%09private final static class JsonMethods %7B%0A%09%09private final static String STRINGIFY = %22stringify%22, PARSE = %22parse%22;%0A%09%7D%0A%09%0A
 %09public 
@@ -2480,16 +2480,16 @@
  json )%0A
-
 %09%7B%0A%09%09thi
@@ -2944,23 +2944,33 @@
 ( json, 
-%22parse%22
+JsonMethods.PARSE
 , builde
@@ -3445,27 +3445,37 @@
 ( json, 
-%22stringify%22
+JsonMethods.STRINGIFY
 , return

Finalise some constants.