/// <reference path="../../includes.ts"/>
module Developer{
    var log = Logger.get('developer-navigation');
    export function createCurrentSubNavBar($scope, $location, $routeParams){    	
		return activateCurrent([
			{
			 href: UrlHelpers.join(context,"Overview","hot/data-type/all"),
			 label: "热区数据管理",
			 title: "查看所有数据",
			 items: [{
			 	href: UrlHelpers.join(context,"Overview","hot/data-type/all"),
			 	label: "全部",
			 	title: "全部数据"
			 },
			 {
				href: UrlHelpers.join(context,"Overview","hot/data-type/financial"),
			 	label: "财政",
			 	title: "财政数据"
			 },
			 {
			 	href: UrlHelpers.join(context,"Overview/","hot/data-type/social-security"),
			 	label: "社保",
			 	title: "社保数据"
			 }]
			},
			{
			 href: UrlHelpers.join(context,"Overview","cold/data-type/all"),
			 label: "冷区数据管理",
			 title: "数据汇总任务",
			 items: [{
			 	href: UrlHelpers.join(context,"Overview","cold/data-type/all"),
			 	label: "全部",
			 	title: "全部数据"
			 },
			 {
				href: UrlHelpers.join(context,"Overview","cold/data-type/financial"),
			 	label: "财政",
			 	title: "财政数据"
			 },
			 {
			 	href: UrlHelpers.join(context,"Overview/","cold/data-type/social-security"),
			 	label: "社保",
			 	title: "社保数据"
			 }]
			}
		]);
	}

	function activateCurrent(navBarItems) {
		navBarItems = _.compact(navBarItems);
		var injector = HawtioCore.injector;
		var $location = injector ? injector.get<ng.ILocationService>("$location") : null;
		if ($location) {
			var path = normalizeHref(trimQuery($location.path()));
			var found = false;
			function makeActive(item) {
			    item.active = true;
			    found = true;
			}
			function getHref(item) {
			    var href = item.href;
			    var trimHref = trimQuery(href);
			    return normalizeHref(trimHref);
			}
			angular.forEach(navBarItems, (item) => {
			    if (!found && item) {
				    if (angular.isFunction(item.isActive)) {
				        if (item.isActive(item, path)) {
				         	makeActive(item);
				        }
				    } else {
				      	var trimHref = getHref(item);
			        	if (!trimHref) {
				          	return;
				        }
				        if (trimHref === path) {
				         	makeActive(item);
				        }
				    }
			    }
	  		});
      // Maybe it's a sub-item of a tab, let's fall back to that maybe
		    if (!found) {
		        angular.forEach(navBarItems, (item) => {
		          	if (!found) {
		            	if (!angular.isFunction(item.isActive)) {
			              	var trimHref = getHref(item);
			              	if (!trimHref) {
			                	return;
			              	}
			              	if (_.startsWith(path, trimHref)) {
			               		makeActive(item);
			              	}
		            	}
		          	}
		        });
		    }
      // still not found, let's log it
		    if (!found) {
		        log.debug("No navigation tab found for path:", path);
		    }
    	}
    	return navBarItems;
  	}

  	function trimQuery(text) {
   		if (text) {
	      	var idx = text.indexOf("?");
	      	if (idx >= 0) {
	        	return text.substring(0, idx);
	      	}
   		}
    	return text;
 	}
 
}