package net.fdms.Function.Convert 
{
	/**
	 * 2011/5/27 16:56
	 * @author Dawnboy
	 */
	public class ResXMLtoOBJECT 
	{
		
		public function ResXMLtoOBJECT() 
		{
			
		}
		
		/**
		 * 将XNL文档节点转换为Object对象
		 * @param	xml
		 * @return
		 */
		public static function convert(xml:*):Object {
			var config:Object = new Object();
			for each(var prop:XML in xml.attributes()) {
				var ob:Object = prop.localName();
				config[ob] = xml.@[ob];
				//trace(prop.toString());
			}
			config.id = String(config.id);
			config.src = (config.src != undefined && config.src != "")?config.src = config.src.split(","):undefined;
			config.reset = (config.reset != undefined && config.reset != "")?config.reset = config.reset.split(","):undefined;
			config.status = (config.status != undefined && config.status != "")?config.status = config.status.split(","):undefined;
			config.alignSets = (config.alignSets != undefined && config.alignSets != "")?config.alignSets = config.alignSets.split(","):undefined;
			config.preload = (config.preload != undefined && config.preload != "")?config.preload = StringToBoolean.convert(config.preload):false;
			config.loadSets = (config.loadSets != undefined && config.loadSets != "")?config.loadSets = config.loadSets.split(","):undefined;
			if (config.loadSets && config.loadSets[2] == undefined) config.loadSets[2] = "Center";
			config.unloadSets = (config.unloadSets != undefined && config.unloadSets != "")?config.unloadSets = config.unloadSets.toString().split(","):undefined;
			config.preAction =  (config.preAction != undefined && config.preAction != "")?config.preAction = ACTtoArr(config.preAction):undefined;
			config.action =  (config.action != undefined && config.action != "")?config.action = ACTtoArr(config.action):undefined;
			config.unloadAction =  (config.unloadAction != undefined && config.unloadAction != "")?config.unloadAction = ACTtoArr(config.unloadAction):undefined;
			config.btnAction =  (config.btnAction != undefined && config.btnAction != "")?config.btnAction = BTNtoArr(config.btnAction):undefined;
			config.overAction =  (config.overAction != undefined && config.overAction != "")?config.overAction = BTNtoArr(config.overAction):undefined;
			config.outAction =  (config.outAction != undefined && config.outAction != "")?config.outAction = BTNtoArr(config.outAction):undefined;
			config.status = (config.status != undefined && config.status != "")?config.status = config.status.toString().split(","):undefined;
			config.space = (config.space != undefined && config.space != "")?config.space = config.space.toString():undefined;
			return config;
		}
		
		/**
		 * 解析动作字串
		 * @param	action
		 * @return
		 */
		private static function ACTtoArr(action:String):Array {
			var foo_a:Array = action.split("|");
			var foo_b:Array = [];
			var n:int = foo_a.length;
			
			for (var a:int = 0 ; a < n; a++ ) {
				
				var tmp:Array = foo_a[a].toString().split(",");
				
				tmp[0] = int(tmp[0]);
				tmp[2] = Number(tmp[2]);
				
				switch(tmp[0]) {
					case 2:
						tmp[3] = StringToObject.convert(tmp[3]);
						break;
					case 3:
						tmp[3] = StringToObject.convert(tmp[3]);
						break;
					case 4:
						tmp[3] = String(tmp[3]);
						break;
				}
				
				foo_b.push(tmp);
			}
			return foo_b;
		}
		
		/**
		 * 按钮动作解析
		 * @param	action
		 * @return
		 */
		public static function BTNtoArr(action:String):Array {
			var btns:Array = action.split("&");
			var c_btn:Array = [];
			var n:int = btns.length;
			for (var a:int = 0; a < n; a ++ ) {
				var btnSets:Array = btns[a].split("#");

				var newBtn:Object = {};
				newBtn.id = btnSets[0];
				newBtn.action = btnSets[1];
				
				var _actions:Array = String(newBtn.action).split("|");
				var tn:int = _actions.length;
				var c_action:Array = [];
				for (var b:int = 0; b < tn; b++) {
					var tmp:Array = _actions[b].split("'");
					tmp[0] = int(tmp[0]);
					tmp[2] = Number((tmp[2])?tmp[2]:0);
					
					switch(tmp[0]) {
						case 2:
							tmp[3] = StringToObject.convert(tmp[3]);
							break;
						case 3:
							tmp[3] = StringToObject.convert(tmp[3]);
							break;
						case 4:
							tmp[3] = [String(tmp[3]),tmp[4]];
							break;
					}
					c_action.push(tmp);
				}
				newBtn.action = c_action;
				c_btn.push(newBtn);
			}
			return c_btn;
		}
		
	}

}