App.ApplicationController = Ember.Controller.extend
  join_request_sent: false
  notice: ''
  actions:
    confirmCancel: ->
      if cancel = @get('confirm.cancel')
        cancel.call(@)
      $('#confirm-modal').hide()
    clearNotice: ->
      @set 'notice', null
    openDebugger: ->
      debugger
  currentPathDidChange: (->
    @set 'notice', ''
  ).observes('currentPath')
  events:
    list_helped: -> @set 'list.needs_help', false
    list_needs_help: -> @set 'list.needs_help', true # incoming from other users
    list_is_paid: -> @set 'list.needs_payment', false
    list_needs_payment: -> @set 'list.needs_payment', true # incoming from other users
    list_closed: (data)->
      @transitionToRoute('list', data.id).then =>
        @set 'list.state', 'closed'
        @set 'list', null
    join_request_approved: (data)->
      @setCurrentList -> @transitionToRoute('active_list')
    order_being_processed: (data)->
      order = @store.all('order').findBy 'id', data.id
      order.set('state', 'active') if order
    order_being_delivered: (data)->
      order = @store.all('order').findBy 'id', data.id
      order.set('state', 'delivered') if order
    user_join_request: (data)->
      list = @store.all('list').findBy 'id', data.join_request.list_id
      user_data = data.users[0]
      user = @store.all('user').findBy 'id', user_data.id
      user = @store.createRecord 'user', user_data unless user
      data.join_request.list = list
      data.join_request.user = user
      join_request = @store.createRecord 'join_request', data.join_request
      @transitionToRoute 'join_requests'
    join_request_rejected: (data)->
      # Remove join request from connected users
      join_request = @store.all('join_request').findBy 'id', data.id
      join_request.eraseRecord() if join_request

      # Notify requestor in a proper manner
      # NOTE data.id is in the form of: jr-:user_id
      requestor_id = data.id.substr(3)
      if requestor_id is Qstorage.getItem('user_id')
        @set 'notice', t('join_request.requestor.join_request_rejected')
        @set 'join_request_sent', false

    join_request_approved: ->
      @setCurrentList ->
        @transitionToRoute('active_list').then =>
          @set 'notice', t('join_request.requestor.join_request_approved')

    list_changed_table: ->
      @setCurrentList()
    orders_in_process_count: (data)->
      @set 'list.supplier.orders_in_process_count', data.count
    orders_placed_count: (data)->
      @set 'list.supplier.orders_placed_count', data.count


  setCurrentList: (callback)->
    success = (list)=>
      #@store.find('list', 'current').deleteRecord() # gets not replaced, buty stays as dummy

      # A list record with id current and with the content of the returned list is created
      # at the moment remove the dummy list, this should be resolved by Ember eventually
      if error_list = @store.all('list').findBy('id', 'current')
        error_list.eraseRecord()
      @set 'list', list
      @controllerFor('active_list').set('model', list)
      if list.get('join_requests').toArray().length
        @transitionToRoute 'join_requests'
      callback.call(@) if callback
    error = @ajaxError (emberError)=>
      # if jqXHR.status == 404 officially, now assume close list on error
      #@redirect_to 'index', message: 'the_list_has_been_closed'
      console.log "Error: #{emberError.message}" if emberError.message
      if error_list = @store.all('list').findBy('id', 'current')
        error_list.eraseRecord()
      @set 'list', null
    @store.find('list', 'current').then(success, error)
  didInsertElement: (e)->
    #debugger
