package net.fdms.Resource 
{
	import com.greensock.loading.core.LoaderCore;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import net.fdms.Frame.Frame;
	/**
	 * 2011/5/19 14:15
	 * @author Dawnboy
	 */
	public class Resource 
	{
		private static var _resource:Array = [];
		private static var RID:uint = 0;
		public function Resource() 
		{
			
		}
		public static function get ResLib():Array {
			return _resource;
		}
		
		public static function length():int {
			return _resource.length;
		}
		/**
		 * 向资源库中添加新资源信息
		 * @param	resSets
		 */
		public static function addNewResource(resSets:Object):void {
			if (checkExist(resSets.id)) {
				return;
			}else {
				createID();
				_resource.push([RID, resSets.id, null, resSets, resSets.parentID,false,null]);
			}
			//trace(_resource);
		}
		
		/**
		 * @private
		 * 获取对象的RID
		 * @param	target
		 * @return
		 */
		public static function getRID(target:*):int {
			var foo:int = getType(target);
			var result:int = 0;
			var n:int = _resource.length;
			for (var a:int = 0; a < n; a++) {
				if (target == _resource[a][foo]) {
					result = _resource[a][0];
					break;
				}
			}
			return result;
		}
		
		/**
		 * @private
		 * 获取对象的NAME
		 * @param	target
		 * @return
		 */
		public static function getName(target:*):String {
			var foo:int = getType(target);
			var result:String = null;
			var n:int = _resource.length;
			for (var a:int = 0; a < n; a++) {
				if (target == _resource[a][foo]) {
					result = _resource[a][1];
					break;
				}
			}
			return result;
		}
		
		
		
		/**
		 * 通过RID或引用标识符获取资源库中对象
		 * @param	ID	RID或引用标识符
		 * @return
		 */
		public static function getObject(ID:*):DisplayObject {
			var n:int = _resource.length;
			var foo:int = getType(ID);
			var result:DisplayObject = (ID is DisplayObject)?ID:undefined;
			
			for (var a:int = 0; a < n; a++) {
				if (ID == _resource[a][foo]) {
					result = _resource[a][2];
					break;
				}
			}
			return result;
		}
		
		/**
		 * 设置该资源的显示对象
		 * @param	ID
		 * @param	object
		 */
		public static function setObject(ID:*,object:DisplayObject):void {
			var n:int = _resource.length;
			var foo:int = getType(ID);
			
			for (var a:int = 0; a < n; a++) {
				if (ID == _resource[a][foo]) {
					_resource[a][2] = object;
					break;
				}
			}
			
		}
		
		
		/**
		 * 设置该资源的Loader
		 * 
		 * @param	ID
		 * @param	loader
		 */
		public static function setLoader(ID:*,loader:LoaderCore):void {
			var n:int = _resource.length;
			var foo:int = getType(ID);
			
			for (var a:int = 0; a < n; a++) {
				if (ID == _resource[a][foo]) {
					_resource[a][6] = loader;
					break;
				}
			}
			
		}
		
		/**
		 * 获取资源Loader
		 * @param	ID	RID或引用标识符
		 * @return
		 */
		public static function getLoader(ID:*):LoaderCore {
			var n:int = _resource.length;
			var foo:int = getType(ID);
			var result:LoaderCore = undefined;
			
			for (var a:int = 0; a < n; a++) {
				if (ID == _resource[a][foo]) {
					result = _resource[a][6];
					break;
				}
			}
			return result;
		}
		
		
		/**
		 * 设置该资源的配置信息
		 * @param	ID
		 * @param	config
		 */
		public static function setConfig(ID:*,config:Object):void {
			var n:int = _resource.length;
			var foo:int = getType(ID);
			
			for (var a:int = 0; a < n; a++) {
				if (ID == _resource[a][foo]) {
					_resource[a][3] = config;
					break;
				}
			}
		}
		
		
		/**
		 * 获取该资源的配置信息
		 * @param	ID
		 * @return
		 */
		public static function getConfig(ID:*):Object {
			var n:int = _resource.length;
			var foo:int = getType(ID);
			var result:Object;
			
			for (var a:int = 0; a < n; a++) {
				if (ID == _resource[a][foo]) {
					result = _resource[a][3];
					break;
				}
			}
			return result;
		}
		
		/**
		 * 是否含有子资源
		 * @param	ID
		 * @return
		 */
		public static function hasChild(ID:String):Boolean {
			var n:int = _resource.length;
			var result:Boolean = false;
			
			for (var a:int = 0; a < n; a++) {
				if (ID == _resource[a][4]) {
					result = true;
					break;
				}
			}
			return result;
		}
		
		/**
		 * 获取子资源
		 * @param	ID
		 * @return
		 */
		public static function getChild(ID:String):Array {
			var n:int = _resource.length;
			var result:Array = [];
			
			for (var a:int = 0; a < n; a++) {
				if (ID == _resource[a][4]) {
					result.push(_resource[a][1])
				}
			}
			return result;
		}
		
		
		/**
		 * 获取首次预载资源
		 * @return
		 */
		public static function getPreloadRes():Array {
			var n:int = _resource.length;
			var tmp:Array = [];
			for (var a:int = 0; a < n; a++ ) {
				if (_resource[a][3].parentID == "root" && _resource[a][3].preload == true) {
					tmp.push(_resource[a][1]);
				}
			}
			return tmp;
		}
		
		/**
		 * 获取子预载资源
		 * @param	res
		 */
		public static function getPreloadChild(ID:*):Array {
			
			var n:int = _resource.length;
			var foo:int = getType(ID);
			var name:String = getName(ID);
			var childs:Array = [];
			if (checkExist(ID)) {
				for (var a:int = 0; a < n; a++) {
					if (_resource[a][4] == name && _resource[a][3].preload) {
						childs.push(_resource[a][1]);
						
						var preloadChild:Array = getPreloadChild(_resource[a][1]);
						var tn:int = preloadChild.length;
						for (var b:int = 0; b < tn-1; b++) {
							childs.push(preloadChild[b]);
						}
					}
				}
				if(getConfig(name).src){
					childs.push(name);
				}
			}
			return childs;
		}
		
		/**
		 * 重设对齐方式
		 * @param	target
		 * @param	AlignSets
		 */
		public static function resetAlign(ID:*,AlignSets:Array):void {
			var tmp:Boolean = false;
			var n:int = _resource.length;
			var foo:int = getType(ID);
			for (var a:int = 0; a < n; a++) {
				if (ID == _resource[a][foo]) {
					_resource[a][3].alignSets = AlignSets;
					tmp = true;
				}
				if (tmp) break;
			}
		}
		
		/**
		 * 该方法用于检查所提供的显示对象是否存在资源表中
		 * @param	target	可提供显示资源的RID,标识flagID或对象引用
		 * @return		返回该显示对象是否存在表中
		 */
		public static function checkExist(ID:String):Boolean {
			var tmp:Boolean = false;
			var n:int = _resource.length;

			for (var a:int = 0; a < n; a++) {
				if (ID == _resource[a][1]) tmp = true;
				if (tmp) break;
			}
			return tmp;
		}
		
		/**
		 * 检查对象是否已经被载入
		 * @param	target
		 */
		public static function checkLoaded(ID:*):Boolean {
			var n:int = _resource.length;
			var foo:int = getType(ID);
			var result:Boolean;
			
			for (var a:int = 0; a < n; a++) {
				if (ID == _resource[a][foo]) {
					result = Boolean(_resource[a][2]);
					break;
				}
			}
			return result;
		}
		
		/**
		 * 获取父级对象
		 * @param	ID
		 * @param	getRID	0-1
		 * @return
		 */
		public static function getParent(ID:*, getID:int = 0):* {
			//trace(_resource);
			var result:* = undefined;
			var temp:Boolean;
			var n:int = _resource.length;
			var foo:int = getType(ID);
			for (var a:int = 0; a < n; a++) {
				if (ID == _resource[a][foo]) {
					for (var b:int = 0; b < n; b++) {
						if (_resource[a][4] == _resource[b][1]) {
							if (getID == 1) {
								result = _resource[b][1] as String;
							}else {
								result = _resource[b][2] as DisplayObject;
							}
							temp = true;
							break;
						}
					}
				}
				if (temp) break;
			}
			return result;
		}
		
		/**
		 * 设定对象显示状态为False
		 * @param	target
		 */
		public static function unShow(ID:*):void {
			setShowStatus(ID,false);
		}
		/**
		 * 设定对象显示状态为True
		 * @param	target
		 */
		public static function show(ID:*):void {
			setShowStatus(ID, true);
		}
		/**
		 * 获取对象的显示状态
		 * @param	target
		 * @return
		 */
		public static function Status(ID:*):Boolean {
			var result:Boolean;
			var n:int = _resource.length;
			var foo:int = getType(ID);
			for (var a:int = 0; a < n; a++) {
				if (ID == _resource[a][foo]) {
					result = _resource[a][5];
					break;
				}
			}
			return result;
		}
		
		/**
		 * 获取动作列表
		 * @param	ID
		 * @param	actType
		 */
		public static function getAction(ID:*,actType:int = 0):Array {
			var result:Array = [];
			var temp:Boolean;
			var n:int = _resource.length;
			var foo:int = getType(ID);
			for (var a:int = 0; a < n; a++) {
				if (ID == _resource[a][foo]) {
					switch(actType) {
						case 0:
							result = _resource[a][3].preAction;
							break;
						case 1:
							result = _resource[a][3].Action;
							break;
						case 2:
							result = _resource[a][3].unloadAction;
							break;
					}
					temp = true;
				}
				if (temp) break;
			}
			return result;
		}
		
		/**
		 * 设置对象的SPACE值
		 */
		/*public static function setSpace(ID:*):void {
			var n:int = _resource.length;
			var foo:int = getType(ID);
			for (var a:int = 0; a < n; a++) {
				if (ID == _resource[a][foo]) {
					//result = _resource[a][5];
					break;
				}
			}
		}*/
		
		/**
		 * 改变显示层级
		 * @param	target
		 * @param	layer
		 */
		public static function setLayer(ID:*, layer:String):void {
			var target:DisplayObject = getObject(ID);
			if(Status(ID) && target.parent){
				switch(layer) {
					case "top":
						target.parent.setChildIndex(target, target.parent.numChildren - 1);
					break;
					case "bottom":
						target.parent.setChildIndex(target,0);
					break;
					default:
						var foo:Boolean = false;
						var targetOB:* = Frame.getContainer(layer);
						if (targetOB) {
							target.parent.setChildIndex(target, targetOB.parent.getChildIndex(targetOB)+1);
						}
					break;
				}
			}
		}
		
		public static function clear(ID:*):void {
			var target:*;
			var n:int = _resource.length;
			var foo:int = getType(ID);
			for (var a:int = 0; a < n; a++) {
				if (ID == _resource[a][foo]) {
					target = getObject(ID);
					if(target is DisplayObject){
						clearChild(target as DisplayObject);
						break;
						_resource[a][6].unload();
						_resource[a][6] = null;
					}
				}
			}
		}
		
		private static function clearChild(target:*):void {
			if (target is DisplayObjectContainer) {
				var n:int = target.numChildren;
				for (var a:int = 0; a < n; n--) {
					var child:* = target.getChildAt(a);
					if (child && child is DisplayObjectContainer) {
						clearChild(child);
					}else {
						try {
							child["clear"]();
							target.removeChild(child);
						}catch (e:*) {
							
						}
						child = null;
					}
					
				}
			}
			try{
				target["clear"]();
			}catch(e:*) {
				
			}
			target.parent.removeChild(target);
			target = null;
		}
		
		/**
		 * 生成新ID
		 */
		private static function createID():void {
			RID++;
		}
		
		/**
		 * 设定对象显示状态
		 * @param	target
		 * @param	status
		 */
		private static function setShowStatus(ID:*, status:Boolean):void {
			var temp:Boolean;
			var n:int = _resource.length;
			var foo:int = getType(ID)
			
			for (var a:int = 0; a < n; a++) {
				if (ID == _resource[a][foo]) {
					_resource[a][5] = status;
					temp = true;
				}
				if (temp) break;
			}
		}
		
		/**
		 * 获取请求类型
		 * @param	ID
		 * @return
		 */
		private static function getType(ID:*):int {
			var foo:int;
			if (ID is Number) {
				foo = 0;
			}else if(ID is String) {
				foo = 1;
			}else if (ID is DisplayObject) {
				foo = 2;
			}
			return foo;
		}

	}

}