// max of 140 chars is recommended, but it really allows 160...
// but that gets truncated on some displays? grr
const TWITTER_STATUS_MAXLEN = 140; 

CmdUtils.CreateCommand({
  names: ["yfd", "log flowing data"],
  arguments: [
    {role: "object", label: 'data', nountype: noun_arb_text},
    {role: "alias", nountype: noun_type_twitter_user}
  ],
  icon: "http://flowingdata.com/wp-content/themes/flowingdata-2-4/images/favicon.ico",
  description:
  "Updates Your.FlowingData via Twitter.",
  help: ("You'll need a <a href=\"http://twitter.com\">Twitter account</a>," +
         " obviously, that mutual friends with @yfd.  If you're not already logged in" +
         " you'll be asked to log in."),
  preview: function(previewBlock, args) {
    var dataText = (args.object ? args.object.text : '');
    var usernameText = "";
    if (args.alias) {
      usernameText = args.alias.text;
    } else if (args.as) {
      usernameText = args.as.text;
    }
    if (usernameText[0] == '@')
      usernameText = usernameText.slice(1);
    
    var previewTemplate = (
      "<div class='twitter'>"+_("Sends direct message via Twitter ${username}:")+"<br/>" +
      "<b class='status'>${data}</b><br/><br/>" +
      _("Characters remaining: <b>${chars}</b>") +
      "</div>");
    var truncateTemplate = (
      "<strong>"+_("The last <b>${truncate}</b> characters will be truncated!")+"</strong>");
    var previewData = {
      data: <>{dataText}</>.toXMLString(),
      username: usernameText && _("(As user <b>${usernameText}</b>)"),
      chars: TWITTER_STATUS_MAXLEN - dataText.length - "d yfd ".length
    };

    var previewHTML = CmdUtils.renderTemplate(
                        CmdUtils.renderTemplate(previewTemplate, previewData),
                        {usernameText:usernameText});

    if (previewData.chars < 0) {
      var truncateData = {
        truncate: 0 - previewData.chars
      };

      previewHTML += CmdUtils.renderTemplate(truncateTemplate, truncateData);
    }

    previewBlock.innerHTML = previewHTML;
  },
  execute: function(args) {
    var dataText = args.object.text;
    if(dataText.length < 1) {
      this._show(_("requires log data to be entered"));
      return;
    }

    var updateUrl = "https://twitter.com/statuses/update.json";
    var updateParams = {
      source: "ubiquity",
      status: "d yfd " + dataText
      //dont cut the input since sometimes, the user selects a big url,
      //and the total lenght is more than 140, but tinyurl takes care of that
    };
    var me = this;

    function sendMessage() {
      jQuery.ajax({
        type: "POST",
        url: updateUrl,
        data: updateParams,
        dataType: "json",
        error: function() {
          me._show(_("error - message not sent"));
        },
        success: function() {
          me._show(_("direct message sent"));
        },
        username: login.username,
        password: login.password
      });
    }

    var login;
    var alias = args.alias;
    if (alias && alias.text && alias.data) {
      login = alias.data;
      sendMessage();
    } else {
      login = {username: null,
               password: null};
      if (alias && alias.text)
        login.username = alias.text;
      sendMessage();
    }
  },
  _show: function(txt){
    displayMessage({icon: this.icon, title: this.name, text: txt});
  }
});
