/**
 * AttachmentsCtrl Controller
 * Extracted from bookings module
 */

Bookings.controller('AttachmentsCtrl', ['$scope', 'Files', 'ErrorsHandler', function ($scope, Files, ErrorsHandler) {

	$scope.files = [];

	$scope.$watch('task.id', function (value) {
		if (value) $scope.loadFiles();
	});

	$scope.loadFiles = function () {
		Files.get({ objectType: $scope.type, objectId: $scope.task.id, limit: 100 }, function (response) {
			$scope.files = response.data;
		}, ErrorsHandler);
	};

	/**
	 * Removes a file from the files list.
	 *
	 * @param  object  the file to be removed
	 * @return void
	 */
	$scope.removeFile = function (file) {
		Files.remove({ fileId: file.id }, function () {
			$scope.files = _.reject($scope.files, function (el) { return el.id == file.id; });
		}, ErrorsHandler);
	};

}])
