/////////////////////////////////////////////////////////////////////////////////
//
// c) 2012 24/7 Real Media - A WPP Company  
// This source code is Confidential and Proprietary information of the company.
//
/////////////////////////////////////////////////////////////////////////////////
package com.panache.assets.renditions
{
	import com.panache.core.PanacheCreativeObject;
	import com.panache.debug.MediaServicesAdDebug;
	
	/**
	 * PanacheCreativeRendition is a class which represents the panache rendition information.
	 * 
	 * <p>
	 *This interfaces extends IPanacheObject.
	 * </p>
	 * 
 	 * @see		com.panache.core.PanacheCreativeObject					PanacheCreativeObject
     * @see 	com.panache.assets.renditions.IPanacheCreativeRendition	IPanacheCreativeRendition
	 */
	public class PanacheCreativeRendition extends PanacheCreativeObject implements IPanacheCreativeRendition
	{
		// CONSTANTS
		private static const DEBUG_SOURCE:String		= "PanacheCreativeRendition";
		
		//////////////////////////////////////////////////
		// PROTECTED VARIABLES
		protected var _name						: String 	= "";
		protected var _url						: String 	= "";
		protected var _width					: Number 	= undefined;
		protected var _height					: Number 	= undefined;
		protected var _bitrate					: Number 	= undefined;
		protected var _key						: String 	= null;
		
		protected var _additionalProperties		: Object 	= new Object();
		
		/**
		 * Creates a PanacheCreativeRendition object.
		 */
		public function PanacheCreativeRendition()
		{
			super();
		}

		/**
		 * variationName provides the variation name of the rendition.
		 */
		public function get variationName() : String
		{
			return _name;
		}
		public function set variationName( value:String ) : void
		{
			_name = value;
		}

		/**
		 * This function gets key - used to store and look up variations for a specific "rendition" spec.
		 * A key is typically a concantenation of the width, height and bitrate.
		 */
		public function get key() : String
		{
			if (_key == null)
				_key = createRenditionKey();
			return _key;
		}

		/**
		 * url provides the url for the rendition.
		 */
		public function get url() : String
		{
			return _url;
		}
		public function set url( value:String ) : void
		{
			_url = value;
		}

		/**
		 * width provides the width parameter for the rendition.
		 */
		public function get width() : Number
		{
			return _width;
		}
		public function set width( value:Number ) : void
		{
			_width = value;
		}

		/**
		 * height provides the height parameter for the rendition.
		 */
		public function get height() : Number
		{
			return _height;
		}
		public function set height(  value:Number ) : void
		{
			_height = value;
		}

		/**
		 * bitrate provides the bitrate parameter for the rendition.
		 */
		public function get bitrate () : Number
		{
			return _bitrate;
		}
		public function set bitrate( value:Number ) : void
		{
			_bitrate = value;
		}

		/**
		 * This method gets any additional property of the rendition (used by additional filters during rendition selection).
		 * 
		 * @param	propertyName	the property name.
		 * @param	type			the property type.
		 * @param	defaultValue	the default value if property does not exist (default to null).
		 * 
		 * @return	the property value or the default value if property does not exist.
		 */
		public function getRenditionProperty( propertyName:String, type:String, defaultValue:Object=null ) : Object
		{
			if (hasRenditionProperty(propertyName))
			{
				var o:Object = _additionalProperties[propertyName];
				var sType:String = type.toLowerCase();
				switch(sType)
				{
					case "object":
						return o;
					case "boolean":
						return Boolean(o);
					case "number":
						return Number(o);
					case "string":
						return o.toString();
					case "xml":
						return XML(o);
				}
				return defaultValue;
			}
			else
			{
				return defaultValue;
			}
		}
		/**
		 * This method sets any additional property of the rendition (used by additional filters during rendition selection).
		 * 
		 * @param	propertyName	the property name.
		 * @param	value			the value of the property.
		 */
		public function setRenditionProperty( propertyName:String, value:Object ) : void
		{
			_additionalProperties[propertyName] = value;
		}
		/**
		 * This method checks if additional property exists.
		 * 
		 * @param	propertyName	the property name.
		 * 
		 * @return	true if the property exists.
		 */
		public function hasRenditionProperty( propertyName:String ) : Boolean
		{
			return _additionalProperties.hasOwnProperty(propertyName);
		}

		/**
		 * This method returns true if a url has been specified.
		 * 
		 * @return <code>true</code> if it has a URL; <code>false</code>, otherwise.
		 */
		public function hasUrl() : Boolean
		{
			return _url != null && _url.length > 0;
		}

		/**
		 * This method returns true if there are NO parameters specified for the rendition.
		 * 
		 * @return <code>true</code> if it has no parameters; <code>false</code>, otherwise.
		 */
		public function hasNoParameters() : Boolean
		{
			return (isNaN(_width) && isNaN(_height) && isNaN(_bitrate)) ;	
		}

		/**
		 * This method returns true if width or height as been specified.
		 * 
		 * @return	<code>true</code> if it has size spec; <code>false</code>, otherwise.
		 */
		public function hasSizeSpec() : Boolean
		{
			return ((!isNaN(_width)) || (!isNaN(_height)));
		}

		/**
		 * This method returns true if the rendition specifies a target bitrate.
		 * 
		 * @return <code>true</code> if it has bitrate specs; <code>false</code>, otherwise.
		 */
		public function hasBitrateSpec() : Boolean
		{
			return (!isNaN(_bitrate));
		}

		/**
		 * This method returns true if the rendition consists solely of a url without any extra selection parameters.
		 * 
		 * @return <code>true</code> if this object has a URL and no parameters; <code>false</code>, otherwise.
		 */
		public function isSimpleRendition() : Boolean
		{
			return hasUrl() && hasNoParameters();
		}

		/**
		 * This method returns a string representation of the rendition.
		 * 
		 * @return	<code>String</code> the information of this class.
		 */
		override public function toString() : String
		{
			return "PanacheRendition ["
				+ _url
				+ " w=" + _width
				+ " h=" + _height
				+ " bitrate=" + _bitrate
				+ "]";
		}
		
		/**
		 * This function returns a rendition key as string.
		 * 
		 * <p>
		 * A rendition key is a concantenation of the width, height and bitrate
		 * and looks like:  SpecW340>H240>B500>
		 * if there is no bitrate, then the key would like: SpecW340>H240>B>
		 * </p>
		 * 
		 * @return	<code>String</code> the redition key.
		 * 
		 */
		protected function createRenditionKey() : String
		{
			var key:String = "SpecW"
			if (!isNaN(_width))
				key += _width;
				
			key += ">H";
			if (!isNaN(_height))
				key += _height;
				
			key += ">B";
			if (!isNaN(_bitrate))
				key += _bitrate;
				
			key += ">";
			
			return key;
		}
	}
}