import Ember from 'ember'
export default Ember.Controller.extend
  alert_message: ""
  modal_options: {}
  layoutName: 'modals/layout'
  willOpenModal: -> # can be implemented, is fired after loading attributes, before rendering
  title: (->
    # return title if directly set by options
    return @get('modal_options.title') if @get('modal_options.title')
    # return translated title_path if directly set by controller
    translation_params = {}
    if title_params = @get('modal_options.title_params')
      translation_params = title_params
    else if model = @get('model')
      translation_params = model.toJSON() if model.toJSON

    # return translated title_path if directly set by options
    # return translated title_path if directly set by controller
    if title_path = @get('modal_options.title_path') || @get('title_path')
      return tspan(title_path, translation_params).htmlSafe()
    # return translated title_path if directly set by options
    #return tspan(@get('modal_options.title_path'), translation_params).htmlSafe() if @get('modal_options.title_path')
    # return translated title_path if directly set by controller
    #return tspan(@title_path, translation_params).htmlSafe() if @title_path
    # infer title path based on controller name App.modals.AddSectionController => add_section
    #underscored = `this.constructor.toString().substr(11).replace(/Controller$/, '').underscore()`
    underscored = @constructor.toString().replace(/.*controller:/, '').replace(/\W+$/, '').underscore().replace('/', '.')
    #underscored = @constructor.toString().replace(/[a-zA-Z_-]+\@controller:modals\//, '').replace(/:$/, '').underscore() # ember cli
    # find translated title or humanize the controller name
    if convention_translation = ttry("#{underscored}.title", translation_params)
      tspan("#{underscored}.title", translation_params).htmlSafe()
    else
      underscored.replace(/.*\./,'').capitalize().replace(/_/, ' ')
  ).property('model.id', 'modal_options.title_path')
  body: (->
    @get('modal_options.body')
  ).property('modal_options.body')
  save_error: (error)->
    switch error.status
      when 403
        @set 'alert_message', 'Unauthorized action'
      else
        @set 'alert_message', 'Something went wrong'
  save_success: ->
    if save_callback = @get('modal_options.save')
      save_callback.apply(@)
    @set 'alert_message', ''
    @send 'closeModal'

  actions:
    close: ->
      if close = @get('modal_options.close')
        close.apply(@)
      unless @preventClose
        @set 'alert_message', ''
        @send 'closeModal'
    closeOnOverlay: ->
      @send('close') if @get('modal_options.closeOnOverlay')
      false
    modalClick: ->
      @send('close') if @get('modal_options.closeOnModalClick')
      false
    ok: ->
      if ok = @get('modal_options.ok')
        ok.apply(@)
      @set 'alert_message', ''
      @send 'closeModal' unless @preventClose
    confirm: -> @send('ok')
    saveRecord: ->
      if @get('model.isValid')
        @get('model').save().then @save_success.bind(@), @save_error.bind(@)
        #@send 'closeModal' unless @preventClose
    rollback_and_close: ->
      @get('model').rollbackAttributes()
      @send 'close'
    destroyRecord: ->
      my_scope = @
      destroy_callback = @get('modal_options.destroy_callback')
      @modal 'confirm',
        title_path: @get('modal_options.destroy_text_path') || 'general.destroy.are_you_sure'
        model: @get('model')
        ok: ->
          @get('model').destroyRecord()
          destroy_callback.call(my_scope) if destroy_callback
          @send 'closeModal' unless @preventClose
