App.ApplicationRoute = Ember.Route.extend
  setupController: (controller)->
    #@controllerFor('product_orders').set 'model', @store.filter('product_order', (po)-> !po.get('order')) # does not work (yet)
    @controllerFor('product_orders').set 'model', @store.filter('product_order', (po)-> !po.get('id'))
    controller.secured ->
      faye = new Faye.Client($event_host)
      user_id = Qstorage.getItem('user_id')
      faye.subscribe "/user/"+user_id, (e)=>
        console.log e
        @events[e.event].call(@, e.data) if @events[e.event]
      @setCurrentList()

  unauthorized: ->
    Qstorage.setItem('auth_token', '')
    @controllerFor('application').set 'list', null
    App.obtain_token(t('messages.unauthorized'))
    @controllerFor('application').redirect_to 'index', message: 'unauthorized'
  actions:
    openModal: (modalName, model)->
      @controllerFor(modalName).set('model', model)
      @render modalName,
        into: 'application'
        outlet: 'modal'
    closeModal: ->
      @disconnectOutlet
        outlet: 'modal'
        parentView: 'application'
    confirm: (options = {})->
      @send 'openModal', 'modal_confirm', Ember.Object.create
        title: options.title
        body: options.body
        cancel: options.cancel
        ok: options.ok
    listNeedsPayment: ->
      @get('controller').secured ->
        Ember.$.post '/user/list_needs_payment.json'
        #Ember.$.post('/user/list_needs_payment.json').then (res) =>
          #@set('list.needs_payment', true) # also done by faye
    listNeedsHelp: ->
      @get('controller').secured ->
        Ember.$.post '/user/needs_help.json'
        #Ember.$.post('/user/needs_help.json').then (res) =>
          #@set('list.needs_help', true) # also done by faye
    scanQr: ->
      @transitionTo 'select_qrcode'
  events: ->
    error: (error)->
      debugger
