App.modals.SectionArrangeTablesController = App.modals.BaseController.extend
  title_path: 'section.arrange_tables.modal.title'
  arrange_type: 'distributed' # can be distributed, by_row or by_column
  row_count: 2
  column_count: 2
  alert_message: ''
  #isDistributed: ~> @arrange_type is 'distributed'
  #isByRow: ~> @arrange_type is 'by_row'
  #isByColumn: ~> @arrange_type is 'by_column'
  isDistributed: (->@get('arrange_type') is 'distributed').property('arrange_type')
  isByRow: (->@get('arrange_type') is 'by_row').property('arrange_type')
  isByColumn: (->@get('arrange_type') is 'by_column').property('arrange_type')
  canArrangeTables: (->
    unless parseFloat(@get('model.width')) > 0 and parseFloat(@get('model.height')) > 0
      @set 'alert_message', t('section.arrange_tables.modal.cannot_arrange')
      return false
    @set 'alert_message', ''
    true
  ).property('model.width', 'model.height')
  actions:
    arrangeTables: ->
      return unless @isValid()
      switch @get('arrange_type')
        when 'by_row'
          @get('model').invoke 'arrange_tables_in_rows_of', parseInt(@get('row_count'))
        when 'by_column'
          @get('model').invoke 'arrange_tables_in_columns_of', parseInt(@get('column_count'))
        else
          @get('model').invoke 'arrange_tables_in_grid'
      @send 'close'

    makeDistributed: ->
      @set 'arrange_type', 'distributed'
      @set 'alert_message', ''
    makeByRow: ->
      @set 'arrange_type', 'by_row'
      @set 'alert_message', ''
    makeByColumn: ->
      @set 'arrange_type', 'by_column'
      @set 'alert_message', ''
  isValid: ->
    switch @get('arrange_type')
      when 'by_row'
        if parseInt(@get('row_count')) < 1
          @set 'alert_message', "Must at least be 1"
          return false
      when 'by_column'
        if parseInt(@get('column_count')) < 1
          @set 'alert_message', "Must at least be 1"
          return false
    true
