// Generated by CoffeeScript 1.7.1
(function() {
  var fOnLoad;

  fOnLoad = function() {
    var editor, fErrHandler, fFetchModules, fUpdateModuleList, moduleType;
    document.title = 'Edit Modules';
    $('#pagetitle').text("{{{user.username}}}, edit your Modules!");
    moduleType = $('#module_type').val();
    $('#module_type').change(function() {
      moduleType = $(this).val();
      console.log(moduleType);
      return fFetchModules();
    });
    editor = ace.edit("editor");
    editor.setTheme("ace/theme/monokai");
    editor.setReadOnly(true);
    editor.setShowPrintMargin(false);
    fErrHandler = function(errMsg) {
      return function(err) {
        var fDelayed;
        if (err.status === 401) {
          return window.location.href = 'forge?page=edit_modules';
        } else {
          $('#moduleName').html("<h2>&nbsp;</h2>");
          $('#moduleLanguage').html("<b>&nbsp;</b>");
          editor.setValue("");
          fDelayed = function() {
            var msg, oErr;
            if (err.responseText === '') {
              msg = 'No Response from Server!';
            } else {
              try {
                oErr = JSON.parse(err.responseText);
                msg = oErr.message;
              } catch (_error) {}
            }
            $('#info').text(errMsg + msg);
            return $('#info').attr('class', 'error');
          };
          return setTimeout(fDelayed, 500);
        }
      };
    };
    fFetchModules = function() {
      var cmd;
      if (moduleType === 'Event Poller') {
        cmd = 'get_event_pollers';
      } else {
        cmd = 'get_action_invokers';
      }
      return $.post('/usercommand', {
        command: cmd
      }).done(fUpdateModuleList).fail(fErrHandler('Did not retrieve rules! '));
    };
    fUpdateModuleList = function(data) {
      var img, imgTwo, inp, modName, oMods, tr, _results;
      $('#tableModules tr').remove();
      oMods = JSON.parse(data.message);
      _results = [];
      for (modName in oMods) {
        tr = $('<tr>');
        img = $('<img>').attr('class', 'del').attr('src', 'red_cross_small.png');
        imgTwo = $('<img>').attr('class', 'log').attr('src', 'logicon.png');
        inp = $('<div>').text(modName);
        tr.append($('<td>').append(img));
        tr.append($('<td>').append(imgTwo));
        tr.append($('<td>').append(inp));
        _results.push($('#tableModules').append(tr));
      }
      return _results;
    };
    fFetchModules();
    $('#tableModules').on('click', 'img.del', function() {
      var cmd, data, modName;
      modName = $('div', $(this).closest('tr')).text();
      if (confirm("Do you really want to delete the Module '" + modName + "'? The module might still be active in some of your rules!")) {
        $('#moduleName').html("<h2>&nbsp;</h2>");
        $('#moduleLanguage').html("<b>&nbsp;</b>");
        editor.setValue("");
        if (moduleType === 'Event Poller') {
          cmd = 'delete_event_poller';
        } else {
          cmd = 'delete_action_invoker';
        }
        data = {
          command: cmd,
          payload: {
            id: modName
          }
        };
        data.payload = JSON.stringify(data.payload);
        return $.post('/usercommand', data).done(fFetchModules).fail(fErrHandler('Could not delete module! '));
      }
    });
    return $('#tableModules').on('click', 'img.log', function() {
      var cmd, data, modName;
      modName = $('div', $(this).closest('tr')).text();
      if (moduleType === 'Event Poller') {
        cmd = 'get_full_event_poller';
      } else {
        cmd = 'get_full_action_invoker';
      }
      data = {
        command: cmd,
        payload: {
          id: modName
        }
      };
      data.payload = JSON.stringify(data.payload);
      return $.post('/usercommand', data).done(function(data) {
        var err, oMod;
        try {
          oMod = JSON.parse(data.message);
        } catch (_error) {
          err = _error;
          fErrHandler(err.message);
        }
        if (oMod.lang === 'CoffeeScript') {
          editor.getSession().setMode("ace/mode/coffee");
        } else {
          editor.getSession().setMode("ace/mode/javascript");
        }
        editor.setValue(oMod.data);
        editor.gotoLine(1, 1);
        editor.scrollToRow(1);
        $('#moduleName').html("<h2>" + oMod.id + "</h2>");
        return $('#moduleLanguage').html("<b>" + oMod.lang + "</b>");
      }).fail(fErrHandler('Could not get module! '));
    });
  };

  window.addEventListener('load', fOnLoad, true);

}).call(this);
