/**
 * File.java
 * Created Feb 23, 2009
 * Copyright (c) Tandberg Television 2009
 */
package com.tandbergtv.metadatamanager.model;

import static com.tandbergtv.metadatamanager.model.FieldName.TYPE;
import static com.tandbergtv.metadatamanager.model.FileType.ORIGINAL;

import java.util.ArrayList;

/**
 * Represents a file asset that has actual physical bits
 * 
 * @author Sahil Verma
 */
public class File extends Item {
	
	/**
	 * Ctor
	 */
	public File() {
		super();
		this.setType(ItemType.FILE);
	}
	
	/* (non-Javadoc)
	 * @see com.tandbergtv.metadatamanager.model.Asset#addField(com.tandbergtv.metadatamanager.model.Field)
	 */
	@Override
	public void addField(Field field) {
		/* SIGH... */
		if (field.getIndices() == null || field.getIndices().size() == 0) {
			String [] a = field.getTtvXPath().split("/");
			
			if (a.length == 0)
				throw new IllegalArgumentException("Field (" + field + ") has invalid name");
			
			ArrayList<Integer> indices = new ArrayList<Integer>();
			
			for(String s : a) {
				if(!s.equals("") && !s.startsWith("@")) {
					indices.add(new Integer(1)); // Files have at-most-one value of a property
				}
			}
			
			field.setIndices(indices);
		}
		
		super.addField(field);
	}

	/**
	 * Determines whether this file is an original or not
	 * @return
	 */
	public boolean isOriginal() {
		Field field = this.getFirstField(TYPE.toString());
		
		if (field != null)
			return ORIGINAL.toString().equals(field.getValue());
		
		return false;
	}
	
	/**
	 * Sets the fileType
	 * 
	 * @param original
	 */
	public void setFileType(FileType fileType) {
		Field field  = getFirstField(TYPE.toString());
		
		if (field == null) {
			field = new Field(TYPE.toString(), null);
			addField(field);
		}
		
		field.setValue(fileType.toString());

	}
	
	/**
	 * Returns the URL of this file
	 * 
	 * @return
	 */
	public String getURL() {
		return (String) getFieldValue(FieldName.FILE_URL.toString());
	}
	
	/**
	 * Gets the file name
	 * 
	 * @return
	 */
	public String getName() {
		return (String) getFieldValue(FieldName.FILE_NAME.toString());
	}
	
	/**
	 * Returns the MIME type of this file
	 * 
	 * @return
	 */
	public String getMIMEType() {
		return (String) getFieldValue(FieldName.MIME_TYPE.toString());
	}
	
	/**
	 * Gets the resolution
	 * 
	 * @return
	 */
	public String getResolution() {
		return (String) getFieldValue(FieldName.RESOLUTION.toString());
	}
	
	/**
	 * Returns the size of this file in bytes
	 * 
	 * @return
	 */
	public int getSize() {
		Object value = getFieldValue(FieldName.SIZE.toString());
		
		if (value == null)
			return 0;
		
		return (Integer) value;
	}
	
	/**
	 * Returns the width. Valid only if this is a picture or a series of pictures
	 * 
	 * @return
	 */
	public int getWidth() {
		Object value = getFieldValue(FieldName.WIDTH.toString());
		
		if (value == null)
			return 0;
		
		return (Integer) value;
	}
	
	/**
	 * Returns the height. Valid only if this is a picture or a series of pictures
	 * 
	 * @return
	 */
	public int getHeight() {
		Object value = getFieldValue(FieldName.HEIGHT.toString());
		
		if (value == null)
			return 0;
		
		return (Integer) value;
	}
	
	/**
	 * Returns the duration
	 * 
	 * @return
	 */
	public String getDuration() {
		return (String) getFieldValue(FieldName.DURATION.toString());
	}
	
	/**
	 * Returns the bitrate
	 * 
	 * @return
	 */
	public String getBitrate() {
		return (String) getFieldValue(FieldName.BITRATE.toString());
	}
	
	/**
	 * Returns the aspectratio
	 *
	 * @return
	 */
	public String getAspectratio() {
		return (String) getFieldValue(FieldName.ASPECTRATIO.toString());
	}
	
	private Object getFieldValue(String path) {
		Field field = getFirstField(path);
		
		if (field != null)
			return field.getTypedValue();
		
		return null;
	}
}
