package com.xt.hb.server
{
	import com.xt.hb.events.DataConfigEvent;
	
	import flash.events.EventDispatcher;
	
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	
	[Event(name="mapConfigLoad", type="com.xt.hb.events.DataConfigEvent")]
	[Event(name="menuConfigLoad", type="com.xt.hb.events.DataConfigEvent")]
	public class XMLDecoder extends EventDispatcher
	{
		public function XMLDecoder()
		{			
			mapConfigLoad(); 
			menuConfigLoad();
		}
		
		private var mapConfig:String = "com/xt/hb/cfg/MapConfig.xml";
		private var menuConfig:String = "com/xt/hb/cfg/MenuConfig.xml";
		
		private function mapConfigLoad():void
		{
			var configService:HTTPService = new HTTPService();			
			configService.url = mapConfig;
			configService.resultFormat = "e4x";
			configService.addEventListener(ResultEvent.RESULT, configResult);
			configService.addEventListener(FaultEvent.FAULT, configFault);	
			configService.send();
		}
		
		private function configResult(event:ResultEvent):void
		{						
			var config:XML = event.result as XML;
			dispatchEvent(new DataConfigEvent(DataConfigEvent.MAPCONFIG_LOAD, config));
		}
		
		/**
		 *  当主配置文件解析失败时 派发CONFIG_LOAD_ERROR事件
		 */
		private function configFault(event:FaultEvent):void
		{			
			this.dispatchEvent(new DataConfigEvent(DataConfigEvent.CONFIG_LOAD_ERROR, event.fault));
		}
		
		private function menuConfigLoad():void
		{
			var configService:HTTPService = new HTTPService();			
			configService.url = menuConfig;
			configService.resultFormat = "e4x";
			configService.addEventListener(ResultEvent.RESULT, menuConfigResult);
			configService.addEventListener(FaultEvent.FAULT, menuConfigFault);	
			configService.send();
		}
		
		private function menuConfigResult(event:ResultEvent):void
		{						
			var config:XML = event.result as XML;
			dispatchEvent(new DataConfigEvent(DataConfigEvent.MENUCONFIG_LOAD, config));
		}
		
		/**
		 *  当主配置文件解析失败时 派发CONFIG_LOAD_ERROR事件
		 */
		private function menuConfigFault(event:FaultEvent):void
		{			
			this.dispatchEvent(new DataConfigEvent(DataConfigEvent.CONFIG_LOAD_ERROR, event.fault));
		}
	}
}