# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/

changed = false

successMessage = (title, msg = '', selector = '#messages') ->
  html = $('<div class="alert alert-success" style="display: none">')
    .append("<h4><i class=\"icon-star\"></i>#{title}</h4>")
    .append(msg)
  $(selector).append(html)
  html.fadeIn('slow')
    .delay(3000)
    .fadeOut('slow')

errorMessage = (msg, selector = '#messages') ->
  html = $('<div class="alert alert-error" style="display: none">')
    .append('<button type="button" class="close" data-dismiss="alert">×</button>')
    .append('<h4><i class="icon-exclamation-sign"></i>エラー</h4>')
    .append(msg)
  $(selector).append(html)
  html.fadeIn('slow')

$ ->
  saving = false

  textEditor = ace.edit('text-editor')
  textEditor.setTheme('ace/theme/clouds')
  textEditor.setShowInvisibles(true)

  textEditor.focus()
  textEditor.gotoLine(0, 0)

  textEditor.on 'change', (e) ->
    changed = true

  session = textEditor.getSession()
  session.setMode('ace/mode/ruby')
  session.setTabSize(2)
  session.setUseSoftTabs(true)

  $('#save-button').click (e) ->
    e.preventDefault()
    filename = $.trim($('#filename').val())
    if filename.length <= 0
      errorMessage('セーブする前にプログラムに名前をつけてね!')
      $('#filename').focus()
    else
      afterSave = ->
        changed = false
        successMessage('セーブしました')

        $.ajax
          url: '/source_codes/check'
          type: 'POST'
          data:
            source_code:
              data: session.getDocument().getValue()
          dataType: 'json'
          success: (data, textStatus, jqXHR) ->
            if data.length == 0
              successMessage('チェックしました', 'ただし、プログラムを動かすとエラーが見つかるかもしれません。')
            else
              for errorInfo in data
                do (errorInfo) ->
                  msg = "#{errorInfo.row}行"
                  if errorInfo.column > 0
                    msg += "、#{errorInfo.column}文字"
                  errorMessage(msg + ": #{errorInfo.message}")

      $.ajax
        url: '/source_codes/'
        type: 'POST'
        data:
          source_code:
            filename: filename
            data: session.getDocument().getValue()
        dataType: 'json'
        success: (data, textStatus, jqXHR) ->
          <% if Rails.env == 'standalone' %>
          $.ajax
            url: '/source_codes/write'
            type: 'DELETE'
            dataType: 'json'
            success: (data, textStatus, jqXHR) ->
              if data.source_code.error
                if confirm("前に#{filename}という名前でセーブしているけど本当にセーブしますか?\nセーブすると前に作成したプログラムは消えてしまうよ!")
                  $.ajax
                    url: '/source_codes/write?force=1'
                    type: 'DELETE'
                    dataType: 'json'
                    success: (data, textStatus, jqXHR) ->
                      afterSave()
                else
                  successMessage('セーブをキャンセルしました')
              else
                afterSave()
          <% else %>
          afterSave()
          saving = true
          $('#download-link').click()
          <% end %>

  $('#filename').keypress (e) ->
    e = window.event if !e
    if e.keyCode == 13
      $('#save-button').click()
      false
    else
      true

  $('#load-button').click (e) ->
    e.preventDefault()
    if changed
      return unless confirm('まだセーブしていないのでロードするとプログラムが消えてしまうよ!\nそれでもロードしますか?')
    $(@).parent().find('input[name="source_code[file]"]').click()

  $('#file-form').fileupload
    dataType: 'json'
    done: (e, data) ->
      info = data.result.source_code
      if info.error
        errorMessage(info.filename + 'は' + info.error)
      else
        $('#filename').val(info.filename)
        session.getDocument().setValue(info.data)
        textEditor.focus()
        textEditor.moveCursorTo(0, 0)
        changed = false
        successMessage('ロードしました')

  window.onbeforeunload = (event) ->
    if !saving && session.getDocument().getValue().length > 0
      return '作成中のプログラムが消えてしまうよ!'
    else
      saving = false
      return