'use strict';

/**
 * @ngdoc function
 * @name annotationAppApp.controller:AssignReviewsToAnalystCtrl
 * @description
 * # AssignReviewsToAnalystCtrl
 * Controller of the annotationAppApp
 */
angular.module('app')
	.filter('filterByAssignedProduct', function() {
		return function(analysts, product) {

			analysts = analysts || [];


			return analysts.filter(function(analyst) {
				return analyst.assignedProducts && Object.keys(analyst.assignedProducts).indexOf(product._id) >= 0 && analyst.assignedProducts[product._id].length > 0;
			});

		};
	})
	.controller('AssignReviewsToAnalystCtrl', function($scope, $log, $q, $rootScope, notify, RealtimeUserForAdministrator, AssignReviewstoAnalystUtils, TextReview) {

		var UserForAdministrator = RealtimeUserForAdministrator;
		var UserForAdministratorEventNamesByHttpMethods = RealtimeUserForAdministrator.getRestEventNames();

		$rootScope.$on(UserForAdministratorEventNamesByHttpMethods.POST, (e, analyst) => {
			$log.info('Some collaborative user created an analyst', analyst);
			$scope.analysts.push(analyst);
		});

		$rootScope.$on(UserForAdministratorEventNamesByHttpMethods.PUT, (e, analyst) => {
			$log.info('Some collaborative user updated an analyst', analyst);
			$scope.analysts.every((el, index) => {
				if (el._id === analyst._id) {
					$scope.analysts[index] = analyst;
					$scope.$apply();
					return false;
				}
				return true;
			});
		});

		$rootScope.$on(UserForAdministratorEventNamesByHttpMethods.DELETE, (e, analyst) => {
			this.$log.info('Some collaborative user deleted an analyst', analyst);
			this.analysts.every((el, index) => {
				if (el._id === analyst._id) {
					$scope.analysts.splice(index, 1);
					$scope.analysts[index] = analyst;
					$scope.$scope.$apply();
					return false;
				}
				return true;
			});
		});

		var checkForErrors = function(chosenLanguage, chosenAnalyst, chosenDataSource) {

			if (!chosenDataSource) {
				notify.show('Please select a data source before assigning reviews');
				return true;
			} else if (!chosenLanguage) {
				notify.show('Please select a language before assigning reviews');
				return true;
			} else if (!chosenAnalyst) {
				notify.show('Please select an analyst before assigning reviews');
				return true;
			}

			return false;
		};

		// Analyst model...
		$scope.assignAnalystToProductAndLanguage = function(product, chosenLanguage, chosenAnalyst, chosenDataSource) {
			var assignedProducts;

			if (checkForErrors(chosenLanguage, chosenAnalyst, chosenDataSource)) {
				return;
			}

			assignedProducts = AssignReviewstoAnalystUtils.encodeAssignedProducts(product, chosenLanguage, chosenAnalyst, chosenDataSource);

			UserForAdministrator.setAssignedProducts(chosenAnalyst, assignedProducts)
				.then(function(updatedAnalyst) {
					notify.show('User updated');
					$log.info('User updated', updatedAnalyst);
					$scope.analysts.every(function(el) {
						if (el._id === updatedAnalyst._id) {
							el.assignedProducts = updatedAnalyst.assignedProducts;
							return false;
						}
						return true;
					});
				}, function(err) {
					notify.show('Error', err);
					$log.info('Error', err);
				});
		};

		$scope.removeAssignedProduct = function(analyst, product, language) {
			var assignedProducts;

			assignedProducts = JSON.parse(JSON.stringify(analyst.assignedProducts));

			assignedProducts[product._id] = assignedProducts[product._id].filter(function(obj) {
				return obj.language !== language;
			});

			assignedProducts = angular.toJson(assignedProducts);
			assignedProducts = angular.fromJson(assignedProducts);


			UserForAdministrator.setAssignedProducts(analyst, assignedProducts)

			.then(function(updatedAnalyst) {
				notify.show('User updated');
				$log.info('User updated', updatedAnalyst);
				$scope.analysts.every(function(el) {
					if (el._id === updatedAnalyst._id) {
						el.assignedProducts = updatedAnalyst.assignedProducts;
						return false;
					}
					return true;
				});
			}, function(err) {
				notify.show('Error', err);
				$log.info('Error', err);
			});
		};

		$scope.onDataSourceChange = function() {

			var promises = [];

			$scope.product.languages.forEach(function(language) {
				var p = TextReview.countReviewsToManuallyAnnotateByReviewTitlesAndLanguagesAndDataSource($scope.product.reviewTitles, [language], $scope.chosenDataSource);
				promises.push(p);
			});

			return $q.all(promises)

			.then(function(resp) {
				$scope.reviewsCountByLanguageIndex = resp;
			})

			.catch(function(err) {
				console.log(err);
			});

		};

		function getAllAnalystsAndAdministratorsFromUserForAdministrator() {
			RealtimeUserForAdministrator.getAllAnalystsAndAdministrators()
				.then((data) => {
					$log.info('analysts', data);
					$scope.analysts = data;
				}, (err) => {
					notify.show('Error');
				});
		}
		getAllAnalystsAndAdministratorsFromUserForAdministrator();

	});