package net.fdms.Action 
{
	import flash.display.DisplayObject;
	import flash.utils.Timer;
	import net.fdms.Resource.Resource;
	import com.greensock.TweenMax;
	import net.fdms.Function.Align.AlignSets;
	
	/**
	 * ...
	 * @author Dawnboy
	 */
	public class ResStatus 
	{
		private static var exp:RegExp = /\-\-|\+\+/;
		private static var check:RegExp = new RegExp(exp);

		public function ResStatus() 
		{
			
		}
		
		public static function change(target:*, newStatus:Object):void {
			var foo:DisplayObject = Resource.getObject(target);	
			var config:Object = Resource.getConfig(target);
			
			if (foo) {
				var symbol:String;
				var tX:Number, tY:Number, tAlpha:Number, tScale:Number;
				//------------------------------------------------------------------------
				if (config.alignSets) {
					var align:String = config.alignSets[0];
					if (newStatus.x && newStatus.y && align != "YT" && align != "YC" && align != "YB" && align != "XL" && align != "XC" && align != "XR") {
						AlignSets.delAlign(foo);
					}else {
						if (newStatus.x && align != "YT" && align != "YC" && align != "YB") {
							AlignSets.delAlign(foo);
						}else if (newStatus.y && align != "XL" && align != "XC" && align != "XR") {
							AlignSets.delAlign(foo);
						}
					}
					
				}
				//------------------------------------------------------------------------
				if (newStatus.x) {
					if (check.test(newStatus.x)) {
						symbol = check.exec(newStatus.x);
						switch(symbol) {
							case "--":
								tX = foo.x - Number(newStatus.x.split("--").join(""));
								break;
							case "++":
								tX = foo.x + Number(newStatus.x.split("++").join(""));
								break;
						}
					}else {
						tX = Number(newStatus.x);
					}
				}else {
					tX = foo.x;
				}
				//------------------------------------------------------------------------
				if (newStatus.y) {
					if (check.test(newStatus.y)) {
						symbol = check.exec(newStatus.y);
						switch(symbol) {
							case "--":
								tY = foo.y - Number(newStatus.y.split("--").join(""));
								break;
							case "++":
								tY = foo.y + Number(newStatus.y.split("++").join(""));
								break;
						}
					}else {
						tY = Number(newStatus.y);
					}
				}else {
					tY = foo.y;
				}
				//------------------------------------------------------------------------
				if (newStatus.alpha) {
					if (check.test(newStatus.alpha)) {
						symbol = check.exec(newStatus.alpha);
						switch(symbol) {
							case "--":
								tAlpha = foo.alpha - Number(newStatus.alpha.split("--").join(""));
								break;
							case "++":
								tAlpha = foo.alpha + Number(newStatus.alpha.split("++").join(""));
								break;
						}
					}else {
						tAlpha = Number(newStatus.alpha);
					}
				}else {
					tAlpha = foo.alpha;
				}
				//------------------------------------------------------------------------
				if (newStatus.scale) {
					if (check.test(newStatus.scale)) {
						symbol = check.exec(newStatus.scale);
						switch(symbol) {
							case "--":
								tScale = foo.scaleX - Number(newStatus.scale.split("--").join(""));
								break;
							case "++":
								tScale = foo.scaleX + Number(newStatus.scale.split("++").join(""));
								break;
						}
					}else {
						tScale = Number(newStatus.scale);
					}
				}else {
					tScale = foo.scaleX;
				}
				//------------------------------------------------------------------------
				if (newStatus["duration"]) {
					TweenMax.to(foo, Number(newStatus["duration"]), { x:tX, y:tY, alpha:tAlpha, scaleX:tScale, scaleY:tScale } );
				}else {
					foo.x = tX;
					foo.y = tY;
					foo.alpha = tAlpha;
					foo.scaleX = tScale;
					foo.scaleY = tScale;
				}
			}
		}
		
	}

}