package com.xt.hb.utils
{
	
	import com.xt.hb.events.QueryLayerEvent;
	
	import mx.core.FlexGlobals;

	public class ConfigUtil
	{
		public function ConfigUtil()
		{
		}
		
		public static function getMenuXML(id:String,xml:XML):XML
		{
			if(xml == null)
				return null;
			for each(var x:XML in xml.children())
			{
				if(x.@cdid == id)
					return x;
			}
			return null;
		}
		
		public static function getMapLayerNameAndOwner(layerId:String):String
		{
			try
			{
				//var serviceList:XMLList = mapConfig.MapServices.Service;
				var serviceList:XMLList = FlexGlobals.topLevelApplication.mapConfig.MapServices.Service;
				var id:String = layerId;
				if(layerId.indexOf("#")>0)
				{
					id = layerId.substring(0,layerId.indexOf("#"));
				}
				for each(var xml:XML in serviceList)
				{
					if("SERVICE_ID_GKZY" == xml.@ID)
					{
						for each(var xmlLayer:XML in xml.Layer)
						{
							if(id == xmlLayer.@ID)
								return xmlLayer.@Name + "@" + xmlLayer.@Owner;
						}
					}
				}
			}
			catch(err:Error)
			{
				return null;
			}
			return null;
		}
		
		public static function getMapUrl(layerId:String):String
		{
			try
			{
				var serviceList:XMLList = FlexGlobals.topLevelApplication.mapConfig.MapServices.Service;
				var id:String = layerId;
				if(layerId.indexOf("#")>0)
				{
					id = layerId.substring(0,layerId.indexOf("#"));
				}
				for each(var xml:XML in serviceList)
				{
					for each(var xmlLayer:XML in xml.Layer)
					{
						if(id == xmlLayer.@ID){
							return getRealUrl(xml.@URL);
						}
					}
				}
			}
			catch(err:Error)
			{
				return null;
			}
			return null;
		}
		/**
		 * 根据xml配置的url获取真正的url
		 */
		public static function getRealUrl(xmlUrl:String):String
		{
			xmlUrl =xmlUrl.replace("${serverName}",FlexGlobals.topLevelApplication.serverName);
			xmlUrl =xmlUrl.replace("${port}",FlexGlobals.topLevelApplication.port);
			return xmlUrl
		}
		public static function getMapServiceUrl(serviceId:String):String
		{
			try
			{
				var serviceList:XMLList = FlexGlobals.topLevelApplication.mapConfig.MapServices.Service;
				for each(var xml:XML in serviceList)
				{
					if(serviceId == xml.@ID)
					{
						return getRealUrl(xml.@URL);
					}
				}
			}
			catch(err:Error)
			{
				return null;
			}
			return null;
		}
		
		public static function getFeatureStyle(xml:XML):Object
		{
			var obj:Object = new Object();
			for each(var x:XML in xml.children())
			{
				for each(var x0:XML in x.children())
				{
					obj[x0.@CDID] = x0.@CDTB;
				}
			}
			return obj;
		}
		
		public static function getMapLayerLabel(layerId:String):String
		{
			try
			{
				//var serviceList:XMLList = mapConfig.MapServices.Service;
				var serviceList:XMLList = FlexGlobals.topLevelApplication.mapConfig.MapServices.Service;
				var id:String = layerId;
				if(layerId.indexOf("#")>0)
				{
					id = layerId.substring(0,layerId.indexOf("#"));
				}
				for each(var xml:XML in serviceList)
				{
					if("SERVICE_ID_GKZY" == xml.@ID)
					{
						for each(var xmlLayer:XML in xml.Layer)
						{
							if(id == xmlLayer.@ID)
								return xmlLayer.@Label;
						}
					}
				}
			}
			catch(err:Error)
			{
				return null;
			}
			return null;
		}
		
		public static function getMapLayerState(layerId:String):String
		{
			try
			{
				// 遍历所有菜单配置
				var list:XMLList = FlexGlobals.topLevelApplication.M_ZYZS_TCKZ_XML.children();
				for each( var xmlChildren:XML in list)  
				{
					if(xmlChildren.@cdid==layerId)
					{
						return xmlChildren.@checkState
					}
				}
			}
			catch(err:Error)
			{
				return null;
			}
			return null;
		}
	}
}