const PINGFM_STATUS_MAXLEN = 140;

var noun_type_userapikey = {
    _name: "user api key",
    
    hasKey: function(key) {
        return Application.prefs.has("pingfm_user_api_key");
    },
    
    setKey: function(key) {
        if (!Application.prefs.has("pingfm_user_api_key")) {
            Application.prefs.setValue("pingfm_user_api_key", key);
        } else {
            var new_key = Application.prefs.get("pingfm_user_api_key");
            new_key.value = key;
            return new_key.value;
        }
    },
    
    suggest: function(text, html) {
        var suggestions = [];
        var key = Application.prefs.get("pingfm_user_api_key");
        return key.value;
    }
}

CmdUtils.CreateCommand({
  name: "ping.fm",
  takes: {status: noun_arb_text, "user_api_key": noun_type_userapikey},
  
  homepage: "http://trodrigues.net/ubiquity/pingfm/",
  author: {name: "Tiago Rodrigues", homepage: "http://trodrigues.net"},
  license: "MPL",
  description: "Post to Ping.fm using Ubiquity",
  help: "Type ping.fm your message to post.",
  
  preview: function(previewBlock, statusText) {
    var previewTemplate = "Updates your Ping.fm default status to: <br/>" +       
                          "<b>${status}</b><br /><br />" + 
                          "Characters remaining: <b>${chars}</b>";
    var truncateTemplate = "<br />The last <b>${truncate}</b> " + 
                           "characters will be truncated!";
    var previewData = {
      status: statusText.text,
      chars: PINGFM_STATUS_MAXLEN - statusText.text.length
    };
      
    var previewHTML = CmdUtils.renderTemplate(previewTemplate,
                                                    previewData);
    
    if(previewData.chars < 0) {
      var truncateData = {
        truncate: 0 - previewData.chars
      };
      
      previewHTML += CmdUtils.renderTemplate(truncateTemplate,
                                                   truncateData);
    }
    
    previewBlock.innerHTML = previewHTML;
  },
  
  execute: function(statusText) {
    if(statusText.text.length < 1) {
      displayMessage("Ping.fm requires a status to be entered");
      return;
    }
    
    if (noun_type_userapikey.hasKey()) {
        var updateUrl = "http://api.ping.fm/v1/user.post";
        var status = statusText.text;
        status = status.replace(/noun_type_userapikey.suggest()/, "");
        var updateParams = {
          api_key: "bef7d6764a53e14e614a6920ef0083cb",
          user_app_key: noun_type_userapikey.suggest(),
          post_method: "default",
          body: status
        };
        
        jQuery.ajax({
          type: "POST",
          url: updateUrl,
          data: updateParams,
          dataType: "text",
          error: function() {
            displayMessage("Ping.fm error - status not updated");
          },
          success: function() {
            displayMessage("Ping.fm default status updated");
          }
        });
    } else {
        displayMessage("Please set your user api key with ping.fm-setkey");
    }
  }
});

CmdUtils.CreateCommand({
  name: "ping.fm-setkey",
  takes: {status: noun_arb_text, "user_api_key": noun_type_userapikey},
  
  homepage: "http://trodrigues.net/ubiquity/pingfm/",
  author: {name: "Tiago Rodrigues", homepage: "http://trodrigues.net"},
  license: "MPL",
  description: "Set your user api key for the ping.fm command. Check your key at http://ping.fm/key",
  help: "Type ping.fm-setkey <user api key>. Check your key at http://ping.fm/key",
  
  preview: function(previewBlock, statusText) {
    var previewTemplate = "Set your Ping.fm user api key to: <br/>" +       
                          "<b>${status}</b><br /><br />";
    
    var previewData = {
      status: statusText.text,
    };
  },
  
  execute: function(statusText) {
    if(statusText.text.length < 1) {
      displayMessage("This command requires a Ping.fm user api key to be entered");
      return;
    }
    
    // store our api key on first run
    noun_type_userapikey.setKey(statusText.text);
    displayMessage("Your user api key has been set.");
  }
});
