package ch.hevs.aislab.magpie.event;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

import alice.tuprolog.Term;
import ch.hevs.aislab.magpie.environment.Services;

public class LogicTupleEvent extends MagpieEvent {

    private String logicRepresentation;
	
	public LogicTupleEvent(Term term) {
		this.type = Services.LOGIC_TUPLE;
		this.logicRepresentation = term.toString();
	}

    public LogicTupleEvent(String logicEvent) {
        this.type = Services.LOGIC_TUPLE;

        // This checks if the Term is correct
        Term term = Term.createTerm(logicEvent);
        this.logicRepresentation = term.toString();
    }

    /**
     * Creates a logic tuple with format: name(arg1,arg2,...,argN)
     * @param name
     * @param args
     */
	public LogicTupleEvent(String name, String ... args) {
		this.type = Services.LOGIC_TUPLE;
		String tuple = name + "(";

        for (String arg : args) {
            tuple = tuple + arg + ",";
        }
		
		// Remove the last comma and close the parenthesis
		tuple = tuple.substring(0, tuple.length() - 1);
		tuple = tuple + ")";
		
		this.logicRepresentation = tuple;
	}
	
	/**
	 * It returns the tuple representation of the event
	 * 
	 * @return
	 */
	public String toTuple(){
		return logicRepresentation;
	}

    public String getName() {
        int end = logicRepresentation.indexOf("(");
        return logicRepresentation.substring(0,end);
    }

    public List<String> getArguments() {
        List<String> arguments = new ArrayList<>();
        int elements = StringUtils.countMatches(logicRepresentation, ",");

        if (elements == 0) {
            arguments.add(getSubstring("(", ")"));
        } else if (elements == 1) {
            arguments.add(getSubstring("(", ","));
            arguments.add(getSubstring(",", ")"));
        } else if (elements > 1) {
            arguments.add(getSubstring("(", ","));
            String restString = getSubstring(",", ")");
            String[] restArray = StringUtils.split(restString, ",");
            for (String aRestArray : restArray) {
                arguments.add(aRestArray);
            }
        }
        return arguments;
    }

    private String getSubstring(String start, String end) {
        int first = StringUtils.indexOf(logicRepresentation, start);
        int second = StringUtils.indexOf(logicRepresentation, end);
        return StringUtils.substring(logicRepresentation, first + 1, second);
    }

}

@@ -745,16 +745,409 @@
 ..,argN)
+, and assigns the timestamp to the%0A     * event%0A     * @param timestamp%0A     * @param name%0A     * @param args%0A     */%0A    public LogicTupleEvent(long timestamp, String name, String ... args) %7B%0A        this(name, args);%0A        this.setTimestamp(timestamp);%0A    %7D%0A%0A    /**%0A     * Creates a logic tuple with format: name(arg1,arg2,...,argN), and assigns the current timestamp%0A     * to the event
 %0A     * 

Add constructor to assign a specific timestamp