package edu.fiu.jtlex.data;

import java.util.HashSet;
import java.util.Set;



/**
 * Default implementation of {@code IText}.
 * 
 * @author arada002
 *
 */
public class Text implements IText {
	
	private final String rawText;
	private final String text;
	private final Set<ITimex> timexes;
	private final Set<ILink> links;
	private final Set<IInstance> instances;
	
	
	/**
	 * @param rawText						full article text including the tags 
	 * 										may not be <code>null</code>
	 * @param text							article text without the tags
	 * 										may not be <code>null</code>
	 * @param events						set of event objects 
	 * 										may not be empty
	 * @param timexes						set of timex objects 
	 * 										may not be empty
	 * @param links							set of link objects
	 * 										may not be empty
	 * @param instances					set of makeInstance objects
	 * 										may not be empty		
	 * 
	 * @throws NullPointerException 		if rawText or text argument is <code>null</code>
	 * @throws IllegalArgumentException		if rawText or text argument is all whitespace or an empty string
	 * 										if events, timexes, or links argument is an empty set
	 * 
	 */
	public Text(String rawText, String text, final Set<ITimex> timexes,
			final Set<ILink> links, final Set<IInstance> instances) {
		
		checkArguments(rawText, text, timexes, links, instances);
		
		this.rawText = rawText;
		this.text = text;
		this.timexes = Set.copyOf(timexes);
		this.links = Set.copyOf(links);
		this.instances = Set.copyOf(instances);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.fiu.jtlex.data.IText#getText()
	 */
	public String getText() {
		return text;
	};
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.fiu.jtlex.data.IText#getRawText()
	 */
	public String getRawText() {
		return rawText;
	};
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.fiu.jtlex.data.IText#getTimexes()
	 */
	public Set<ITimex> getTimexes() {
		return timexes;
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.fiu.jtlex.data.IText#getLinks()
	 */
	public Set<ILink> getLinks() {
		return links;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.fiu.jtlex.data.IText#getMakeInstances()
	 */
	public Set<IInstance> getInstances(){
		return instances;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.fiu.jtlex.data.IText#getRelatedInstances(IEvent event)
	 */
	public Set<IInstance> getRelatedInstances(IEvent event){
		Set<IInstance> relatedInstances  = new HashSet<IInstance>();
		for(IInstance instance : instances) {
			if (event.equals(instance.getEvent())) {
				relatedInstances.add(instance);
			}
		}
		return relatedInstances;
	}
	/**
	 * Utility method that checks if arguments passed to a constructor are valid.
	 * @param rawText			text with the tags
	 * @param text				text without tags
	 * @param links				set of links 
	 * @param instances			set of event instances
	 * @param timexes			set of timexes
	 */
	// some files do not have timexes 
	private void checkArguments(String rawText, String text, Set<ITimex> timexs, 
			Set<ILink> links, Set<IInstance> instances) {
		
		if (rawText == null) {
			throw new NullPointerException("Raw text can not be null");
		}
		rawText = rawText.trim();
		if (rawText.length() == 0) {
			throw new IllegalArgumentException("Raw text can not be empty or all whitespace");
		}
		if (text == null) {
			throw new NullPointerException("Text can not be null");
		}
		text = text.trim();
		if (text.length() == 0) {
			throw new IllegalArgumentException("Text can not be empty or all whitespace");
		}
		if(timexs.isEmpty()) {
			throw new IllegalArgumentException("No timex objects found");
		}
		if(links.isEmpty()) {
			throw new IllegalArgumentException("No link objects found");
		}
		if(instances.isEmpty()) {
			throw new IllegalArgumentException("Missing makeInstances");
		}
	}
	
	
}
