package br.uem.din.architectureEvolution.representation;

import static br.uem.din.architectureEvolution.helpers.ElementTypeNames.*;

import java.util.ArrayList;
import java.util.Collection;

import com.sdmetrics.model.ModelElement;

public class AssociationInterClassRelationship extends InterClassRelationship {
	
	private final Collection<AssociationEnd> participants = new ArrayList<AssociationEnd>();
	
	public AssociationInterClassRelationship(ModelElement element, Architecture architecture) throws Exception {
		super(element);
		loadAssociationEnds(architecture);
	}
	
	public AssociationInterClassRelationship(Class class1, Class class2, Architecture architecture) throws Exception {
		super(new ModelElement(architecture.getMetaModel().getType(ASSOCIATION)));
	
		Collection<ModelElement> connections = new ArrayList<ModelElement>();
		
		ModelElement participant1 = new ModelElement(architecture.getMetaModel().getType("associationend"));
		participant1.setRefAttribute("associationendtype", class1.getModelElement());
		connections.add(participant1);
		
		ModelElement participant2 = new ModelElement(architecture.getMetaModel().getType("associationend"));
		participant2.setRefAttribute("associationendtype", class2.getModelElement());
		connections.add(participant2);
		
		getModelElement().setSetAttribute("connection", connections);
		loadAssociationEnds(architecture);
	}
	
	@SuppressWarnings("unchecked")
	public void loadAssociationEnds(Architecture architecture) {
		Collection<ModelElement> connections = (Collection<ModelElement>) getModelElement().getSetAttribute("connection");
		for (ModelElement conn : connections) {
			ModelElement participant = conn.getRefAttribute("associationendtype");
			Class c = architecture.findClassByXMIID(participant.getXMIID());
			getParticipants().add(new AssociationEnd(conn, c));
		}
	}

	public Collection<AssociationEnd> getParticipants() {
		return participants;
	}
}
