/////////////////////////////////////////////////////////////////////////////////
//
// 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;
	import com.panache.xml.MediaServicesXMLConstants;
	import com.panache.xml.MediaServicesXMLParser;

	public class PanacheCreativeRenditionManager extends PanacheCreativeObject
	{
		private static const DEBUG_SOURCE:String = "PanacheCreativeRenditionManager";
		
		public static const CREATIVE_WIDTH:String 		= "creative_width";
		public static const CREATIVE_HEIGHT:String 		= "creative_height";
		public static const CREATIVE_BITRATE:String 	= "creative_bitrate";
		public static const CREATIVE_VARIATION:String 	= "creative_variation";

		protected var _renditions:PanacheCreativeObject = new PanacheCreativeObject();	

		//-----------------------------------------------------------------------------------------
		public function PanacheCreativeRenditionManager()
		{
			super();
		}
		
		//-----------------------------------------------------------------------------------------
		public function getURL(renditionName:String, 
							   defaultURL:String = null, 
							   parser:MediaServicesXMLParser = null,
							   rootNode:XMLList = null, 
							   nodeName:String = null) : String
	    {
	   		if (parser != null)
	   		{
	   			if (rootNode == null || nodeName == null)
	   			{
					MediaServicesAdDebug.reportTrace("Invalid rootNode or nodeName", DEBUG_SOURCE, "getURL");
	   			}
	   			else
	   			{
					MediaServicesAdDebug.reportTrace("Rendition : " + nodeName, DEBUG_SOURCE, "getURL");
					var renditionsSelector : PanacheCreativeRenditionSelector = null;
					try
					{
						var node:XMLList = parser.getNode(rootNode, nodeName);		
						if( parser.isNodeNotEmpty(node) )
						{
							//check to see if you there are "specs" - that match the listname
							var specList:XMLList = node["url"];
							if( parser.isNodeNotEmpty(specList) )
							{
								//if you have "specs" - create the UrlSelector
								renditionsSelector = new PanacheCreativeRenditionSelector(nodeName);
								createRenditionsFromList(parser, specList, renditionsSelector);
								_renditions.setProperty(renditionName, renditionsSelector);
							}
							else
							{
								var urlString:String = node.valueOf();
								if (urlString != null && urlString.length > 0)
								{
									//if you have not empty url - create the UrlSelector
									renditionsSelector = new PanacheCreativeRenditionSelector(nodeName);
									
									var rendition:PanacheCreativeRendition = new PanacheCreativeRendition();
									rendition.url = urlString;
									renditionsSelector.addRendition(rendition);
									_renditions.setProperty(renditionName, renditionsSelector);
								}
								else
								{
									MediaServicesAdDebug.reportTrace("Empty value of node: " + nodeName, DEBUG_SOURCE, "getURL");
								}
							}
						}			
					}
					catch (ex:Error)
					{
						MediaServicesAdDebug.reportTrace("Exception. " + ex.message, DEBUG_SOURCE, "getURL");
					}	
	   			}
	   		}
	   		renditionsSelector = _renditions.getProperty(renditionName, "object");
	   		if (renditionsSelector == null)
	   		{
	   			MediaServicesAdDebug.reportTrace("RenditionName '" + renditionName + "' does not exist", DEBUG_SOURCE, "getURL");
	   			return defaultURL;
	   		}
	   		var selectionSpec:IPanacheCreativeRendition = new PanacheCreativeRendition();
	   		selectionSpec.width = getProperty(CREATIVE_WIDTH, "number", 0);
	   		selectionSpec.height = getProperty(CREATIVE_HEIGHT, "number", 0);
	   		selectionSpec.bitrate = getProperty(CREATIVE_BITRATE, "number", 0);
	   		selectionSpec.variationName = getProperty(CREATIVE_VARIATION, "string");
	   		var rend:IPanacheCreativeRendition = renditionsSelector.select(selectionSpec);
	   		if (rend == null)
	   		{
	   			MediaServicesAdDebug.reportTrace("Failed to select rendition from recndition selector", DEBUG_SOURCE, "getURL");
	   			return defaultURL;
	   		}
	   		return rend.url;
	    }

		//-----------------------------------------------------------------------------------------
		protected function createRenditionsFromList(parser:MediaServicesXMLParser, 
													urlList:XMLList, 
										 			renditionsSelector:PanacheCreativeRenditionSelector) : void
		{
			MediaServicesAdDebug.reportTrace(" Create renditions. ", DEBUG_SOURCE, "createRenditionsFromList");
			if( urlList == null )
			{
				MediaServicesAdDebug.reportTrace("Invalid URL list. ", DEBUG_SOURCE, "createRenditionsFromList");
				return;
			}
			// loop through the xml and extract the rendition
			for each (var urlNode:XML in urlList)
			{
				var urlString:String = parser.getNodeValue(urlNode, "String", null) as String;
				if( urlString != null && urlString.length > 0 )
				{
					var rendition:PanacheCreativeRendition = new PanacheCreativeRendition();				
					rendition.url 			= urlString;
					rendition.width 		= parser.getAttributeValue(urlNode, MediaServicesXMLConstants.FIELD_URL_WIDTH, "Number", 0);
					rendition.height		= parser.getAttributeValue(urlNode, MediaServicesXMLConstants.FIELD_URL_HEIGHT, "Number", 0);
					rendition.bitrate		= parser.getAttributeValue(urlNode, MediaServicesXMLConstants.FIELD_URL_BITRATE, "Number", 0);
					rendition.variationName = parser.getAttributeValue(urlNode, MediaServicesXMLConstants.FIELD_URL_VARIATION, "String");	
					renditionsSelector.addRendition(rendition);
				}
			}	
			// make a second pass to make sure there are no orphan url specs where other specs
			// with the same key (width, height, bitrate) have named variations and some are
			// missing a named variation
			renditionsSelector.validateVariations();
		}
	}
}