package com.guru.event
{
	import com.guru.call.ICall;
	
	public class EventCall implements ICall
	{
		private var mScope_obj:Object = null;
		private var mFunction_fn:Function = null;
		private var mParam_array:Array = null;
		private var mEvent:CusEvent = null;
		
		/**
		 * create a eventcall
		 * @param	pObj_obj	scope object
		 * @param	pFunction_fn	target function
		 * @param	... args
		 */
		public function EventCall(pObj_obj:Object,pFunction_fn:Function, ... args)
		{
			mScope_obj = pObj_obj;
			mFunction_fn = pFunction_fn;

			if ( args.length > 0 )
			{
				mParam_array = args.slice(0,args.length);
			}
			else
			{
				mParam_array = new Array();
			}
		}
		
		/**
		 * check the scope
		 * @param	pScope_obj	scope object
		 * @return	Boolean
		 */
		public function isScore(pScope_obj:Object):Boolean
		{
			return (mScope_obj === pScope_obj);
		}
		
		/**
		 * add the param
		 * @param	pValue_obj	properties of the eventcall
		 */
		public function addParam(pValue_obj:Object):void
		{
			mParam_array.push(pValue_obj);
		}
		
		public function clearParam():void
		{
			mParam_array = new Array();
		}

		public function setEvent(pEvent:CusEvent):void
		{
			mEvent = pEvent;
		}

		/**
		 * execute the eventcall
		 * @param	... args
		 */
		public function execute(... args):void
		{
			var param_array:Array = null;
			if ( mEvent == null )
			{
				param_array = mParam_array.concat();
			}
			else
			{
				param_array = new Array();
				param_array.push(mEvent);
				param_array = param_array.concat(mParam_array);
			}

			if ( args.length > 0 )
			{
				param_array = param_array.concat(args);
			}
			mFunction_fn.apply(mScope_obj,param_array);
		}
	}
}
