package org.wontovlab.model;

import java.util.ArrayList;
import java.util.List;

import org.wontovlab.model.TO.MaterialTO;
import org.wontovlab.model.TO.PropertyTO;


public class Material {
	private String name;
	private List<Property> properties;
	public String ontologyReference;
	
	
	public Material() {
		
	}
	
	
	public Material(MaterialTO materialTO) {
		this.name = materialTO.getName();
		this.ontologyReference = materialTO.getOntologyReference();
		
		if (materialTO.getProperties() != null) {
			this.properties = new ArrayList<Property>();
			for (PropertyTO property: materialTO.getProperties()) {
				this.properties.add(new Property(property));	
			}
		}
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public List<Property> getProperties() {
		return properties;
	}
	public void setProperties(List<Property> properties) {
		this.properties = properties;
	}
	
	public String getOntologyReference() {
		return ontologyReference;
	}
	public void setOntologyReference(String ontologyReference) {
		this.ontologyReference = ontologyReference;
	}
	
	@Override
	public boolean equals(Object obj) {

		if (obj == null && this.ontologyReference == null && this.properties == null)
			return true;
		
		if (obj == null && this.ontologyReference!= null && this.properties != null)
			return false;

/*		//solving a problem that happens when get no property, but it is not null, so set it as null
		if (((Material) obj).getProperties() != null && ((Material) obj).getProperties().size() == 0) {
			((Material) obj).setProperties(null);
		}*/
		//os properties do OBJ nao estão nulos e sim vazios. por isso está dando problemas
		//devo ver onde eles estao sendo alimentados e setar como null
		
		if (((Material) obj).getProperties() == null && this.properties == null &&
				this.ontologyReference.equals(((Material) obj).getOntologyReference()))
			return true;
		
			
			if (((Material) obj).getOntologyReference().equals(this.ontologyReference)) {
				//Verifying the method containsAll to both sides. For instance: if A containsAll B and B containsAll A, so
				//both have the same content
				if ((((Material) obj).getProperties().containsAll(this.properties)))
					if (this.properties.containsAll(((Material) obj).getProperties()))
						return true;
				
			}
		
		return false;
		
	}

}
