package ws.prova;

/*
 * Copyright (C) 2002-2007 <a href="mailto:a.kozlenkov@city.ac.uk">Alex Kozlenkov</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
 */

import ws.prova.util.DefaultLogicFactorySupport;

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

/**
 * Prova message
 * <p>Title: Prova</p>
 * <p>Description: Prova message</p>
 * <p>Copyright: Copyright (C) 2002-2007</p>
 * <p>Company: City University, London, United Kingdom</p>
 *
 * @author Alex Kozlenkov
 * @version 2.0 <25 March 2007>
 */
public class RMessage implements Serializable {
	private static final long serialVersionUID = 204135586772201515L;
	private String content;
	private String protocol;
	private List objects = new ArrayList();
	private transient String tid;

	public void assign_conversation_id(String _tid) {
		if (content.indexOf("[\"id\",") >= 0 || content.indexOf("[id,") >= 0) {
			// It is a follow-up to a previously created conversation
			tid = content.split("[,\\]]")[1];
		} else if(content.startsWith("%")) {
			// It is a new conversation
			//    and
			//    it is a message constructed by reagent.prepare_goal() entirely consisting of an object placeholder in objects[0]
			if( tid==null )
				tid = _tid;
		} else {
			// It is a new conversation
			tid = _tid;
			setContent("[[id," + tid + "]," + content.substring(1));
		}
	}

	/**
	 * Return the sender of the RMessage
	 *
	 * @return The sender of the current message
	 */
	public String sender() {
		int pos1 = getContent().indexOf("],");
		if (pos1 > 0) {
			// id already assigned
			return getContent().substring(pos1 + 2).split(",")[0];
		}
		// id not assigned
		return getContent().substring(1).split(",")[0];
	}

	/**
	 * Build an RMessage given a String representation that does not
	 * contain embedded objects
	 *
	 * @param _content String
	 */
	public RMessage(String _content) {
		setContent(_content);
	}

	public RMessage(String _content, Object[] _objs) {
		setContent(_content);
		for (int i = 0; i < _objs.length; i++)
			objects.add(_objs[i]);
	}

	//
	public RMessage append_string(String s) {
		setContent(getContent() + s);
		return this;
	}

	public String getProtocol() {
		return protocol;
	}

	public void setProtocol(String protocol) {
		this.protocol = protocol;
	}

	public RMessage append(Object o) {
		if (objects.size() == 0) {
			objects.add(o);
			setContent(getContent() + "%0");
			return this;
		}
		int i = 0;
		for (i = 0; i < objects.size(); i++) {
			if (o == objects.get(i)) {
				setContent(getContent() + ("%" + i));
				return this;
			}
		}
		objects.add(o);
		setContent(getContent() + ("%" + i));
		return this;
	}

	public String content() {
		return getContent();
	}

	public Object[] objs() {
		if (objects.size() == 0)
			return new Object[]{};
		return objects.toArray();
	}

	public String toString() {
		return RListUtils.term_as_string(RListUtils.rlist(content, objs()));
	}

	public String performative() {
		String text = this.toString();
		String qperf = text.split(",")[3];
		String perf = qperf.substring(1, qperf.length() - 1);
		return perf;
	}

	public String conv_id() {
		return tid;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public List getObjs() {
		return objects;
	}

	public void setObjs(List objects) {
		this.objects.clear();
		this.objects.addAll(objects);
	}
}

