app.controller('vocListController', function($scope, $http) {			
	$scope.vocabularies = [];
	$scope.tmpVoc = {};
	$scope.mode = '';		
		
	call_http_get($http, './ajax/vocs/?' + $.now(), function(res) {			
		$scope.vocabularies = res.data;			
	});			
	
	$scope.prepareNewVoc = function() {
		$scope.mode = 'new';
		$scope.tmpVoc = {
			'id'          : '',
			'name'        : '',
			'description' : ''
		};
	}

	$scope.prepareEditVoc = function(voc) {
		$scope.mode = 'edit';
		$scope.tmpVoc = angular.copy(voc);
	}
					
	$scope.saveVocabulary = function(voc) {
		if ($scope.mode == 'new') {
			var found = false;
			
			angular.forEach($scope.vocabularies, function(v) {
				if (voc.id == v.id) { found = true; };
			});
			
			if (found) {
				alert("Insertion failed: vocabulary already exists !");
				return;
			}
		}
									
		json_http_post($http, './ajax/vocs/?' + $.now(), voc, function(res) {			
			$scope.vocabularies = res.data;
			alert("Vocabulary saved");
		});			
	};			
	 					
	$scope.deleteVocabulary = function(id) {			
		if (confirm("Are you sure ?")) {			
			call_http_delete($http, './ajax/vocs/' + encodeURIComponent(id) + '?' + $.now(), function(res) {			
				$scope.vocabularies = res.data;
				alert("Vocabulary deleted");
			});			
		}			
	};			
});

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

app.controller('vocEditorController', function($scope, $http, $routeParams) {			
	$scope.terms = [];				
	$scope.vocId = $routeParams.id;
	$scope.vocInfo = {};
	$scope.editTermCode = '';
	$scope.tmpTerm = {};
	$scope.mode = '';
	$scope.currTerm = [];
	
	$scope.baseUrl = './ajax/vocs/' + encodeURIComponent($scope.vocId);
	
	call_http_get($http, $scope.baseUrl + '?' + $.now(), function(res) {			
		$scope.vocInfo = res.data;			

		call_http_get($http, $scope.baseUrl + '/terms?' + $.now(), function(res) {			
			$scope.terms = res.data;			
		});			
	});			
	
	$scope.setCurrTerm = function(term) {
		$scope.currTerm = angular.copy(term);
	}
	
	$scope.prepareNewTerm = function() {
		$scope.mode = 'new';
		$scope.editTermCode = '';
		$scope.tmpTerm = {
			'code'     : '',
			'name'     : '',
			'encoding' : 'OPENAIRE',
			'synonyms' : []
		};
	}

	$scope.prepareEditTerm = function(term) {
		$scope.mode = 'edit';
		$scope.editTermCode = term.code;
		$scope.tmpTerm = angular.copy(term);
	}
	
					
	$scope.saveTerm = function(term) {					
		var url = $scope.baseUrl + '/terms?' + $.now();

		json_http_post($http, url, term, function(res) {			
			if ($scope.editTermCode != '' && $scope.editTermCode != $scope.tmpTerm.code) {
				var deleteUrl = $scope.baseUrl + '/terms/' + encodeURIComponent($scope.editTermCode) + '?' + $.now();
				call_http_delete($http, deleteUrl, function(res) {			
					$scope.terms = res.data;
					alert("Term replaced");
				});
			} else {
				$scope.terms = res.data;
				alert("Term saved");
			}
		});					
	};			

	$scope.deleteTerm = function(code) {			
		if (confirm("Are you sure ?")) {			
			var url = $scope.baseUrl + '/terms/' + encodeURIComponent(code) + '?' + $.now();

			call_http_delete($http, url, function(res) {			
				$scope.terms = res.data;
				alert("Term deleted");
			});
		}			
	};			

});
