 class Fustrate.Components.Modal extends Fustrate.Components.Base
-  @background: undefined
   @fadeSpeed: 250
-  @openModal: undefined
 
   @settings:
     closeOnBackgroundClick: true


  constructor: ({title, content, size, settings}) ->
    @modal = @constructor.createModal size: size
    @setTitle title
    @setContent content

    @settings = $.extend true, @constructor.settings, (settings ? {})

    @locked = false
    @previousModal = undefined

    @_reloadUIElements()
    @addEventListeners()

    super

  @createModal: ({size}) ->
    $ """
      <div class="modal #{size ? 'tiny'}">
        <div class="modal-title">
          <span></span>
          <a href="#" class="modal-close">&#215;</a>
        </div>
        <div class="modal-content"></div>
      </div>"""

  _reloadUIElements: =>
    @fields = {}
    @buttons = {}

    $('[data-field]', @modal).each (index, element) =>
      field = $ element
      @fields[field.data('field')] = field

    $('[data-button]', @modal).each (index, element) =>
      button = $ element
      @buttons[button.data('button')] = button

  setTitle: (title) =>
    $('.modal-title span', @modal).html title

  setContent: (content) =>
    $('.modal-content', @modal).html content

    @_reloadUIElements()

  addEventListeners: =>
    @modal
      .off '.modal'
      .on 'hide.modal close.modal', @close
      .on 'show.modal open.modal', @open
      .on 'opened.modal', @focusFirstInput
      .on 'click.modal', '.modal-close', @closeButtonClicked

    # TODO: Re-enable when modals are fully converted
    #   .off '.modal'
    $(document)
      .on 'click.modal touchstart.modal', '.modal-overlay', @backgroundClicked

  backgroundClicked: (e) =>
    return if !@constructor.openModal || @constructor.openModal.locked

    # Don't continue to close if we're not supposed to
    return unless @constructor.openModal.settings.closeOnBackgroundClick

    e.preventDefault()
    e.stopPropagation()

    @close()

    false

  closeButtonClicked: (e) =>
    return if !@constructor.openModal || @constructor.openModal.locked

    e.preventDefault()

    @close()

    false

  focusFirstInput: =>
    # Focus requires a slight physical scroll on iOS 8.4
    return true if /iPad|iPhone|iPod/g.test navigator.userAgent

    $('input, select', @modal)
      .filter(':visible:not(:disabled):not([readonly])')
      .first()
      .focus()

  @toggleBackground: (visible = true) =>
    @overlay = $ '<div class="modal-overlay">' unless @overlay

        .fadeOut @fadeSpeed, ->
          $(@).detach()

  @backgroundClicked: (e) ->
    modal = $ '.modal.open'

    return if !modal || modal.hasClass('locked')

    # Don't continue to close if we're not supposed to
    return unless Fustrate.Components.Modal.settings.closeOnBackgroundClick

    e.preventDefault()
    e.stopPropagation()

    modal.trigger 'close.modal'

    false

  @closeButtonClicked: (e) ->
    modal = $ '.modal.open'

    return if !modal || modal.hasClass('locked')

    e.preventDefault()

    modal.trigger 'close.modal'

    false
