/**
 * @category MyEic
 * @subcategory Libraries
 * @extends WindozModel
 */
class WindozBusModel extends  WindozModel{


    /**
    *   Makes a request to WSSGateway 
    */
    busWssGwRequest(action, payload = {}){
        if(!app.MessageBus) return(new Promise((ok, ko) => ko))
        if(!app.MessageBus.connected){ // bus not connected => Queue until connected, with 20 sec timeout
            return(
                app.MessageBus.whenConnectedP(20000).then(()=>{
                    return(app.MessageBus.requestWssGwAction(action, payload))
                },
                ()=>{
                    console.log('Timeout waiting for connection !')
                })
            )
        } 
        return(app.MessageBus.requestWssGwAction(action, payload))
    }

    /**
    *   Makes an action request on the bus
    */
    busActionRequest(chan, action, payload = {}){
        if(!app.MessageBus) return(new Promise((ok, ko) => ko))
        if(!app.MessageBus.connected){ // bus not connected => Queue until connected, with 20 sec timeout
            return(
                app.MessageBus.whenConnectedP(20000).then(()=>{
                    return(app.MessageBus.requestBusAction(chan, action, payload))
                },
                ()=>{
                    console.log('Timeout waiting for connection !')
                })
            )
        } 
        return(app.MessageBus.requestBusAction(chan, action, payload))
    }

    busEvent(chan, event, payload) {
        if( (!app.MessageBus) || (!app.MessageBus.connected) ) return(new Promise((ok, ko) => ko))
        app.MessageBus.sendEvent(chan, event, payload)
    }
}

app.registerClass('WindozBusModel', WindozBusModel)
