package flater.cairngormtips.controller.delegates
{
import com.universalmind.cairngorm.business.Delegate;

import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;

import flater.cairngormtips.util.out;

import mx.rpc.IResponder;

/**
 *  A delegate for performing the operations for the
 *  EventGenerator Example.<br/> 
 *  <br/>                                                                                            
 *  Author: <a href="http://www.adamflater.net" target="_blank">
 *  Adam Flater</a>
 *  <br/>
 *                                                                                           
 *  @see flater.cairngormtips.controller.commands.ConfigurationCommand
 *  @see flater.cairngormtips.controller.commands.DownloadCommand
 *  @see flater.cairngormtips.EventGeneratorExampleApplication
 */ 
public class EventGeneratorExampleDelegate extends Delegate
{
    private static const CONFIG_URL : String = 
                           "assets/config/configuration.xml";
    
    public function EventGeneratorExampleDelegate(
                          commandHandlers : IResponder = null )
    {
        super( commandHandlers );
        
        out( "EventGeneratorExampleDelegate Instantiated" );
    }
    
    //--------------------------------------
    //  getConfig
    //--------------------------------------
    
    /**
     *  Downloads the config xml data for the application
     */
    public function getConfig() : void
    {
        out( "EventGeneratorExampleDelegate.getConfig() invoked" );
                  
        var loader : URLLoader = new URLLoader();
            loader.addEventListener( Event.COMPLETE, 
                                     getConfig_Result );
            loader.addEventListener( IOErrorEvent.IO_ERROR,
                                     getConfig_Fault );
            loader.addEventListener( SecurityErrorEvent.SECURITY_ERROR,
                                     getConfig_Fault );
            loader.load( new URLRequest( CONFIG_URL ) ); 
    }
    
    /**
     *  @private
     * 
     *  Event handler; Responds to the results of the getConfig 
     *  operation.
     */
    private function getConfig_Result( e : Event ) : void
    {
        out( "EventGeneratorExampleDelegate.getConfig_Result() " + 
             "invoked" );
                
       var loader  : URLLoader = e.target as URLLoader;
       var xml     : XML       = XML( loader.data );
       
       responder.result( xml );    
    }
    
    /**
     *  @private
     * 
     *  Event handler; Responds to the faults of the getConfig 
     *  operation.
     */
    private function getConfig_Fault( e : Event ) : void
    {
        out( "EventGeneratorExampleDelegate.getConfig_Fault() " + 
             "invoked" );
                
       responder.fault( e );    
    }
    
    //--------------------------------------
    //  downloadSomthing
    //--------------------------------------
    
    /**
     *  Downloads the resource specified by the url.
     */
    public function downloadSomthing( url : String ) : void
    {
        out( "EventGeneratorExampleDelegate.downloadSomthing() " + 
             "invoked" );
                
        var loader : URLLoader = new URLLoader();
            loader.addEventListener( Event.COMPLETE, 
                                     downloadSomthing_Result ) 
            loader.addEventListener( IOErrorEvent.IO_ERROR,
                                     downloadSomthing_Fault )
            loader.addEventListener( SecurityErrorEvent.SECURITY_ERROR,
                                     downloadSomthing_Fault )
            loader.load( new URLRequest( url ) ); 
    }
    
    /**
     *  @private
     * 
     *  Event handler; Responds to the results of the downloadSomthing 
     *  operation.
     */
    private function downloadSomthing_Result( e : Event ) : void
    {
        out( "EventGeneratorExampleDelegate.downloadSomthing_Result() " + 
             "invoked" );
                
       var loader  : URLLoader = e.target as URLLoader;
       
       responder.result( loader.data );    
    }
    
    /**
     *  @private
     * 
     *  Event handler; Responds to the faults of the downloadSomthing 
     *  operation.
     */
    private function downloadSomthing_Fault( e : Event ) : void
    {
        out( "EventGeneratorExampleDelegate.downloadSomthing_Fault() " + 
             "invoked" );
                
       responder.fault( e );    
    }
    
    
} //  end class
} //  end package