/*
Ping.fm Ubiquity
By Waleed Zuberi
7/13/2009 3:11:39 AM

@todo: Improve functionality, for version 2.0 of the script
*/


const PINGFM_STATUS_MAXLEN = 140;

CmdUtils.CreateCommand({
	names: ["pingfm"],
	description: "Update your status to various social networks using Ping.fm",
	arguments: [{ role: "object", label: 'status', nountype: noun_arb_text }],

	icon: 'http://ping.fm/favicon.ico',
	homepage: "http://waleed.doubleudesigns.com/",
	author: {name: "Waleed Zuberi", homepage: "http://waleed.doubleudesigns.com/"},
	license: "MPL",

	user_api_key: function() {
		var key = Application.prefs.getValue("Ping.fm api key", "");
		if (key != "") {
			return key;
		}
		else {
			return null;
		}
	},

	preview: function(pblock, args) {
		var key = this.user_api_key();

		if (key != null) {
			var previewTemplate = "Posts this to your Ping.fm account: <br/> <b>${status}</b> <br /><br />Characters remaining: <b>${chars}</b>";
			var truncateTemplate = "<br />Some services have a "+ PINGFM_STATUS_MAXLEN +" character limit, which we need to respect. The last <b>${truncate}</b> characters will be cut off!";

		    var previewData = {
				status: args.object.text,
				chars: PINGFM_STATUS_MAXLEN - args.object.text.length
			};

			var previewHTML = CmdUtils.renderTemplate(previewTemplate, previewData);

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

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

			pblock.innerHTML = previewHTML;

		}
		else {
			pblock.innerHTML = "Please set your Ping.fm app key with the \"pingfm-key\" command before using this.";
		}
	},

	execute: function(args) {
		if(this.user_api_key() == null) {
			displayMessage("Your Ping.fm app key is required to be able to post. Use the \"pingfm-key\" command to set it.");
			return;
		}
		if(args.object.text.length < 1) {
			displayMessage("It seems you're tyring to update an empty status - Ping.fm requires that something be entered!");
			return;
		}

		var updateUrl = "http://api.ping.fm/v1/user.post";
		var updateParams = {
			source: "ubiquity",
			post_method: "default",
			body: args.object.text,
			api_key: "87159a770615f46ec17c5f16b292b66e",
			user_app_key: this.user_api_key()
		};

		jQuery.post(updateUrl,updateParams,
			function(data) {
				var response = data.documentElement.getAttribute("status");
				if (response == "OK")
				{
					displayMessage("Yaay! Updated your status to Ping.fm!");
				}
				else
				{
					displayMessage("Sorry, something didn't happen right. Your status could not be updated. Try again?");
				}
			}, "xml");
	}
});

CmdUtils.CreateCommand({
	names: ["pingfm-key"],
	icon: 'http://ping.fm/favicon.ico',

	arguments: [{ role: "object", label: 'api_key', nountype: noun_arb_text }],
	description: "Saves your Ping.fm app key",
	help: 'To set your key, copy it from http://ping.fm/key and use the command, "pingfm-key <i>[paste your key here]</i>. Type "ping.fm-key clear" if you want to erase the locally saved app key."',

	validate_key: function(key) {
		if (/[a-f0-9]{32}-[0-9]{10}/.test(key)) {
			return true;
		}
		else {
			return false;
		}
	},

	preview: function(pblock, args, data) {
		var set_key = Application.prefs.getValue("Ping.fm api key", "");

		if (set_key != "") {
			pblock.innerHTML = "Your currently saved Ping.fm app key is: <br /><b>" + set_key + "</b><br /><br />You can set a new one, which will replace the one currently saved.";
		}

		else {
			var data = {};
 			data.key = args.object.text;

			pblock.innerHTML = CmdUtils.renderTemplate("Set your Ping.fm app key. You can get it from <a href=\"http://ping.fm/key/\">http://ping.fm/key</a><br/><br />Entering will save this key: <br /> <strong>${key}</strong>", data);
		}
	},

	execute: function(args, key) {
		var updateUrl = "http://api.ping.fm/v1/user.validate";
		var data = {};
 		data.key = args.object.text;

		if ( this.validate_key(data.key) )
		{
			var updateParams = {
				api_key: "87159a770615f46ec17c5f16b292b66e",
				user_app_key: data.key
			};
			jQuery.post(updateUrl,updateParams,
				function(data) {
					response = data.documentElement.getAttribute("status");
					if (response == "OK")
					{
						Application.prefs.setValue("Ping.fm api key", args.object.text);
						displayMessage("Great, your key has been set. You can now post to Ping.fm!");
					}
					else
					{
						displayMessage("Hmm, the key doesn't seem to be valid. Confirm it from http://ping.fm/key/");
					}
				}, "xml");
		}
		else {
		    // failed local format check
			displayMessage("The entered key is invalid in format. Please recheck.");
		}
	}
});


CmdUtils.CreateCommand({
	names: ["pingfm-key-clear"],
	icon: 'http://ping.fm/favicon.ico',

	description: "Clears your saved Ping.fm appkey from this application",
	help: 'This command will clear your saved Ping.fm app key from this application."',

	preview: function(previewBlock) {
		previewBlock.innerHTML = "This command will clear your saved Ping.fm API key from this application.";
	},

	execute: function() {
	    var key = Application.prefs.getValue("Ping.fm api key", "");

	    if (key != "") {
			Application.prefs.setValue("Ping.fm api key", "");
			displayMessage("Your Ping.fm app key has been cleared from this application.");
		}
		else {
			displayMessage("Your Ping.fm app is currently not saved in this application.");
		}
	},
});
