/////////////////////////////////////////////////////////////////////////////////
//
// 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.debug.MediaServicesAdDebug;
	import com.panache.core.PanacheCreativeConstants;

 	/**
    * CreativeRenditionVariationManager is the class that manages rendition variations.
    * This class should be used for selection rendition by the rendition selector.
    **/

	public class PanacheCreativeRenditionVariationManager
	{
		private static const DEBUG_SOURCE:String = "PanacheCreativeRenditionVariationManager";	

		private var _sName:String = "";
		private var _variations:Object;				// holds the variation for each rendition 

		/**
		 * Creates a RenditionVariationManager object.
		 * 
		 * param	name	the xml node name represents the renditions
		 */
		public function PanacheCreativeRenditionVariationManager(name:String)
		{
			_sName = name;
			_variations = new Object();
		}

		////////////////////////////////////////////////////////////
		// MANAGING VARIATION LIST
		/**
		 * This function adds a rendition to the internal variation list.
		 * 
		 * param	rendition	a rendition
		 */
		public function addVariation(rendition:IPanacheCreativeRendition) : void
		{
			if (rendition != null)
			{
				// check to see if that key already present in the varitions array
				var variationList:Array = getVariationsForRendition(rendition);
				// if it is not present, then create the array and add it to the object
				if (variationList == null)
				{
					variationList = new Array();
					_variations[rendition.key] = variationList;
				}
				
				// now add the url spec to the list
				MediaServicesAdDebug.reportTrace("for " + rendition.key + " rendition=" + rendition.toString(), DEBUG_SOURCE, "addVariation");
				variationList.push(rendition);	
			}
		}
		
		private function getVariationsForRendition(rendition:IPanacheCreativeRendition) : Array
		{
			// get the rendition key
			var key:String = rendition.key;
			// check to see if that key already present in the varitions array
			return (_variations[key]) as Array;
		}
		
		/**
		 * This function adds the orphan rendition to the internal variation list.
		 * 
		 * param	rendition	a rendition
		 */
		public function addOrphanVariation(rendition:IPanacheCreativeRendition) : void
		{
			// get variations for the rendition
			var n:int = 0;
			var variationList:Array = getVariationsForRendition(rendition);
			if (variationList != null)
			{
				// auto generate a name
				n = variationList.length;
			}
			
			rendition.variationName = "AutoGenVariation<" + _sName + ">" + n;
			rendition.setStatus(PanacheCreativeConstants.STATUS_ORPHAN_VARIATION, true);
			MediaServicesAdDebug.reportTrace("creating variation name for: " + rendition.toString(), DEBUG_SOURCE, "addOrphanVariation");
			// add it to the variations
			addVariation(rendition);
		}
		
		/**
		 * This function returns true if the rendition variation is present in the internal variation list, otherwise returns false.
		 * 
		 * param	rendition	a rendition
		 */
		public function hasVariations(rendition:IPanacheCreativeRendition) : Boolean
		{
			// check to see if that key already present in the varitions array
			var variationList:Array = getVariationsForRendition(rendition);
			// return true if there is an array and it is not empty
			return ((variationList != null) && (variationList.length > 0));
		}
		
		////////////////////////////////////////////////////////////
		// VARIATION SELECTION
		/**
		 * This function returns a rendition for the given rendition variation.
		 * 
		 * param	rendition	a rendition
		 */
		public function selectVariation(rendition:IPanacheCreativeRendition) : IPanacheCreativeRendition
		{
			// by default return the renditiion passed in unless there
			// are more than one variation for the rendition
			// if there are more than one, then pick one randomly
			var answer:IPanacheCreativeRendition = rendition;
			// get variations for the rendition
			var variationList:Array = getVariationsForRendition(rendition);
			if (variationList != null)
			{
				var n:int = variationList.length;
				MediaServicesAdDebug.reportTrace("Variation Manager found " + n + " variations for " + rendition.key, DEBUG_SOURCE, "selectVariation");
				if (n > 1)
				{
					// get a random number between 0 and (n - 1)
					var ix:int = Math.round(Math.random() * (n - 1));
					rendition = variationList[ix];	
				}
			}
			
			MediaServicesAdDebug.reportTrace("randomly selected: " + rendition.toString(), DEBUG_SOURCE, "selectVariation");
			return rendition;
		}
	}
}