'use strict';

streamaApp.directive('adminEpisode', [
	'uploadService', 'modalService', 'apiService', function (uploadService, modalService, apiService) {
	return {
		restrict: 'AE',
		templateUrl: 'directive--admin-episode.htm',
		scope: {
			episode: '='
		},
		link: function ($scope, $elem, $attrs) {
			$scope.uploadStatus = {};



			$scope.editEpisode = function(episode){
				modalService.videoModal(episode, null, null, function (data) {
					if(data.deleted){
						episode.deleted = true;
					}
				});
			};


			$scope.openFileBrowser = function(){
				modalService.openFileBrowser(function (file) {
					apiService.video.addFile($scope.episode.id, file.id).success(function () {
						$scope.episode.files = $scope.episode.files || [];
						$scope.episode.files.push(file);
					});
				});
			};



			$scope.addFile = function(episode){
				modalService.fileModal(episode);
			};


			$scope.removeFile = function(file){
				alertify.confirm('Are you sure you want to remove the file "'+file.originalFilename+'"?', function (confirmed) {
					if(confirmed){
						apiService.video.removeFile($scope.episode.id, file.id).success(function () {
							file.deleted = true;
						});
					}
				});
			};
			
			
			
			$scope.upload = uploadService.doUpload.bind(uploadService, $scope.uploadStatus, 'video/uploadFile.json?id=' + $scope.episode.id, function (data) {
				$scope.uploadStatus.percentage = null;
				$scope.episode.files = $scope.episode.files || [];
				$scope.episode.files.push(data);
			});
		}
	}
}]);
@@ -1157,34 +1157,67 @@
 ) %7B%0A
-%09%09%09%09%09%09%09file.deleted = true
+              _.remove($scope.episode.files, %7Bid: file.id%7D)
 ;%0A%09%09
@@ -1245,27 +1245,18 @@
 ;%0A%09%09%09%7D;%0A
-%09%09%09%0A%09%09%09%0A%09%09%09
+%0A%0A
 %0A%09%09%09$sco
@@ -1551,8 +1551,9 @@
 %0A%09%7D%0A%7D%5D);
+%0A

remove file from $scope array when removing