package transformation.associationtags;

import java.util.Map;
import java.util.HashMap;

public class AssociationTag {
	
	String action;
	String user;
	String role;

	
	/**
	 * Constructor that creates empty strings as action, user and role of the association tag
	 */
	public AssociationTag() {
		this.action="";
		this.user="";
		this.role="";
	}
	
	/**
	 * Constructor with values
	 * 
	 * @param action 	Name of the protected action
	 * @param user 		Name of the user
	 * @param role		Name of the role
	 */
	public AssociationTag(String action, String user, String role) {
		
		this.action = action;
		this.user = user;
		this.role = role;
		
	}
	
	/**
	 * 
	 * @return	Name of the action
	 */
	public String getAction() {
		return action;
	}

	/**
	 * 
	 * @param action	Name of the action
	 */
	public void setAction(String action) {
		this.action = action;
	}
	
	/**
	 * 
	 * @return	The association tag {protected = ([action])} as a full string
	 */
	public String actionToString() {
		return "{protected = (" + this.getAction()+ ")}";
	}

	/**
	 * 
	 * @return	The name of the user
	 */
	public String getUser() {
		return user;
	}

	/**
	 * 
	 * @param user	The name of the user
	 */
	public void setUser(String user) {
		this.user = user;
	}

	/**
	 * 
	 * @return	The name of the role
	 */
	public String getRole() {
		return role;
	}

	/**
	 * 
	 * @param role	The name of the role
	 */
	public void setRole(String role) {
		this.role = role;
	}
	
	/**
	 * 
	 * @return	The association tag {role = ([user], [role])} as a full string
	 */
	public String roleToString() {
		return "{role = (" + this.user + ", " + this.role+")}";
	}
	
	/**
	 * 
	 * @return	The association tag {right = ([role], [action])} as a full string
	 */
	public String rightToString() {
		return "{right =  (" + this.role + ", " + this.action + ")}";
	}

	/**
	 * 
	 * @param tagStr	The association tag in the form that it is stored in the comment of the UMLsec model
	 * @return			The association tag as an instance of the AssociationTag class
	 */
	public AssociationTag extractSelfFromStr(String tagStr) {
		
		String[] elements = tagStr.split(",");
		
		if (elements.length < 3) {
			return null;
		}
		
		for (String element : elements) {
			
			if (elements[0].length() > 0) {
				this.action = elements[0];
			}
			
			if (elements[1].length() > 0) {
				this.user = elements[1];
			}
			
			if (elements[2].length() > 0) {
				this.role = elements[2];
			}
		}
		return this;
	}

	/**
	 * @return 	The whole association tag as a full string
	 */
	public String toString() {

		return "\n{protected = (" + this.getAction()+ ")}\n" +
				"{role = (" + this.user + ", " + this.role+")}\n" +
				"{right =  (" + this.role + ", " + this.action + ")}\n";
}
}
