// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
// SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
// OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

MouseApp.Lua = function(element, options) {
  this.element = $(element);
  this.setOptions(options);
  if ( this.options.init ) {
      this.init = this.options.init;
  }
  this.initWindow();
  this.setup();
  this.ml = false;
  this.luaInit = false;
};

$.extend(MouseApp.Lua.prototype, MouseApp.Terminal.prototype, {
    fireOffCmd: function(cmd, func) {
      var lua = this;
      $.ajax({
        error: function(XMLHttpRequest, textStatus, errorThrown) {
          if (XMLHttpRequest.status == 404) {
            lua.reply({error: "Try Lua! is temporarily unavailable"});
          } else if (XMLHttpRequest.status == 500) {
            lua.reply({error: "500 Internal server error"});
          } else {
            lua.reply({error: "Unexpected error on server"});
          }
        },
        url: this.options.luaUrl,
        success: func,
        data: {cmd: cmd},
        dataType: 'json'
      });
    },

    reply: function(res, textStatus) {
      this.ml = false;
      if (res.error != "") {
        this.write("\033[1;4m"+res.error+"\033[m\n");
        this.prompt();
      } else if (res.text == ">>") {
        this.prompt(">>", true);
        this.ml = true;
      } else if (res.text == "") {
        this.prompt();
      } else if ($.isArray(res.text)) {
        for (i = 0; i < res.text.length; i++) {
          var str = res.text[i].replace('\\t', '    ');
          if (str == "" && i == (res.text.length-1)) {
            this.write("\033[0;8m" + str + "\033[m");
          } else {
            if (str == "") {
              str = " ";
            }
            this.write("\033[0;8m" + str + "\033[m\n");
          }
        }
        this.prompt();
      } else {
        this.prompt();
      }
      if (res.step != "") {
        $("#help").hide("fast").empty().html(res.step).show("fast");
      }
    },

    onKeyEnter: function() {
        this.typingOff();
        var cmd = this.getCommand();
        if (cmd) {
          this.history[this.historyNum] = cmd;
          this.backupNum = ++this.historyNum;
        }
        this.commandNum++;
        this.advanceLine();
        if (cmd) {
          var term = this;
          this.fireOffCmd(cmd, (function(res) { term.reply(res) }));
        } else {
          if (this.ml == true) {
            this.prompt(">>", true);
            this.ml = true;
          } else {
            this.prompt();
          }
        }
    }
});
