'using strict';

angular.module('workbench', ['ngRoute',
                             'utils',
                             'tmCloudClient',
                             'workbenchDirectives',
                             'workbenchFilters',
                             'workbenchAuth',
                             'workbenchUser',
                             'workbenchOrganization',
                             'workbenchNetwork',
                             'workbenchDevice',
                             'workbenchMessage',
                             'workbenchQuery',
                             'angularSpinner',
                             'bootstrap-tagsinput'
	], function($provide) {
		//$provide.value('endpoint', 'http://localhost:8080');
		$provide.value('endpoint', 'https://cloud.tiny-mesh.com/api');
	})
	.value('version', '0.3.1')
	.config(['$routeProvider', function($routeProvider) {
		$routeProvider
			.when('/', {
				templateUrl: 'partials/dashboard.html',
				controller: 'wbDashboardCtrl' })
			.when('/dashboard/:entity', {
				templateUrl: 'partials/dashboard.html',
				controller: 'wbDashboardCtrl' })
			.otherwise({
				templateUrl: 'partials/404.html',
				controller: 'wb404Ctrl' });
	}])
	.controller('wbDashboardCtrl', function($routeParams, $scope, tmNet, tmOrganization) {
		$scope._ = _;
		$scope.net = new tmNet({parents: []});
		$scope.entity = ($routeParams.entity || "").split(/:/);
		$scope.networks = tmNet.list();
		$scope.organizations = tmOrganization.list();
		$scope.orgnets = {};
		$scope.userloading = true;
		$scope.netloading = true;
		$scope.orgloading = true;

		$scope.net = new tmNet({parents: []});
		$scope.createNet = function(net, parents) {
			net.parents = parents;

			net.$create().then(function(newnet) {
				$scope.networks.push(newnet);
				_.each(parents, function(p) { $scope.orgnets[p].push(newnet); });
			});

			return new tmNet({});
		};

		$scope.networks.$promise.then(function(data) {
			_.each(data, function(net) {
				_.each(net.parents, function(parent) {
					if (!$scope.orgnets[parent]) {
						$scope.orgnets[parent] = [];
					}

					$scope.orgnets[parent].push(net);
				});
			});

			$scope.netloading = false;
		});

		$scope.netstats = function(net) {
			var gws, active = [], inactive = [];

			var groups = _.groupBy(net.devicemap, function(d, k) {
				var chan, ret = d.provisioned + ':' + d.type;
				if ('active:gateway' === ret) {
					chan = net.channels['app/' + net.key + '/' + k] || {};
					chan.key = chan.key || 'app/' + net.key + '/' + k;
					(chan.active ? active : inactive).push(chan);
				}

				return ret;
			});

			return {
				gateways: groups['active:gateway'],
				channels: {
					active: active,
					inactive: inactive,
				},
			};
		};

		$scope.organizations.$promise.then(function(data) {
			$scope.orgloading = false;
			_.each(data, function(v) {
				$scope.orgnets['organization/' + v.key] = $scope.orgnets[v.key] || [];
			});
		});

		$scope.ready.then(function() {
			$scope.userloading = false;
			$scope.orgnets['user/' + $scope.user.email] = [];

			if (!$scope.entity) {
				$scope.entity = angular.copy('user/' + $scope.user.email);
			}
		});

	})
	.controller('wb404Ctrl', function($scope, $window) {
		$scope.$window = $window;
	});

@@ -1287,17 +1287,44 @@
  $scope,
- 
+%0A%09%09%09loadbar, errorModal,%0A%09%09%09
 tmNet, t
@@ -1563,93 +1563,8 @@
  %7B%7D;
-%0A%09%09$scope.userloading = true;%0A%09%09$scope.netloading = true;%0A%09%09$scope.orgloading = true;
 %0A%0A%09%09
@@ -2120,42 +2120,49 @@
 %7D);%0A
-%0A%09%09%09$scope.netloading = false;%0A%09%09%7D
+%09%09%7D);%0A%0A%09%09loadbar($scope.networks.$promise
 );%0A%0A
@@ -2771,38 +2771,8 @@
 ) %7B%0A
-%09%09%09$scope.orgloading = false;%0A
 %09%09%09_
@@ -2882,24 +2882,67 @@
 %09%7D);%0A%09%09%7D);%0A%0A
+%09%09loadbar($scope.organizations.$promise);%0A%0A
 %09%09$scope.rea
@@ -2966,39 +2966,8 @@
 ) %7B%0A
-%09%09%09$scope.userloading = false;%0A
 %09%09%09$
@@ -3037,16 +3037,19 @@
 e.entity
+%5B0%5D
 ) %7B%0A%09%09%09%09
@@ -3077,25 +3077,24 @@
 ar.copy(
+%5B
 'user
-/' +
+',
  $scope.
@@ -3103,16 +3103,17 @@
 er.email
+%5D
 );%0A%09%09%09%7D%0A

Make dashboard use loadbar and fix settng initial user entity