package net.fdms.Action 
{
	import com.greensock.loading.core.LoaderCore;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;
	import net.fdms.Frame.Frame;
	import net.fdms.Frame.Space;
	import net.fdms.Frame.StageSets;
	import net.fdms.Function.Align.AlignSets;
	import net.fdms.Function.Convert.StringToPoint;
	import net.fdms.Resource.Resource;
	import flash.events.MouseEvent;
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;

	
	/**
	 * 2011/7/11 17:07
	 * @author Dawnboy
	 */
	public class ResControl 
	{
		public static var clearQueue:Array = [];
		public function ResControl() 
		{
			
		}
		
		public static function showRes(target:*):void {			
			//定义临时数据及控制对象
			var foo:DisplayObject = Resource.getObject(target);			//获取控制对象
			var config:Object = Resource.getConfig(target);				//获取对象配置信息
			var status:Object = { alpha:1, scale:1 };					//获取对象实际显示信息

			//初始化属性,加入至场景
			foo.alpha = 0;
			
			Resource.show(target);
			if (config.status) {
				status.alpha = (config.status)?Number(config.status[0]):1;
				status.scale = (config.status)?Number(config.status[1]):1;
				foo.alpha = status.alpha;
				foo.scaleX = status.scale;
				foo.scaleY = status.scale;
			}else {
				foo.alpha = 1;
			}

			//确定对齐方式
			if (config.alignSets) {
				AlignSets.addAlignRes(foo, config);
			}else {
				if (config.reset) {
					foo.x = config.reset[0];
					foo.y = config.reset[1];
				}
			}
			//清除当前对象移动动作
			if (config.TargetPoint) {
				TweenLite.killTweensOf(foo, false, { x:true, y:true } );
				foo.x = config.TargetPoint.x;
				foo.y = config.TargetPoint.y;
			}
			
			
			//设置对象显示方式
			if (config.loadSets) {
				var fromPoint:Point = StringToPoint.getPoint(foo, config.loadSets[2].toString(), config.reset);
				TweenLite.from(foo, Number(config.loadSets[0]), { x:fromPoint.x, y:fromPoint.y, alpha:0, ease:EaseLib.getEase(int(config.loadSets[0]) ),onComplete:onShowInit } );
				//foo.alpha = status.alpha;
			}else {
				foo.alpha = status.alpha;
				//MouseAction.MouseActionOn();
			}
			
			//添加对象按钮控制
			if (config.btnAction) {
				MouseAction.addBtnAction(config.id, config.btnAction);
			}
			if (config.overAction) {
				MouseAction.addBtnAction(config.id, config.overAction,1);
			}
			if (config.outAction) {
				MouseAction.addBtnAction(config.id, config.outAction,2);
			}
			
			
			//添加对象空间
			if (config.space) {
				Space.addSpace(config.space, foo);
			}
			
			try {
				var parent:DisplayObject = Resource.getParent(foo);
				var pconfig:Object = Resource.getConfig(parent);
				Resource.setLayer(parent, pconfig.layer);
			}catch (e:*) {
				
			}
			Frame.getContainer(config.id).addChild(foo);
			//重置层叠关系
			if (config.layer) Resource.setLayer(target, config.layer);
			
		}
		
		private static function onShowInit():void {
			MouseAction.MouseActionOn();
		}
		
		/**
		 * 移除对象
		 * @param	target
		 */
		public static function removeRes(target:*, afterFun:Array = null):Number {
			//MouseAction.MouseActionOff();
			//定义临时数据及控制对象
			var duration:Number = 0;
			var foo:DisplayObject = Resource.getObject(target);			//获取控制对象
			var config:Object = Resource.getConfig(target);				//获取对象配置信息
			//trace(config.unloadSets)
			//移除对象按钮控制
			if (config.btnAction) {
				MouseAction.delMouseAction(target);
			}
			if (config.overAction) {
				MouseAction.delMouseAction(target,1);
			}
			if (config.outAction) {
				MouseAction.delMouseAction(target,2);
			}
			//移除对齐方式
			if (config.alignSets) AlignSets.delAlign(foo);
			
			
			
			if (config.unloadAction) {
				Action.addAction(config.unloadAction);
				Action.start(null);
			}
			
			//移除对象显示方式
			if (config.unloadSets) {
				var fromPoint:Point = StringToPoint.getPoint(foo, config.unloadSets[2], config.reset);
				duration =  Number(config.unloadSets[0]);
				TweenLite.to(foo,duration, { x:fromPoint.x, y:fromPoint.y, alpha:0, onComplete:clearRes, onCompleteParams:[config.id, afterFun], ease:EaseLib.getEase(int(config.unloadSets[1])) } );
				//hideRes(config.id, fromPoint, Number(config.unloadSets[0]), afterFun);
				//foo.alpha = 0;
				//clearRes(config.id,afterFun);
			}else {
				foo.alpha = 0;
				clearRes(config.id,afterFun);
			}
			if (Resource.hasChild(config.id)) {
				var ResChild:Array = Resource.getChild(config.id);
				
				var n:int = ResChild.length;
				for (var a:int = 0; a < n; a++) {
					removeRes(ResChild[a].toString());
				}
			}
			if (config.space) Space.clearSpace(config.space);
			return duration;
		}
		
		/**
		 * RemoveChild
		 * @param	target
		 */
		private static function clearRes(target:*, afterFun:Array = null):void {
			Resource.clear(target);
			Resource.setLoader(target, null);
			Resource.setObject(target, null);
			if (afterFun) {
				afterFun[0](afterFun[1]);
			}
			Resource.unShow(target);
			
		}
		
		private static function hideRes(ResName:String, targetPoint:Point = null, duration:Number = 1, afterFun:Array = null):void {
			var res:DisplayObject = Resource.getObject(ResName);
			var timer:Timer = new Timer(1);
			var times:Number =  duration * 1000
			var oX:Number = res.x;
			var oY:Number = res.y;
			var dX:Number, dY:Number;
			var tA:Number = res.alpha / times;
			if (targetPoint) {
				dX = (targetPoint.x - oX) / times;
				dY = (targetPoint.y - oY) / times;
			}else {
				dX = 0;
				dY = 0;
			}
			timer.addEventListener(TimerEvent.TIMER, hide);
			timer.start();
			function hide():void {
				res.alpha = res.alpha-tA;
				if (targetPoint) {
					res.x += dX;
					res.y += dY;
				}
				if (res.alpha <= 0) {
					res.alpha = 0;
					timer.removeEventListener(TimerEvent.TIMER, hide);
					timer = null;
					clearRes(res, afterFun);
					
				}
			}
			
			
		}
	}

}