/**
 * Planner.java
 * Created Apr 22, 2008
 * Copyright (c) TANDBERG Television 2007-2008
 */
package com.tandbergtv.watchpoint.pmm.entities;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import com.tandbergtv.watchpoint.pmm.entities.event.AssetListEvent;
import com.tandbergtv.watchpoint.pmm.entities.event.AssetListEventType;

/**
 * A transmission planner or provider schedule is a list of titles that is received from an
 * upstream partner with an estimated arrival date.
 * 
 * @author Sahil Verma
 */
public class Planner extends Schedule {

	/**
	 * Creates a Planner
	 */
	public Planner() {
	}

	/**
	 * Creates a Planner
	 */
	public Planner(Long sourcePartnerID, Date date) {
		super(sourcePartnerID, date);
		/* FIXME Status? */
	}
	
	/**
	 * Returns the estimated date by which the assets will arrive
	 * 
	 * @return
	 */
	public Date getArrivalDate() {
		return this.date;
	}
	
	/**
	 * Sets the data on which the assets in this planner are scheduled to arrive
	 * 
	 * @param date
	 */
	public void setArrivalDate(Date date) {
		setDate(date);
	}

	/* (non-Javadoc)
	 * @see com.tandbergtv.watchpoint.pmm.entities.Schedule#getType()
	 */
	public TitleListType getType() {
		return TitleListType.PLANNER;
	}

	/* (non-Javadoc)
	 * @see com.tandbergtv.watchpoint.pmm.entities.AssetList#addTitleInternal(com.tandbergtv.watchpoint.pmm.entities.Title)
	 */
	@Override
	protected void addTitleInternal(Title title) {
		super.addTitleInternal(title);
		
		if(title.getProgressItems() != null) {
			for(ProgressItem pi : title.getProgressItems()) {
				if(pi.getAssetListId() == null) {
					addProgressItem(pi);
				}
			}
		}
	}
	
	/* (non-Javadoc)
	 * @see com.tandbergtv.watchpoint.pmm.entities.AssetList#setTitles(java.util.Collection)
	 */
	@Override
	public void setTitles(Collection<Title> titles) {
		boolean titlesAdded = false;
		boolean titlesRemoved = false;
		
		//replace matching(id) titles and add new titles
		for(Title newTitle : titles) {
			Title matchingExistingTitle = null;
			for(Title existingTitle : this.titles) {
				if(existingTitle.equals(newTitle)) {
					matchingExistingTitle = existingTitle;
					break;
				}
			}
			if(matchingExistingTitle != null) {
				this.titles.remove(matchingExistingTitle);
				this.titles.add(newTitle);
			} else {
				addTitleInternal(newTitle);
				titlesAdded = true;
			}
		}
		
		//remove existingTitles that are not in newTitles
		List<Title> titlesToBeRemoved = new ArrayList<Title>();
		for(Title existingTitle : this.titles) {
			if(!titles.contains(existingTitle)) {
				titlesToBeRemoved.add(existingTitle);
			}
		}
		if(!titlesToBeRemoved.isEmpty()) {
			titlesRemoved = true;
			for(Title t : titlesToBeRemoved) {
				removeTitleInternal(t);
			}
		}
		
		//fire events
		if(titlesAdded) {
			fireEvent(new AssetListEvent(this, this, AssetListEventType.TITLES_ADDED));
		}
		if(titlesRemoved) {
			fireEvent(new AssetListEvent(this, this, AssetListEventType.TITLES_REMOVED));
		}
	}
	
	/* (non-Javadoc)
	 * @see com.tandbergtv.watchpoint.pmm.entities.AssetList#toString()
	 */
	public String toString() {
		return super.toString() + ", arrival date " + getArrivalDate();
	}
}
