/**
 * GetOriginalFileInfoMessageHandler.java
 * Created on Apr 10, 2009
 * (C) Copyright TANDBERG Television Ltd.
 */
package com.tandbergtv.watchpoint.contentmgmt.communication.handlers;

import java.util.List;

import com.tandbergtv.cms.portal.util.transaction.Transactional;
import com.tandbergtv.metadatamanager.model.Asset;
import com.tandbergtv.watchpoint.communication.Util;
import com.tandbergtv.watchpoint.pmm.communication.HandlerErrorCode;
import com.tandbergtv.watchpoint.pmm.communication.MessageHandler;
import com.tandbergtv.watchpoint.pmm.communication.MessageHandlerException;
import com.tandbergtv.watchpoint.pmm.entities.Title;
import com.tandbergtv.watchpoint.pmm.title.ITitlePersistenceService;
import com.tandbergtv.workflow.core.service.ServiceRegistry;
import com.tandbergtv.workflow.message.IMessageKey;
import com.tandbergtv.workflow.message.IMessageUID;
import com.tandbergtv.workflow.message.WorkflowMessage;
import com.tandbergtv.workflow.message.WorkflowMessage.MessageType;

/**
 * @author Vlada Jakobac
 *
 */
public class GetOriginalFileInfoMessageHandler implements MessageHandler {

	private static final String DURATION = "duration";
	private static final String RESOLUTION = "resolution";
	private static final String MIME_TYPE = "mimeType";
	private static final String FILE_SIZE = "fileSize";
	private static final String FILE_NAME = "fileName";
	private static final String FILE_URI = "fileUri";
	private static final String TITLE_ID = "titleId";
	private static final String ASSET_ID = "assetIdLong";

	/* (non-Javadoc)
	 * @see com.tandbergtv.watchpoint.pmm.communication.MessageHandler#handleMessage(com.tandbergtv.workflow.message.WorkflowMessage)
	 */
	@Override
	@Transactional
	public WorkflowMessage handleMessage(WorkflowMessage message)
			throws Exception {
		
		Util.validateRequired(message, TITLE_ID, ASSET_ID);
		
		/* Get the title Id */
		Long titleId = Util.getLongValue(message, TITLE_ID);

		/* Get the Service Registry to allow fetching of the title */
		ServiceRegistry registry = ServiceRegistry.getDefault();
		ITitlePersistenceService service = registry.lookup(ITitlePersistenceService.class);

		/* Get the Title using the Service */
		Title title = null;
		try {
			title = service.get(titleId);
		} catch (Exception e) {
			String msg = "Failed to read the Title from the persistence service, error: "
					+ e.getMessage();
			throw new MessageHandlerException(HandlerErrorCode.OBJECT_NOT_PRESENT, msg, e);
		}

		if (title == null) {
			String msg = "Did not get a Title from the persistence service.";
			throw new MessageHandlerException(HandlerErrorCode.OBJECT_NOT_PRESENT, msg);
		}

		/* Get the asset Id */
		Long assetId = Util.getLongValue(message, ASSET_ID);
		
		/* Get the matching asset from the title */
		Asset groupAsset = title.getAsset();
		if (groupAsset == null) {
			String msg = "Did not get a group object for a given title.";
			throw new MessageHandlerException(HandlerErrorCode.OBJECT_NOT_PRESENT, msg);
		}
		//TODO - need to get all relations to recursively search for all assets; include group asset in search too
		Asset targetAsset = groupAsset.getAsset(assetId);
		if (targetAsset == null) {
			String msg = "Did not get an item object for a given assetId.";
			throw new MessageHandlerException(HandlerErrorCode.OBJECT_NOT_PRESENT, msg);
		}
		//find the original file with the given assetId
		com.tandbergtv.metadatamanager.model.File originalFile = null;
		List<com.tandbergtv.metadatamanager.model.File> files =
			targetAsset.getChildrenOfType(com.tandbergtv.metadatamanager.model.File.class);
		if(files != null) {			
			for(com.tandbergtv.metadatamanager.model.File file : files) {
				if(file.isOriginal()) {
					originalFile = file;
					break;
				}
			}			
		}
		if (originalFile == null) {
			String msg = "Could not find an original file associated with the given assetId.";
			throw new MessageHandlerException(HandlerErrorCode.OBJECT_NOT_PRESENT, msg);
		}
		
		/* Build the response Workflow Message */
		IMessageUID uid = message.getMessageUID();
		IMessageKey key = message.getKey();
		WorkflowMessage response = new WorkflowMessage(uid, key, MessageType.ack); 
		String fileUri = (originalFile.getURL() != null) ? originalFile.getURL() : "";
		String fileName = (originalFile.getName() != null) ? originalFile.getName() : "";
		int fileSize = originalFile.getSize();
		String mimeType = (originalFile.getMIMEType() != null) ? originalFile.getMIMEType() : "";
		String resolution = (originalFile.getResolution() != null) ? originalFile.getResolution() : "";
		String duration = (originalFile.getDuration() != null) ? originalFile.getDuration() : "";
		
		response.putValue(FILE_URI, fileUri);
		response.putValue(FILE_NAME, fileName);
		response.putValue(FILE_SIZE, Integer.toString(fileSize));
		response.putValue(MIME_TYPE, mimeType);
		response.putValue(RESOLUTION, resolution);
		response.putValue(DURATION, duration);
		
		return response;

		
		
	}

}
