package br.uem.din.metrics.concernDrivenMetrics.interactionBeteweenConcerns;

import java.util.Collection;
import java.util.HashMap;

import br.uem.din.architectureEvolution.representation.Architecture;
import br.uem.din.architectureEvolution.representation.Component;
import br.uem.din.architectureEvolution.representation.Concern;
import br.uem.din.architectureEvolution.representation.Element;
import br.uem.din.architectureEvolution.representation.Interface;
import br.uem.din.architectureEvolution.representation.Operation;

public class IIBC {

	private final Architecture architecture;
	private final HashMap<Concern, IIBCResult> results = new HashMap<Concern, IIBCResult>();

	public IIBC(Architecture architecture){
		this.architecture = architecture;
		
		for (Component component : architecture.getComponents().values()) {
			inspectInterfacesOperations(component, component.getImplementedInterfaces());
			inspectInterfacesOperations(component, component.getRequiredInterfaces());
		}
	}

	private void inspectInterfacesOperations(Component component, Collection<Interface> interfaces) {
		for (Interface i : interfaces) {
			inspectConcernsOfElement(i, i);
			for (Operation operation : i.getOperations()) {
				inspectConcernsOfElement(operation, i);
			}
		}
	}

	private void inspectConcernsOfElement(Element element, Interface i) {
		for (Concern concern : element.getConcerns()) {
			if (results.containsKey(concern))
				results.get(concern).addInterlacedConcerns(element, i);
			else
				results.put(concern, new IIBCResult(concern, element, i));
		}
	}

	public Architecture getArchitecture() {
		return architecture;
	}

	public HashMap<Concern, IIBCResult> getResults() {
		return results;
	}
}
