package br.uem.din.architectureEvolution.representation;

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

import br.uem.din.architectureEvolution.helpers.ModelElementHelper;
import br.uem.din.architectureEvolution.helpers.StereotypeHelper;

import com.sdmetrics.model.ModelElement;

public abstract class Element {

	private ModelElement modelElement;
	private String name;
	private Boolean isVariationPoint;
	private VariantType variantType;
	private final HashMap<String, Feature> features = new HashMap<String, Feature>();
	private final Collection<Concern> concerns = new ArrayList<Concern>();
	private Architecture architecture;
	
	protected abstract void loadFields() throws Exception;

	protected ModelElement getModelElement() {
		return modelElement;
	}

	protected void setModelElement(ModelElement modelElement, Architecture architecture) throws Exception {
		this.modelElement = modelElement;
		this.setArchitecture(architecture);
		setName(modelElement.getName());
		inspectStereotypes();
		loadFields();
	}

	private void inspectStereotypes() {
		for (ModelElement stereotype : ModelElementHelper.getStereotypes(modelElement)) {
			
			verifyVariationPoint(stereotype);
			verifyVariant(stereotype);	
			verifyFeature(stereotype);
			verifyConcern(stereotype);
		}
	}

	private void verifyConcern(ModelElement stereotype) {
		if (StereotypeHelper.isConcern(stereotype))
			concerns.add(architecture.getOrCreateConcernByName(stereotype.getName()));
	}

	private void verifyFeature(ModelElement stereotype) {
		if (StereotypeHelper.isFeature(stereotype))
			concerns.add(architecture.getOrCreateFeatureByName(stereotype.getName()));
	}

	private void verifyVariant(ModelElement stereotype) {
		VariantType type = VariantType.getByName(stereotype.getName());
		if (type != VariantType.NONE)
			setVariantType(type);
	}

	private void verifyVariationPoint(ModelElement stereotype) {
		if (StereotypeHelper.isVariationPoint(stereotype))
			setIsVariationPoint(true);
	}
	
	public String getName() {
		return name;
	}

	protected void setName(String name) {
		this.name = name;
	}
		
	public Boolean isVariationPoint() {
		return isVariationPoint;
	}

	private void setIsVariationPoint(Boolean isVariationPoint) {
		this.isVariationPoint = isVariationPoint;
	}

	public VariantType getVariantType() {
		return variantType;
	}

	private void setVariantType(VariantType variantType) {
		this.variantType = variantType;
	}
	
	public Collection<Concern> getConcerns() {
		return concerns;
	}
	
	public Collection<Feature> getFeatures() {
		return features.values();
	}
	
	public boolean containsConcern(Concern concern){
		return concerns.contains(concern);
	}
	
	public boolean containsFeature(String featureName){
		for (Feature feature : features.values()) {
			if (feature.getName().equalsIgnoreCase(featureName))
				return true;
		}
		return false;
	}

	public Architecture getArchitecture() {
		return architecture;
	}

	private void setArchitecture(Architecture architecture) {
		this.architecture = architecture;
	}
	
	public void addConcern(String concernName) {
		concerns.add(architecture.getOrCreateConcernByName(concernName));
	}
	
	public void removeConcern(String concernName) {
		concerns.remove(architecture.getOrCreateConcernByName(concernName));
	}

	@Override
	public String toString() {
		return getName();
	}
}