app.controller('dsmSearchController', function($scope, $http, $location, $timeout) {
	$scope.browseFieldId = "";
	$scope.browseFieldName = "";
	$scope.browseData = [];
		
	$scope.browsableFields = [];
		
	call_http_get($http, './ajax/dsm/browsableFields' , function(res) {
		$scope.browsableFields = res.data;				
	});
		
	$scope.browseField = function(id, label) {
		$scope.browseFieldId = id;
		$scope.browseFieldName = name;
		$scope.browseData = [];
		
		call_http_get($http, './ajax/dsm/browse/' + encodeURIComponent(id) + '?' + $.now(), function(res) {
			$scope.browseData = res.data;			
		});
	}
		
	$scope.search = function(field, value) {
		var path = "/dsm/results";
		if (field) { path += '/' + encodeURIComponent(field); }
		path += '/0/100';
		
		$timeout(function() {
			$location.path(path).search('value', value);
		}, 1000);
	}
});

// ----------------------------------------------------
	
app.controller('dsmResultsController', function($scope, $http, $location, $routeParams) {			
	$scope.field = $routeParams.field;
	$scope.value = $routeParams.value;
	$scope.pageSize = $routeParams.size;
	$scope.currPage = $routeParams.page;
		
	$scope.nResults = 0;
	$scope.results = [];
	$scope.nPages = 0;
			
	var url = './ajax/dsm/';
	if ($scope.field) { url += 'searchByField/' + encodeURIComponent($scope.field); }
	else              { url += 'search' }
	url += '/' + $scope.currPage + '/' + $scope.pageSize; 
	url += '?value=' + encodeURIComponent($scope.value) + '&' + $.now();
			
	call_http_get($http, url, function(res) {
		$scope.results = res.data.content;			
		$scope.nResults = res.data.totalElements;
		$scope.currPage = res.data.number;
		$scope.nPages = res.data.totalPages;				
	});
		
	$scope.gotoPage = function(page) {
		$scope.results = [];
		var path = "/dsm/results";
		if ($scope.field) { path += '/' + encodeURIComponent($scope.field); }
		path += '/' + page + '/' + $scope.pageSize;
		$location.path(path).search('value', $scope.value);
	}
		
});

// ----------------------------------------------------

app.controller('dsmApiController', function($scope, $http, $routeParams) {
	$scope.apiId = $routeParams.id;
	$scope.api = {};
	
	call_http_get($http, './ajax/dsm/api?id='  + encodeURIComponent($scope.apiId) , function(res) {
		$scope.api = res.data;				
	});
		
});
	
// ----------------------------------------------------
	
app.controller('dsmAddApiController', function($scope, $http, $routeParams) {
	$scope.dsName = $routeParams.dsName;
	$scope.prefix = 'api_________::' + $routeParams.dsId + '::';
	$scope.api = {}

	$scope.protocols = [];
	$scope.protocolsMap = {};
	$scope.compatibilityLevels = [];
	$scope.contentDescTypes = [];
	
	$scope.selProtParams = [];
	
	$scope.$watch('api.protocol', function() {
		if ($scope.api.protocol) {
			$scope.selProtParams = $scope.protocolsMap[$scope.api.protocol];
 		}
 	});
	
	
	call_http_get($http, './ajax/dsm/conf' , function(res) {
		$scope.protocols = [];
		$scope.protocolsMap = {};
		angular.forEach(res.data.protocols, function(prot) {
			$scope.protocols.push(prot.id);
			$scope.protocolsMap[prot.id] = prot.params;
		});
		
		$scope.compatibilityLevels = res.data.compatibilityLevels;
		$scope.contentDescTypes = res.data.contentDescTypes;
	});
		
	$scope.save = function() {
		var record = angular.copy($scope.api);
		record.id = $scope.prefix + record.id;
		record.datasource = $routeParams.dsId;
			
		json_http_post($http, './ajax/dsm/api', record, function(res) {
			$scope.api = res.data;				
		});
	}

});
