/*
 * Copyright 2008-2009 Palm, Inc. All rights reserved.
 */

UrlUtil = {};

/**
 * Convert "domain.tld" to "www.domain.tld". This is a pretty
 * stupid function at present and only looks for a single dot.
 * 
 * @param hostname The hostname to "fixup".
 */
UrlUtil.fixupHost = function(hostname) {

	try {
		var numDots = 0;
		for (var i = 0; i < hostname.length; i++) {
			if (hostname[i] === '.') {
				numDots = numDots + 1;
			}
		}

		if ( numDots === 1 ) {
			return 'www.' + hostname;
		}
		else {
			return hostname;
		}
	}
	catch (e) {
		return hostname;
	}
};

/**
 * Fixup the resource portion of a URL. By fix
 * @param {String} resource
 */
UrlUtil.fixupResource = function(resource) {
	var len = resource.length;
	if (len < 1) {
		return resource;
	}
	if (resource[len-1] == '/') {
		return resource.substr(0, len-1);
	}
	else {
		return resource;
	}
};

/**
 * Used only for debugging purposes.
 * @param {Object} The *parsed* URL to log.
 * @private
 */
UrlUtil.logUri = function(url) {
	Mojo.Log.info("Protocol: '%s'", url.getProtocol());
	Mojo.Log.info("host: '%s'", url.getHost());
	Mojo.Log.info("path: '%s'", url.getPathname());
	Mojo.Log.info("qs: '%s'", url.getQuerystring());
	Mojo.Log.info("frag: '%s'", url.getFragment());
	Mojo.Log.info("uname: '%s'", url.getUsername());
	Mojo.Log.info("pwd: '%s'", url.getPassword());
};

/**
 * Test if a URL is equal to the one in this instance.
 *
 * http://www.foo.com == http://www.foo.com/
 * http://foo.com == http://www.foo.com
 *
 * @param {Poly9.URLParser} urla 
 * @param {Poly9.URLParser} urlb
 */
UrlUtil.urlParsedEquals = function(urla, urlb) {

	if (urla === urlb) {
		return true;
	}
	
	try {
		if (urla.getProtocol() !== urlb.getProtocol()) {
			return false;
		}
		if (urla.getQuerystring() !== urlb.getQuerystring()) {
			return false;
		}
		if (urla.getUsername() !== urlb.getUsername()) {
			return false;
		}
		if (urla.getPassword() !== urlb.getPassword()) {
			return false;
		}
		if (urla.getFragment() !== urlb.getFragment()) {
			return false;
		}
		if (UrlUtil.fixupHost(urla.getHost()) !== UrlUtil.fixupHost(urlb.getHost())) {
			return false;
		}
		if (UrlUtil.fixupResource(urla.getPathname()) !== UrlUtil.fixupResource(urlb.getPathname())) {
			return false;
		}

		return true;
	}
	catch (e) {
		return false;
	}
};

UrlUtil.isResource = function(url) {
	var lc = url.toLowerCase();
	return lc.startsWith("http:") || lc.startsWith("https:") || lc.startsWith("ftp:");
};

UrlUtil.isCommand = function(url) {
	return !UrlUtil.isResource(url);
};

/**
 * Test if a URL is equal to the one in this instance.
 *
 * http://www.foo.com == http://www.foo.com/
 * http://foo.com == http://www.foo.com
 * 
 * @param {String} urla
 * @param {String} urlb
 */
UrlUtil.urlEquals = function(urla, urlb) {

	if (urla === urlb) {
		return true;
	}
	try {
		return UrlUtil.urlParsedEquals(new Poly9.URLParser(urla), new Poly9.URLParser(urlb));
	}
	catch (e) {
		return false;
	}
};

/**
 * Determine if the string appears to be a URL.
 * @param {String} url
 * @return true if it is a valid url, false if not.
 */
UrlUtil.appearsToBeUrl = function(url) {
	
	if (url.include(' ')) {
		return false;
	}
	else if (url === "localhost") {
		return true;
	}
	else if (url.startsWith("about:")) {
		return true;
	}
	else {
		return url.length >= 2 && (url.match(/^w+$/i) !== null || url.include(":/") || url.include("."));
	}
};

/**
 * Determine if the string passed could possibly be a URL.
 *
 * @remarks Remember because of our auto appending, etc. even short strings that
 *          don't look like URL's can still be made into one.
 *
 * @param {String} url
 * @return true if url appears to be a URL, false if not.
 */
UrlUtil.canBeUrl = function(url) {
	return url && url.length >= 2 && !url.include(" ");
};

/**
 * Determine if the string is a well formed URL (including the scheme).
 * @param {String} url
 * @return true if it is a well formed URL url, false if not.
 */
UrlUtil.isWellFormedUrl = function(url) {

	return UrlUtil.canBeUrl(url) && url.include("://");
};

/**
 * Return the resource extension (if present). Will return null if there is no
 * extension and will throw an exception if it cannot be parsed.
 */
UrlUtil.getResourceExtension = function(url) {

	var p = new Poly9.URLParser(url);

	var matches = p.getPathname().match(/\.([^\.]*)$/i);
	if (matches) {
		return matches[1];
	}
	else {
		return null;
	}
};

UrlUtil.getUrlHost = function(url) {
	var p = new Poly9.URLParser(url);
	return p.getHost();
};

/**
 * Cleanup a URL.
 */
UrlUtil.cleanup = function(url)
{
	if (url) {
		var matches = url.match(/^https?:\/\/(www\.)?(.*)$/i);
		if (matches) {
			return matches[2];
		}
		else {
			return url;
		}
	}
	else {
		return url;
	}
};

