import java.util.*;

/**
 * This class represents a segmentation in items of video file
 * and used in known-items task programs of TREC 2001.
 * Not the entire video is represented in a segmentation, the term of segmentation
 * is used for convenience, but it's just a bunch of items of the same video
 * that need to be processed together cause result of one particular system 
 * for one particular topic.
 * 
 * @author NIST-ITL-IAD-Retrieval Group
 * This software was produced by NIST, an agency of the
 * U.S. government, and by statute is not subject to copyright in the
 * United States. Recipients of this software assume all
 * responsibilities associated with its operation, modification and
 * maintenance.
 *
 */
public class RefSegmentation extends Segmentation{
    /**
     * Constructs Segmentation object with an ordering comparator for its set of items.
     *
     * @param     orderingComparator The comparator used to order transitions in the set of transitions
     * @param     topicNumber The topic number of the results.
     */
	public RefSegmentation(Comparator orderingComparator, String topicNumber){
		this.itemSet = new TreeSet(orderingComparator);
		this.topicNumber = topicNumber;
	}	
    /**
     * Process the comparison between the current assumed reference segmentation and the given system segmentation.
     *
     * @param     systemSegmentation The system segmentation to be compared with.
     * @return    an object representing all the results (measures...) of the comparison.
     * @exception Exception if the definition of an item is not correct.
     */	
	public comparisonResult processComparison(SysSegmentation systemSegmentation, int maxAllowedItems) throws Exception{
	
		OverlapComparator overlap = OverlapComparator.getInstance();

		int matchingRefItem = 0;
		int matchingSysItem = 0;
		String nl = System.getProperty("line.separator");
		String matchingSection = nl;
		String notRetrievedKnownItemsSection = nl+"<notMatchedKnownItems>"+nl;

		// Administration for computing MAP
		double meanAvgPrec = 0.0;
		double avgPrec = 0.0;
		int nSegSoFar = 0;

		//For each item within the systemSegmentation
		//detection of insertion or match.
		Iterator sysSegIterator = systemSegmentation.getElements();
		while(sysSegIterator.hasNext()){

			SystemItem sysItem = (SystemItem) sysSegIterator.next();
			nSegSoFar++;
						
			Iterator refSegIterator = this.getElements();
			while(refSegIterator.hasNext()){

				KnownItem temp = (KnownItem) refSegIterator.next();
				//RMIN RMAX
				if(temp.intersected(sysItem) && overlap.compare(temp, sysItem) == 0){
					if (sysItem.matched == 0) {
						matchingSysItem++;
						matchingSection += sysItem.toStringOpen();
					}
					temp.matched = 1;
					sysItem.matched = 1;
					matchingSection += "    " + temp.toString();
				}
			}
			if(sysItem.matched == 0)
				matchingSection += sysItem.toString();
			else{
				avgPrec += ((double) matchingSysItem) / (double) nSegSoFar;
				//System.out.println(matchingSysItem+" "+nSegSoFar);
				matchingSection += sysItem.toStringClose();
			}			
		}
		if ( nSegSoFar > 0 ) meanAvgPrec = avgPrec / (double) this.getItemCount();
		//System.out.println("meanAvgPrec " + meanAvgPrec+" = avgPrec "+avgPrec + " / getItemCount "+this.getItemCount());
		
		//For each item within the refSegmentation
		//detection of DELETION with MATCHED attribute in item objects
		Iterator m = this.getElements();
		
		while(m.hasNext()){
			Item t = (Item) m.next();
			if(t.matched == 0){
				notRetrievedKnownItemsSection += "    " + t.toString();
			}
			else
				matchingRefItem++;
		}
		notRetrievedKnownItemsSection += "</notMatchedKnownItems>"+nl;

		comparisonResult result = new comparisonResult(systemSegmentation);
		result.setResults(this.getItemCount(), matchingRefItem, matchingSysItem,
						  matchingSection, notRetrievedKnownItemsSection,
						  overlap.getPRef(), overlap.getPSys(),
				          meanAvgPrec, maxAllowedItems);
		return result;
	}
}
