package net.fdms.Function.Align 
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.geom.Point;
	import net.fdms.Resource.Resource;
	import net.fdms.Frame.Frame;
	import com.greensock.TweenLite;
	import net.fdms.Action.EaseLib;
	
	/**
	 * ...
	 * @author Dawnboy
	 */
	public class AlignLib extends Sprite
	{
		
		public function AlignLib() 
		{
			
		}		
		/**
		 * 普通对齐方式
		 * @param	target
		 * @param	config
		 * @return
		 */
		public static function normalAlign(target:DisplayObject, config:Object, autoAlign:Boolean = true):Point {
			var style:int = (config.alignSets[1] != undefined)?int(config.alignSets[1]):0;
			var rX:Number = (config.reset!=undefined)?config.reset[0]:0;
			var rY:Number = (config.reset!=undefined)?config.reset[1]:0;
			var rWidth:Number = (config.reset!=undefined && config.reset[2]!=0)?config.reset[2]:target.width;
			var rHeight:Number = (config.reset != undefined && config.reset[3] != 0)?config.reset[3]:target.height;
			var Refer:* = Frame.Stage;
			var tX:Number;
			var tY:Number;
			//trace("config:" + config.id);
			//trace("rX:"+rX,"rY:"+rY,"rWidth:" + rWidth, "rHeight:" + rHeight);
			
			
			switch(config.alignSets[0]) {
				case "LC":
					tX = rX;
					tY = (Refer.stageHeight - rHeight) / 2 + rY;
					break;
				case "LB":
					tX = rX;
					tY = Refer.stageHeight - rHeight - rY;
					break;
				case "CT":
					tX = (Refer.stageWidth - rWidth) / 2 + rX;
					tY = rY;
					break;
				case "Center":
					tX = (Refer.stageWidth - rWidth) / 2 + rX;
					tY = (Refer.stageHeight - rHeight) / 2 + rY;
					break;
				case "CB":
					tX = (Refer.stageWidth - rWidth) / 2 + rX;
					tY = Refer.stageHeight - rHeight - rY;
					break;
				case "RT":
					tX = Refer.stageWidth - rWidth - rX;
					tY = rY;
					break;
				case "RC":
					tX = Refer.stageWidth - rWidth - rX;
					tY = (Refer.stageHeight - rHeight) / 2 + rY;
					break;
				case "RB":
					tX = Refer.stageWidth - rWidth - rX;
					tY = Refer.stageHeight - rHeight - rY;
					break;
				/**************************************单独改变X、Y轴位置***************************************/
				case "YT":
					tY = rY;
					break;
				case "YC":
					tY = (Refer.stageHeight - rHeight) / 2 + rY;
					break;
				case "YB":
					tY = Refer.stageHeight - rHeight - rY;
					break;
				case "XL":
					tX = rX;
					break;
				case "XC":
					tX = (Refer.stageWidth - rWidth) / 2 + rX;
					break;
				case "XR":
					tX = Refer.stageWidth - rWidth - rX;
					break;
				default:
					tX = rX;
					tY = rY;
					break;
			}
			var targetPoint:Point = new Point(tX, tY);
			config.TargetPoint = targetPoint;
			
			if (autoAlign) {
				moveTarget(target, style, targetPoint);
			}
			return targetPoint;
		}
		
		/**
		 * 特殊对齐方式
		 * @param	target
		 * @param	config
		 * @return
		 */
		public static function positionAlign(target:DisplayObject, config:Object,autoAlign:Boolean = true):Point {
			var parentTargetPoint:Point = Resource.getConfig(Resource.getParent(target)).TargetPoint;
			var tX:Number;
			var tY:Number;
			var tmpOB:Object;
			var style:int = (config.alignSets[config.alignSets.length - 1] != undefined)?int(config.alignSets[config.alignSets.length - 1]):0;
			var rWidth:Number = (config.reset!=undefined && config.reset[2]!=0)?config.reset[2]:target.width;
			var rHeight:Number = (config.reset != undefined && config.reset[3] != 0)?config.reset[3]:target.height;
			//X轴位置处理
			var rX:Number = (config.reset!=undefined)?config.reset[0]:0;
			var tmpX:Array = getTargetPoint(config.alignSets[0]);
			switch(tmpX[0]) {
				case "%":
					tmpOB = Frame.Stage;
					tX = tmpOB.stageWidth / 100 * tmpX[1] + rX;
					
				break;
				case "@":
					tmpOB = Resource.getParent(target);
					tX = Number(parentTargetPoint.x) + rX + tmpX[1];
				break;
			}
			
			//Y轴处理
			var rY:Number = (config.reset != undefined)?config.reset[1]:0;
			var tmpY:Array = getTargetPoint(config.alignSets[1]);
			switch(tmpY[0]) {
				case "%":
					tmpOB = Frame.Stage;
					tY = tmpOB.stageHeight / 100 * tmpY[1] + rY;
				break;
				case "@":
					tmpOB = Resource.getParent(target);
					tY = Number(parentTargetPoint.y) + rY + tmpY[1];
				break;
			}
			var targetPoint:Point = new Point(tX, tY);
			config.TargetPoint = targetPoint;
			
			if (autoAlign) {
				moveTarget(target, style, targetPoint);
			}
			
			return targetPoint;
			
		}
		
		/**
		 * 移动对象
		 * @param	target
		 * @param	style
		 * @param	TP
		 */
		protected static function moveTarget(target:DisplayObject, style:int, TP:Point):void {
			if (style) {
				TweenLite.killTweensOf(target, false, { x:true, y:true } );
				TweenLite.to(target, 0.5, { x:TP.x, y:TP.y, ease:EaseLib.getEase(style),overwrite:0 } );
			}else {
				target.x = TP.x;
				target.y = TP.y;
			}
		}
		
		/**
		 * 获取特殊定位类型
		 * @param	sets
		 * @return
		 */
		protected static function getTargetPoint(sets:String):Array {
			var type:Array = new Array(2);
			var n:int = sets.length;
			for (var a:int = 0; a < n; a++) {
				switch(sets.charAt(a)) {
					case "%":
						type[0] = "%";
						type[1] = Number(sets.substring(0, a));
						break;
					case "@":
						type[0] = "@";
						type[1] = Number(sets.substring(a + 1));
						break;
					default:
						break;
				}
			}
			if (type[0] != "%" && type[0] != "@") {
				type[0] = "#";
				type[1] = Number(sets);
			}
			return type;
			
			
		}
		
		
	}

}