package org.workcraft.plugins.stg;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.workcraft.exceptions.ArgumentException;
import org.workcraft.plugins.stg.SignalTransition.Direction;
import org.workcraft.util.Pair;
import org.workcraft.util.Triple;

public class LabelParser {
	private static final Pattern signalTransitionPattern =
			Pattern.compile("^([_A-Za-z][_A-Za-z0-9]*)([\\+\\-\\~])(\\/([0-9]+))?");

	private static final Pattern dummyTransitionPattern =
			Pattern.compile("^([_A-Za-z][_A-Za-z0-9]*)(\\/([0-9]+))?");

	private static final Pattern instancedTransitionPattern =
			Pattern.compile("^([_A-Za-z][_A-Za-z0-9]*[\\+\\-\\~]?)(\\/([0-9]+))?");

	public static Pair<String, String> parseImplicitPlaceReference(String ref) {
		String[] parts = ref.replaceAll(" ", "").split(",");
		if (parts.length < 2 || !parts[0].startsWith("<") || !parts[1].endsWith(">")) {
			return null;
		}
		return Pair.of(parts[0].substring(1), parts[1].substring(0, parts[1].length()-1));
	}

	public static Triple<String, SignalTransition.Direction, Integer> parseSignalTransition(String s) {
		Triple<String, SignalTransition.Direction, Integer> result = null;
		final Matcher matcher = signalTransitionPattern.matcher(s);
		if (matcher.find() && (matcher.end() == s.length())) {
			final String signalName = matcher.group(1);

			final Direction direction;
			String directionGroup = matcher.group(2);
			if (directionGroup.equals("+")) {
				direction = SignalTransition.Direction.PLUS;
			} else if (directionGroup.equals("-")) {
				direction = SignalTransition.Direction.MINUS;
			} else {
				direction = SignalTransition.Direction.TOGGLE;
			}

			final Integer instance;
			String instanceGroup = matcher.group(4);
			if (instanceGroup == null) {
				instance = null;
			} else {
				instance =  Integer.parseInt(instanceGroup);
			}
			result = Triple.of(signalName, direction, instance);
		} else {
			throw new ArgumentException();
		}
		return result;
	}

	public static Pair<String, Integer> parseDummyTransition(String s) {
		Pair<String, Integer> result = null;
		final Matcher matcher = dummyTransitionPattern.matcher(s);
		if (matcher.find() && (matcher.end() == s.length())) {
			final String name = matcher.group(1);

			final Integer instance;
			String instanceGroup = matcher.group(3);
			if (instanceGroup == null) {
				instance = null;
			} else {
				instance =  Integer.parseInt(instanceGroup);
			}
			result = Pair.of(name, instance);
		}
		return result;
	}

	public static Pair<String, Integer> parseInstancedTransition(String s) {
		final Matcher matcher = instancedTransitionPattern.matcher(s);

		if (!matcher.find()) {
			return null;
		}
		if (! (matcher.end() == s.length())) {
			return null;
		}
		final String instanceGroup = matcher.group(3);

		return Pair.of(matcher.group(1),
				instanceGroup == null? null : Integer.parseInt(instanceGroup));
	}

}

@@ -98,59 +98,8 @@
 n;%0A%0A
-import org.workcraft.exceptions.ArgumentException;%0A
 impo
@@ -1871,53 +1871,8 @@
 e);%0A
-%09%09%7D else %7B%0A%09%09%09throw new ArgumentException();%0A
 %09%09%7D%0A
@@ -2487,16 +2487,55 @@
 ng s) %7B%0A
+%09%09Pair%3CString, Integer%3E result = null;%0A
 %09%09final 
@@ -2591,24 +2591,22 @@
 her(s);%0A
-%0A
 %09%09if (
-!
 matcher.
@@ -2615,39 +2615,11 @@
 nd()
-) %7B%0A%09%09%09return null;%0A%09%09%7D%0A%09%09if (!
+ &&
  (ma
@@ -2655,33 +2655,76 @@
 %0A%09%09%09
-return null;%0A%09%09%7D%0A%09%09final 
+final String name = matcher.group(1);%0A%09%09%09final Integer instance;%0A%09%09%09
 Stri
@@ -2760,114 +2760,187 @@
 oup(
-3);%0A%0A%09%09return Pair.of(matcher.group(1),%0A%09%09%09%09instanceGroup == null? null : Integer.parseInt(instanceGroup))
+2);%0A%09%09%09if (instanceGroup == null) %7B%0A%09%09%09%09instance = null;%0A%09%09%09%7D else %7B%0A%09%09%09%09instance =  Integer.parseInt(instanceGroup);%0A%09%09%09%7D%0A%09%09%09result = Pair.of(name, instance);%0A%09%09%7D%0A%09%09return result
 ;%0A%09%7D

Fix for STG signal renaming in the property editor.