function SettingsCommandList() {
}

function validChar(keyCode) {
  return (keyCode >= 'A'.charCodeAt(0) && keyCode <= 'Z'.charCodeAt(0)) ||
         (keyCode >= '0'.charCodeAt(0) && keyCode <= '9'.charCodeAt(0));
}

function keystrokeToString(event) {
  var output = '';
  if (event.ctrlKey)
    output = 'Ctrl+';
  if (!event.ctrlKey && event.altKey)
    output += 'Alt+';
  if (event.shiftKey)
    output += 'Shift+';
  if (validChar(event.keyCode))
    output += String.fromCharCode('A'.charCodeAt(0) + event.keyCode - 65);
  return output;
}

SettingsCommandList.prototype = {
  currentKeyEvent_: null,
  oldValue_: '',
  oldSessionValue_ : '',
  capturingElement_: null,
  curBossKey_ : '',

  initialize: function(jq) {
    this.$ = jq;
  },

  updateCurBossKey : function(value){
    this.oldValue_ = value;
    $("#boss_key_text").text(this.oldValue_);
  },

  /**
   * Synthesizes and initializes an HTML element for the extension command
   * metadata given in |command|.
   * @param {Object} command A dictionary of extension command metadata.
   * @private
   */
  initNodeForCommand_: function(shortcutNode) {
    chrome.send('setShortcutHandlingSuspended', [true]);
    $(shortcutNode).bind("mouseup", this.startCapture_.bind(this));
    $(shortcutNode).bind("blur", this.endCapture_.bind(this));
    $(shortcutNode).bind("keydown", this.handleKeyDown_.bind(this));
    $(shortcutNode).bind("keyup", this.handleKeyUp_.bind(this));
    this.oldSessionValue_ = this.oldValue_;
    $(shortcutNode).val(this.oldValue_);
    $("#boss_key_text").text(this.oldValue_);
  },

  resetBosskeyCfgSession_ :function(){
    this.oldValue_ = this.oldSessionValue_;
    chrome.send('setBosskeyCommandShortcut', [this.oldSessionValue_]);
    $("#boss_key_text").text(this.oldValue_);
  },

  handleFocus_ : function(event){
    var shortcutNode = event.target;
    shortcutNode.textContent = "";
  },

  handleBlur_ : function(event){
    debugger;
    var shortcutNode = event.target;
    shortcutNode.textContent = this.oldValue_;
  },
  /**
   * Starts keystroke capture to determine which key to use for a particular
   * extension command.
   * @param {Event} event The keyboard event to consider.
   * @private
   */
  startCapture_: function(event) {
    if (this.capturingElement_)
      return;  // Already capturing.
    var shortcutNode = event.target;
    $(shortcutNode).val("");
    this.capturingElement_ = event.target;
  },

  /**
   * Ends keystroke capture and either restores the old value or (if valid
   * value) sets the new value as active..
   * @param {Event} event The keyboard event to consider.
   * @private
   */
  endCapture_: function(event) {
    debugger;
    if (!this.capturingElement_)
      return;  // Not capturing.
    var shortcutNode = this.capturingElement_;
    if (!this.currentKeyEvent_ || !validChar(this.currentKeyEvent_.keyCode))
      $(shortcutNode).val(this.oldValue_);
    this.capturingElement_ = null;
    this.currentKeyEvent_ = null;
  },

  /**
   * The KeyDown handler.
   * @param {Event} event The keyboard event to consider.
   * @private
   */
  handleKeyDown_: function(event) {
    if (!this.capturingElement_)
      this.startCapture_(event);

    this.handleKey_(event);
  },

  /**
   * The KeyUp handler.
   * @param {Event} event The keyboard event to consider.
   * @private
   */
  handleKeyUp_: function(event) {
    if (!this.currentKeyEvent_ || !validChar(this.currentKeyEvent_.keyCode)) {
      if (!event.ctrlKey && !event.altKey) {
        this.endCapture_(event);
        //this.startCapture_(event);
      } else {
        this.handleKey_(event);
      }
    }
  },

  /**
   * A general key handler (used for both KeyDown and KeyUp).
   * @param {Event} event The keyboard event to consider.
   * @private
   */
  handleKey_: function(event) {
    event.preventDefault();
    event.stopPropagation();

    if (!event.ctrlKey && !event.altKey)
      return;  // Ctrl or Alt is a must (or Cmd on Mac).

    var shortcutNode = this.capturingElement_;
    var keystroke = keystrokeToString(event);
    shortcutNode.textContent = keystroke;

    if (validChar(event.keyCode)) {
      debugger;
      var node = event.target;
      this.oldValue_ = keystroke;  // Forget what the old value was.
      $("#boss_key_text").text(this.oldValue_);
      chrome.send('setBosskeyCommandShortcut', [keystroke]);
      this.endCapture_(event);
    }

    this.currentKeyEvent_ = event;
  },

  /**
   * A handler for the delete command button.
   * @param {Event} event The mouse event to consider.
   * @private
   */
  handleClear_: function(event) {
    chrome.send('setBosskeyCommandShortcut',['']);
  }
};

