package com.wks.util
{
 import com.wks.common;
 
 import flash.events.Event;
 import flash.events.IOErrorEvent;
 import flash.net.URLLoader;
 import flash.net.URLRequest;
 
 import mx.collections.ArrayCollection;
 import mx.controls.Alert;
 import mx.rpc.events.FaultEvent;
 import mx.rpc.events.ResultEvent;
 import mx.rpc.http.HTTPService;
 
 import spark.components.Application;
 
 public class LoadXML
 {
  public static const loadXMLCompleteEvent:String = "load_xml_complete";
  public static const loadXMLFaultEvent:String = "load_xml_fault";
  public static const loadXMLSearchTitleEvent:String = "load_xml_searchTitle_complete";
  
  public static var roleList:XMLList;
  public static var menuList:XMLList;
  public static var remoteStr:String = "";
       
        public static var moduleList:XMLList;
       
        private static var http:HTTPService = null;
       
  public function LoadXML()
  {
  }
  
  public static function loadLoginXml(url:String="configData.xml"):void
  {
   http = new HTTPService();
   http.url = url;
   http.resultFormat = "e4x";
   http.addEventListener(ResultEvent.RESULT,xmlresult);
   http.addEventListener(FaultEvent.FAULT,xmlfault);
   http.send();
   
   function xmlresult(event:ResultEvent):void
   {
    var conxml:XML = event.result as XML;
    
    if(conxml)
    {
     var remotes:XMLList = conxml.child("remotingAddress");
     var remote:XML = remotes[0];
     common.RemotingAddress = remote.toString();
     common.ExportExcelURL = conxml.child("exportExcelURL")[0].toString();
     
     var freePage:XML = conxml.child("freePage")[0] as XML;
     if(freePage.@enable == "true")common.freePage = true;
     else common.freePage = false;
     if(freePage.@label)common.freePageLabel = freePage.@label;
     if(freePage.@url)common.freePageUrl = freePage.@url;
     
     var swfFile:XML = conxml.child("swfFile")[0] as XML;
     if(swfFile.@enable == "true")
     {
      common.swfFile = true;
      common.freePageLabel = swfFile.@label;
      common.freePageUrl = swfFile.@url;
     }
     else
     {
      common.swfFile = false;
     }
     
     var p_w_picpath:XML = conxml.child("p_w_picpath")[0] as XML;
     common.imgWidth = p_w_picpath.@width || 200;
     common.imgHeight = p_w_picpath.@height || 180;
     
     roleList = conxml.child("roleList");
     menuList = conxml.child("menuList");
    }
    EventBus.getInstance().dispatchEvent(new Event("load_xml_complete"));
   }
   
   function xmlfault(e:FaultEvent):void
   {
    EventBus.getInstance().dispatchEvent(new Event("load_xml_fault"));
   }
  }
       
        public static function loadBackgroundConfig(url:String = "backgroundConfig.xml"):void
  {
            var xmlUrl:URLRequest = new URLRequest(url);
            var loader:URLLoader = new URLLoader(xmlUrl);
            loader.addEventListener(Event.COMPLETE, xmlResult);
            loader.addEventListener(IOErrorEvent.IO_ERROR, xmlFault);
           
            function xmlResult(event:Event):void
   {
                var config:XML = new XML(event.target.data);
                if(config)
    {
                    var remoteAddress:XMLList = config.child("remotingAddress");
                    var remote:XML = remoteAddress[0];
                    remoteStr = remote.toString();
                    common.RemotingAddress = remote.toString();
     
     common.ExportExcelURL = config.child("exportExcelURL")[0].toString();
     common.uploadUrl = config.child("imgUploadURL")[0].toString();
                    moduleList = config.child("menuBar");
                   
                    common.CouponUpdateTemplatesURL = config.child("couponUpdateTemplatesURL")[0].toString();
                    common.ImportCouponInfoURL = config.child("importCouponInfoURL")[0].toString();
                }
                EventBus.getInstance().dispatchEvent(new Event(loadXMLCompleteEvent));
            }
            function xmlFault(e:IOErrorEvent):void
   {
                EventBus.getInstance().dispatchEvent(new Event(loadXMLFaultEvent));
            }
        }
  
  public static function loadSearchTitleConfig(url:String = "searchTitle.xml"):void
  {
   
   var xmlUrl:URLRequest = new URLRequest(url);
   var loader:URLLoader = new URLLoader(xmlUrl);
   loader.addEventListener(Event.COMPLETE, xmlResult);
   loader.addEventListener(IOErrorEvent.IO_ERROR, xmlFault);
   
   function xmlResult(event:Event):void
   {
    var config:XML = new XML(event.target.data);
    
    if(config)
    {
     
     var kouweiTitles:XMLList = config.child("kouweiTitle");
     var jiageTitles:XMLList = config.child("jiageTitle");
     var yanseTitles:XMLList = config.child("yanseTitle");
     
     var kouweiList:ArrayCollection = new ArrayCollection();
     var kws:XMLList = kouweiTitles.children();
     
     for each(var currKouwei:XML in kws)
     {
      var currObj:Object = new Object();
      currObj.title = currKouwei[0].@label;
      currObj.value = currKouwei[0].@value;
      currObj.type = 1;
      
      kouweiList.addItem(currObj);
     }
     
     var jiageList:ArrayCollection = new ArrayCollection();
     var jgs:XMLList = jiageTitles.children();
     
     for each(var currJiage:XML in jgs)
     {
      var currObj:Object = new Object();
      currObj.title = currJiage[0].@label;
      currObj.min = currJiage[0].@min;
      currObj.max = currJiage[0].@max;
      currObj.type = 2;
      
      jiageList.addItem(currObj);
     }
     
     var yanseList:ArrayCollection = new ArrayCollection();
     var yss:XMLList = yanseTitles.children();
     for each(var currYanse:XML in yss)
     {
      var currObj:Object = new Object();
      currObj.title = currYanse[0].@label;
      currObj.value = currYanse[0].@value;
      currObj.type = 3;
      
      yanseList.addItem(currObj);
     }
     
     common.searchTitles.addItem(kouweiList);
     common.searchTitles.addItem(jiageList);
     common.searchTitles.addItem(yanseList);
    }
    
//    EventBus.getInstance().dispatchEvent(new Event(loadXMLSearchTitleEvent));
   }
   function xmlFault(e:IOErrorEvent):void
   {
    Alert.show("error");
//    EventBus.getInstance().dispatchEvent(new Event(loadXMLFaultEvent));
   }
  }
       
        public static function getModuleList(menuid:int):XMLList
  {
            var menus:XML = moduleList[0] as XML;
            var menu:XMLList = menus.child("menu");
            var singleMenu:XMLList = menu.(@id == menuid);
            var itemList:XMLList = singleMenu.item;
            return itemList;
        }
 }
}