package com.dl.mortgageapp.view
{
    import com.dl.modules.pipes.components.PipeAwareModule;
    import com.dl.modules.pipes.events.ModuleEvents;
    import com.dl.modules.pipes.interfaces.IPipeAwareModule;
    import com.dl.modules.pipes.plumbing.DynamicTeeSplit;
    import com.dl.mortgageapp.MortgageAppEventNames;
    
    import org.puremvc.as3.multicore.interfaces.INotification;
    import org.puremvc.as3.multicore.utilities.pipes.interfaces.IPipeMessage;
    import org.puremvc.as3.multicore.utilities.pipes.messages.Message;
    import org.puremvc.as3.multicore.utilities.pipes.plumbing.Junction;
    import org.puremvc.as3.multicore.utilities.pipes.plumbing.JunctionMediator;
    import org.puremvc.as3.multicore.utilities.pipes.plumbing.Pipe;
    import org.puremvc.as3.multicore.utilities.pipes.plumbing.TeeMerge;

    public class ApplicationJunctionMediator extends JunctionMediator
    {
        
        public static const NAME:String = 'MortgageAppJunctionMediator';
        
        public function ApplicationJunctionMediator()
        {
            super(NAME, new Junction());
        }


        override public function onRegister():void
        {
            junction.registerPipe( PipeAwareModule.APP_TO_MODULE_PIPE,  Junction.OUTPUT, new DynamicTeeSplit() );
            junction.registerPipe( PipeAwareModule.MODULE_TO_APP_PIPE,  Junction.INPUT, new TeeMerge() );
            junction.addPipeListener( PipeAwareModule.MODULE_TO_APP_PIPE, this, handlePipeMessage );
        }

        override public function listNotificationInterests():Array
        {
            var interests:Array = super.listNotificationInterests();
            interests.push(ModuleEvents.CONNECT_MODULE_JUNCTION);
            interests.push(MortgageAppEventNames.REQUEST_FOR_LOAN);
            return interests;
        }
        
        override public function handleNotification(note:INotification):void
        {
            
            switch( note.getName() )
            {
                case MortgageAppEventNames.REQUEST_FOR_LOAN:
                    var loanMessage:Message = new Message(MortgageAppEventNames.REQUEST_FOR_LOAN,null,note);
                    junction.sendMessage(PipeAwareModule.APP_TO_MODULE_PIPE,loanMessage);
                break;
                
                case  ModuleEvents.CONNECT_MODULE_JUNCTION:
                    var module:IPipeAwareModule = note.getBody() as IPipeAwareModule;
                    
                    // Create the pipe
                    var moduleToApp:Pipe = new Pipe();
                    // Connect the pipe to our module
                    // Here we're handing down our pipe to the module, the module will, in turn,
                    // register this pipe as an OUTPUT pipe via junction.registerPipe(...);
                    // See JunctionMediator.as for more info
                    module.acceptOutputPipe(PipeAwareModule.MODULE_TO_APP_PIPE, moduleToApp);                
                    
                    // Connect the pipe to our app
                    var appIn:TeeMerge = junction.retrievePipe(PipeAwareModule.MODULE_TO_APP_PIPE) as TeeMerge;
                    appIn.connectInput(moduleToApp);
                    
                    // Cache for easy cleanup later
                    module.cacheFitting(moduleToApp,appIn);
                    
                    // Create the pipe
                    var appToModule:Pipe = new Pipe();
                    // Connect the pip to our module
                    // The module will register this pipe as an INPUT pipe and add a pipe listener
                    // i.e. call junction.registerPipe(...) and junction.addPipeListener(...)
                    // See JunctionMediator.as for more info
                    module.acceptInputPipe(PipeAwareModule.APP_TO_MODULE_PIPE,appToModule);
                    
                    // Connect the pipe to our app
                    var appOut:DynamicTeeSplit = junction.retrievePipe(PipeAwareModule.APP_TO_MODULE_PIPE) as DynamicTeeSplit;
                    appOut.connect(appToModule);

                    // Cache for easy cleanup later
                    module.cacheFitting(appToModule,appOut);

                    break;

                // And let super handle the rest (ACCEPT_OUTPUT_PIPE, ACCEPT_INPUT_PIPE, SEND_TO_LOG)                                
                
                default:
                    super.handleNotification(note);
                    
            }
        }
        
        override public function handlePipeMessage(message:IPipeMessage):void
        {
            // Handle our Module->Application integration
            if(message.getBody() is INotification)
            {
                var note:INotification = message.getBody() as INotification;
                
                switch(note.getName())
                {
                    case MortgageAppEventNames.LOAN_QUOTE_READY:
                        sendNotification(note.getName(),note.getBody(),note.getType());
                        break;
                    default:
                        sendNotification(note.getName(),note.getBody(),note.getType());
                        break;
                }
            }
        }        
    }
}