App.ApplicationController = Ember.Controller.extend
  #notice: ''
  actions:
    confirmCancel: ->
      if cancel = @get('confirm.cancel')
        cancel.call(@)
      $('#confirm-modal').hide()
    clearNotice: ->
      @set 'globals.notice', ''
    showSupplierStatusInfo: ->
      @modal 'supplier_status_info',
        model: @get('globals.active_list.supplier')
        title_path: 'supplier_status_info.title'

    openDebugger: ->
      debugger

  currentPathDidChange: (->
    @set 'globals.notice', ''
  ).observes('currentPath')
  events:
    notify: (notification) -> @set 'globals.notice', notification.message
    list_helped: -> @set 'globals.active_list.needs_help', false
    list_needs_help: -> @set 'globals.active_list.needs_help', true # incoming from other users
    list_is_paid: -> @set 'globals.active_list.needs_payment', false
    list_needs_payment: -> @set 'globals.active_list.needs_payment', true # incoming from other users
    list_closed: (data)->
      @transitionToRoute('list', data.id).then =>
        @set 'globals.active_list.state', 'closed'
        @set 'globals.active_list', null
    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.peekRecord('list', data.join_request.list_id)
      user_data = data.users[0]
      user = @store.peekRecord('user', 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'
    order_cancelled: (data)->
      if order = @store.peekRecord('order', data.id)
        order.markCancelled()
      @events.orders_placed_count.call(@, count: data.supplier_orders_placed_count) if data.supplier_orders_placed_count == 0 or data.supplier_orders_placed_count
      @events.orders_in_process_count.call(@, count: data.supplier_orders_in_process_count) if data.supplier_orders_in_process_count == 0 or data.supplier_orders_in_process_count
    join_request_rejected: (data)->
      # Remove join request from connected users
      join_request = @store.peekRecord('join_request', 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 'globals.notice', t('join_request.requestor.join_request_rejected')
        @set 'globals.join_request_sent', false
      else
        @transitionToRoute 'active_list'


    join_request_approved: (data)->
      return @transitionToRoute 'active_list' if @get('globals.active_list.id') # Not interested when there is an active list
      # The rest of the code is for the new user
      @setCurrentList ->
        @transitionToRoute('active_list').then =>
          @set 'globals.notice', t('join_request.requestor.join_request_approved')
          @set 'globals.join_request_sent', false

    list_changed_table: (data)->
      @store.findRecord('table', data.to_table_id).then (table)=>@set('globals.active_list.table', table)
    orders_in_process_count: (data)->
      @set 'globals.active_list.supplier.orders_in_process_count', data.count
    orders_placed_count: (data)->
      @set 'globals.active_list.supplier.orders_placed_count', data.count
    new_order: (data)->
      # return if @store.all('order').findProperty('id', data.order.id)
      @store.pushPayload data
      @store.peekRecord('list', data.list.id).then (list)=> @set 'globals.active_list', list
      # @store.peekRecord('order', data.order.id).then (order)->
      #   list = order.get('list')
      #   list.get('orders').addObject(order)


    remove_list_needs_payment: (data)->
      if list = @store.peekRecord('list', data.id)
        list.set 'needs_payment', false
  setCurrentList: (callback)->
    success = (list)=>
      #@store.findRecord('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
      # TODO: keek checking if peekAll can be replaced by peekRecord
      if error_list = @store.peekAll('list', 'current').findBy('id', 'current') # peekRecord('list', 'current') not working in Ember 2.0.0
        error_list.eraseRecord()
      #TODO: depricate list on application controller
      #@set 'list', list
      @set 'globals.active_list', list
      if list.get('join_requests.length')
        @transitionToRoute 'join_requests'
      else if @currentRouteName is 'index'
        @transitionToRoute 'active_list'
      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.peekRecord('list', 'current')
        error_list.eraseRecord()
      @set 'globals.active_list', null
      switch @currentRouteName
        when 'table' then # nothing
        else @redirect_to 'user_root'

    @store.findRecord('list', 'current').then(success, error)
