/**
 * PlannerNotificationGenerator.java
 * Created Jul 2, 2008
 * Copyright (c) TANDBERG Television 2007-2008
 */
package com.tandbergtv.watchpoint.pmm.schedule.notify;

import static com.tandbergtv.watchpoint.pmm.entities.ContainerType.PARTNER;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Collection;

import com.tandbergtv.watchpoint.pmm.entities.IContainer;
import com.tandbergtv.watchpoint.pmm.entities.Partner;
import com.tandbergtv.watchpoint.pmm.entities.Schedule;
import com.tandbergtv.watchpoint.pmm.entities.Title;
import com.tandbergtv.watchpoint.pmm.entities.TitleStatus;
import com.tandbergtv.watchpoint.pmm.partner.IPartnerManagement;
import com.tandbergtv.watchpoint.pmm.partner.PartnerManager;
import com.tandbergtv.watchpoint.pmm.schedule.ScheduleRuntimeException;
import com.tandbergtv.workflow.core.service.ServiceRegistry;
import com.tandbergtv.workflow.core.service.cache.ICacheService;

/**
 * Generates notifications for a planner
 * 
 * @author Sahil Verma
 */
public class PlannerNotificationGenerator extends AbstractNotificationGenerator {

	/* (non-Javadoc)
	 * @see com.tandbergtv.watchpoint.pmm.schedule.notify.AbstractNotificationGenerator#getType()
	 */
	protected String getType() {
		return "planner";
	}

	/* (non-Javadoc)
	 * @see com.tandbergtv.watchpoint.pmm.schedule.notify.AbstractNotificationGenerator#addMessage(com.tandbergtv.watchpoint.pmm.schedule.notify.Notification, java.lang.String, java.util.Collection)
	 */
	protected void addMessage(Notification notification, String status, Collection<Title> titles) {
		String message = super.constructMessage(status, titles);
		Schedule schedule = notification.getSchedule();
		String date = new SimpleDateFormat("yyyy-MM-dd").format(schedule.getDate());
		String source = getSource(schedule);
		
		notification.addMessage("Expected assets for planner dated " + date + " from " + source + " are late: " + message);
	}
	
	@SuppressWarnings("unchecked")
	private String getSource(Schedule schedule) {
		long partnerId = schedule.getSourcePartnerID().longValue();
		ICacheService<IContainer> cache = 
			(ICacheService<IContainer>)ServiceRegistry.getDefault().lookup("Container Cache");
		
		if (cache != null) {
			for (Serializable key : cache.getKeys()) {
				IContainer container = cache.get(key);
				long id = container.getContainerId();
				
				if (container.getContainerType() == PARTNER && id == partnerId)
					return container.getContainerName();
			}
		}
		
		/* Cache miss...royal PITA */
		IPartnerManagement pm = PartnerManager.getInstance();
		Partner partner = pm.getPartner(partnerId);
		
		if (partner != null)
			return partner.getName();
		
		throw new ScheduleRuntimeException("Cannot find partner " + partnerId);
	}

	/* (non-Javadoc)
	 * @see com.tandbergtv.watchpoint.pmm.schedule.notify.AbstractNotificationGenerator#isTitleDelayed(com.tandbergtv.watchpoint.pmm.entities.Title, java.lang.String)
	 */
	@Override
	protected boolean isTitleDelayed(Title title, Schedule schedule, String status) {
		// the status currently checked is 'READY' or 'APPROVED'
		return !(title.getStatus() == TitleStatus.READY || title.getStatus() == TitleStatus.APPROVED);
	}
}
