App.ApplicationRoute = Ember.Route.extend
  beforeModel: ->
    # Preload only active lists and orders
    @supplier = @store.push
      data:
        id: supplier_object.id
        type: 'supplier'
        attributes: supplier_object
    @employee = @store.push
      data:
        id: employee_object.id
        type: 'employee'
        attributes: employee_object
    #@supplier = @store.push 'supplier' supplier_object
    #@employee = @store.push 'employee' employee_object
    #@product_categories = @store.findAll 'product_category'
    #@sections = @store.findAll 'section'
    #Ember.RSVP.all([@product_categories, @sections]).then (results)=>
    @supplier.reload().then =>
      @store.query('list', state: 'active').then => #.then (lists) -> lists.invoke('get', 'table')
        @controllerFor('index').set 'loading_data', false
       #@lists = @store.peekAll 'list'
    # product_categories = controller.set 'product_categories', @store.peekAll('product_category')
    #@store.findAll 'order', state: 'active' included in list
  setupController: (controller)->
    controller.set 'globals.current_supplier', @supplier
    controller.set 'globals.current_employee', @employee
    # @set 'supplier', @store.findRecord('supplier', supplier_id)
    #controller.set 'sections', @sections
    #controller.set 'product_categories', @product_categories

    faye = new Faye.Client(event_host)
    faye.subscribe "/supplier/#{supplier_object.id}", (e)=>
      @set('globals.current_supplier.orders_placed_count', e.data.supplier_orders_placed_count) if e.data.supplier_orders_placed_count?
      @set('globals.current_supplier.orders_in_process_count', e.data.supplier_orders_in_process_count) if e.data.supplier_orders_in_process_count?
      @events[e.event].call(@, e.data) if @events[e.event]
<% if Rails.env.test? %>
      window.faye_log ||= []
      faye_message = {}
      faye_message[e.event] = JSON.parse(JSON.stringify(e.data))
      faye_log.push faye_message
<% elsif Rails.env.development? %>
      console.log "Event: #{e.event}"
      console.log e.data
<% end %>

    # Fetch suppliers for current_employee
    $.getJSON(Routes.employees_suppliers_path()).then (result) =>
      @store.pushPayload result


  actions:
    # openModal: (modalName, model, options={})->
    #   controller_name = options.controller || modalName
    #   controller =  @controllerFor(controller_name)
    #   controller.set 'model', model
    #   controller.set 'modal_options', options
    #   @render modalName,
    #     into: 'application'
    #     outlet: 'modal'
    #     controller: controller_name
    openModal: (modalName, options={})->
      controller_name = options.controller || modalName
      try
        controller =  @controllerFor("modals/#{modalName}")
      catch error
        controller = @controllerFor("modals/base")
      controller ||= @controllerFor("modals/base")
      controller.set 'model', options.model
      defaultModalOptions =
        closeOnOverlay: true
        closeOnModalClick: false
      modal_options = Ember.Object.create($.extend(defaultModalOptions, options))
      controller.set 'modal_options', modal_options
      @render "modals/#{modalName}",
        into: 'application'
        outlet: 'modal'
        view: 'modal'
        controller: controller
      @set 'globals.modal_active', true

    closeModal: ->
      @disconnectOutlet
        outlet: 'modal'
        parentView: 'application'
      @set 'globals.modal_active', false
    confirm: (options = {})->
      @send 'openModal', 'confirm',
        model: Ember.Object.create
        title: options.title
        body: options.body
        cancel: options.cancel
        ok: options.ok
    #newOrder: (order_id)->
    #  if order = @store.peekRecord('order', order_id)
    #    return if @get('globals.active_section.id') and order.get('section.id') isnt @get('globals.active_section.id')
    #    $('body').addClass('new-order')
    #    @set 'globals.flash_message', order.get('display_with_table')
    #    setTimeout (-> $('body').removeClass('new-order')), 4000
    #    try ion.sound.play('water_droplet')
    showDashboardOrders: (section)->
      @set('globals.active_section', section)
      @transitionTo 'index'
    markSupplierClosed: ->
      return unless @get('globals.current_employee.manager')
      return unless supplier = @get('globals.current_supplier')
      @send 'openModal', 'confirm',
        title_path: 'supplier.close_for_orders_confirmation'
        model: supplier
        ok: -> supplier.close()
    markSupplierOpen: ->
      return unless @get('globals.current_employee.manager')
      return unless supplier = @get('globals.current_supplier')
      supplier.open_the_place()
    editTable: (table)->
      @send 'openModal', 'table_edit',
        model: table
        close: -> table.rollbackAttributes()
  events:
    list_needs_help: (data) ->
      if list = @store.peekRecord('list', data.id)
        list.isNeedingHelp()
        return if @get('globals.active_section.id') and list.get('section.id') isnt @get('globals.active_section.id')
        @set 'globals.flash_message', t('table.needs_help.flash_message', number: list.get('table.table_number') || -1)
        try ion.sound.play 'bell_ring'
    list_needs_payment: (data) ->
      if list = @store.peekRecord('list', data.id)
        list.isNeedingPayment()
        return if @get('globals.active_section.id') and list.get('section.id') isnt @get('globals.active_section.id')
        try ion.sound.play 'water_droplet_3'
    list_is_paid: (data) -> list.isPaid() if list = @store.peekRecord('list', data.id)
    new_order: (data) ->
      if data.payload
        @store.pushPayload(data.payload)
        if order_id = data.payload.data.id
          order = @store.peekRecord('order', order_id)
          return if @get('globals.active_section.id') and order.get('list.table.section.id') isnt @get('globals.active_section.id')
          @set 'globals.flash_message', order.get('display_with_table')
      try ion.sound.play('water_droplet')
    new_list: (data)->
      if data.payload
        @store.pushPayload(data.payload)
      try ion.sound.play('water_droplet')
    list_changed_table: (data)->
      @store.pushPayload(data.payload)
    list_closed: (data) -> list.isClosed() if list = @store.peekRecord('list', data.id)
    list_helped: (data) -> list.isHelped() if list = @store.peekRecord('list', data.id)
    remove_list_needs_payment: (data) -> list.set('needs_payment', false) if list = @store.peekRecord('list', data.id)
    order_closed: (data) -> order.isClosed() if order = @store.peekRecord('order', data.id)
    order_being_processed: (data) -> order.isActive() if order = @store.peekRecord('order', data.id)
    order_being_delivered: (data) -> order.isDelivered() if order = @store.peekRecord('order', data.id)
    order_cancelled: (data) -> order.isCancelled() if order = @store.peekRecord('order', data.id)
    orders_in_process_count: (data) -> @set('globals.current_supplier.orders_in_process_count', data.count)
    orders_placed_count: (data) -> @set('globals.current_supplier.orders_placed_count', data.count)

    #return new Ember.Handlebars.SafeString('<span class="highlight">' + escaped + '</span>');
    #"<span class='highlight'>#{escaped}</span>".htmlSafe()
