App.SectionController = Ember.Controller.extend
  #needs: ['application', 'sections', 'section'] #wtf? section, otherwise an Ember error
  actions:
    makeEditable: ->
      @set('model.editmode', true)
      false
    finishEditable: ->
      @set('model.editmode', false)
      @get('model').save()
      @get('model.tables').filterBy('hasDirtyAttributes').invoke 'save'
      @get('model.section_elements').filterBy('hasDirtyAttributes').invoke 'save'
      @get('model.section_areas').filterBy('hasDirtyAttributes').invoke 'save'
    rollbackEditable: ->
      @get('model').rollbackAttributes()
      tables = @get('model.tables')
      tables.filterBy('isNew').invoke 'unloadRecord'
      tables.rejectBy('isNew').invoke 'rollbackAttributes'

      section_elements = @get('model.section_elements')
      section_elements.filterBy('isNew').invoke 'unloadRecord'
      section_elements.rejectBy('isNew').invoke 'rollbackAttributes'

      section_areas = @get('model.section_areas')
      section_areas.filterBy('isNew').invoke 'unloadRecord'
      section_areas.rejectBy('isNew').invoke 'rollbackAttributes'
      @set('model.editmode', false)
    addSection: -> @modal 'add_section', model: @get('model')
    addTables: -> @modal 'section_add_tables', model: @get('model')
    arrangeTables: -> @modal 'section_arrange_tables', model: @get('model')
    destroySection: ->
      @modal 'confirm',
        title_path: 'helpers.links.are_you_sure'
        ok: =>
          @get('model').destroyRecord()
          @transitionToRoute 'sections'
    addSectionElement: ->
      @modal 'add_section_element', model: @get('model')
    addSectionArea: ->
      section_area = @store.createRecord('section-area')
      section_area.set 'section', @get('model')
      #@set 'model.editmode', true
      @modal 'section_area',
        title_path: 'section_area.add_button'
        model: section_area
  textures: ['wood1', 'wood2']

  #sections: (-> @get('controllers.sections.model')).property('controllers.sections.model')
  sections_controller: Ember.inject.controller('sections')
  sections: Ember.computed.alias 'sections_controller.model'
