'use strict';

/**
 * @ngdoc service
 * @name annotationAppApp.insightMergeOperation
 * @description
 * # insightMergeOperation
 * Service in the annotationAppApp.
 */
angular.module('app')
	.service('InsightMergeOperation', function($q, $log, HostFactory, Model, RealtimeProductResource, User) {
		function InsightMergeOperation(opts) {
			// initialize
			opts = opts || {};

			opts.scheme = HostFactory.getScheme();
			opts.host = HostFactory.getHost();
			opts.dbName = HostFactory.getDbUrlComponent();
			opts.collectionName = 'insightsMergeOperations';

			// build
			Model.call(this, opts);
			this.insightsToMerge = opts.insightsToMerge || [];
			this.newTextOfMergedInsight = opts.newTextOfMergedInsight || '';
			this.product = opts.product || null;
		}
		InsightMergeOperation.prototype = new Model();
		InsightMergeOperation.prototype.getNewNumberOfReviewIds=function(){
			var newReviewIds;
			newReviewIds = this.insightsToMerge.reduce(function(acc, el) {
				if (el.reviewIds && el.reviewIds.length > 0){
					el.reviewIds.forEach(function(reviewId){
						if (acc.indexOf(reviewId)<0){
							acc.push(reviewId);
						}
					});
				}
				return acc;
			}, []);
			return newReviewIds.length;
		};
		InsightMergeOperation.prototype.getNewNumberOfCallCenterIds=function(){
			var newCallCenterDataIds;
			newCallCenterDataIds = this.insightsToMerge.reduce(function(acc, el) {
				if (el.callCenterDataIds && el.callCenterDataIds.length > 0){
					el.callCenterDataIds.forEach(function(callCenterDataId){
						if (acc.indexOf(callCenterDataId)<0){
							acc.push(callCenterDataId);
						}
					});
				}
				return acc;
			}, []);
			return newCallCenterDataIds.length;
		};
		InsightMergeOperation.prototype.execute = function() {
			var def = $q.defer();
			var isMergePossibleToExecute;
			var insight, newReviewIds, newCallCenterDataIds;
			var p1;
			var author, currentUser;


			isMergePossibleToExecute = this.insightsToMerge && this.insightsToMerge.length && this.insightsToMerge.length > 0 && this.newTextOfMergedInsight && this.newTextOfMergedInsight !== '' && this.product;
			if (!isMergePossibleToExecute) {
				def.reject(new Error('Oooops: 0 insights to merge or no text specified for new insight'));
				return def.promise;
			}
			// create new insight
			newReviewIds = this.insightsToMerge.reduce(function(acc, el) {
				if (el.reviewIds && el.reviewIds.length > 0){
					el.reviewIds.forEach(function(reviewId){
						if (acc.indexOf(reviewId)<0){
							acc.push(reviewId);
						}
					});
				}
				return acc;
			}, []);
			newCallCenterDataIds = this.insightsToMerge.reduce(function(acc, el) {
				if (el.callCenterDataIds && el.callCenterDataIds.length > 0){
					el.callCenterDataIds.forEach(function(callCenterDataId){
						if (acc.indexOf(callCenterDataId)<0){
							acc.push(callCenterDataId);
						}
					});
				}
				return acc;
			}, []);

			currentUser = User.get();
			author = (currentUser && currentUser.username) ? currentUser.username : '';


			insight = {
				text: this.newTextOfMergedInsight,
				createdAt: new Date(),
				reviewIds: newReviewIds,
				callCenterDataIds: newCallCenterDataIds,
				author: author
			};
			// save
			p1 = RealtimeProductResource.updateInsightsByProductAndInsightsToRemoveAndInsightToAdd(this.product, this.insightsToMerge, insight);

			$log.info('Waiting for update...');
			return $q.all([p1]);
		};
		return InsightMergeOperation;
	});