package org.arakhne.neteditor.fsm.constructs ;

import java.io.IOException;
import java.util.Map;

import org.arakhne.neteditor.formalism.standard.StandardEdge;

public class FSMTransition
extends StandardEdge<FiniteStateMachine,AbstractFSMNode,FSMAnchor,FSMTransition> {

	private String action = null;
	private String guard = null;
	
	public FSMTransition() {
		super();
	}
	
	public String getAction() {
		return this.action;
	}

	public void setAction(String action) {
		String na = (action==null || action.isEmpty()) ? null : action;
		if ((this.action==null && na!=null) ||
			(this.action!=null && !this.action.equals(na))) {
			String old = this.action;
			this.action = na;
			firePropertyChanged("action", old, this.action);
		}
	}

	public String getGuard() {
		return this.guard;
	}

	public void setGuard(String guard) {
		String ng = (guard==null || guard.isEmpty()) ? null : guard;
		if ((this.guard==null && ng!=null) ||
			(this.guard!=null && !this.guard.equals(ng))) {
			String old = this.guard;
			this.guard = ng;
			firePropertyChanged("guard", old, this.guard);
		}
	}
	
	@Override
	public Map<String, Object> getProperties() {
		Map<String,Object> properties = super.getProperties();
		properties.put("action", this.action);
		properties.put("guard", this.guard);
		return properties;
	}
	
	@Override
	public void setProperties(Map<String, Object> properties)
			throws IOException {
		super.setProperties(properties);
		if (properties!=null) {
			setAction(propGet(String.class, "action", null, properties));
			setGuard(propGet(String.class, "guard", null, properties));
		}
	}
	
	@Override
	public String getExternalLabel() {
		StringBuilder label = new StringBuilder();
		String guard = getGuard();
		if (guard!=null) {
			label.append(guard);
		}
		String action = getAction();
		if (action!=null) {
			label.append("/");
			label.append(action);
		}
		return label.toString();
	}
	
	public void setStartNode(FSMState node) {
		FSMAnchor anchor = null;
		if (node!=null && !node.getAnchors().isEmpty()) {
			anchor = node.getAnchors().get(0);
		}
		setStartAnchor(anchor);
	}
	
	public void setEndNode(FSMState node) {
		FSMAnchor anchor = null;
		if (node!=null && !node.getAnchors().isEmpty()) {
			anchor = node.getAnchors().get(0);
		}
		setEndAnchor(anchor);
	}

}