/* globals $ */

'use strict';

function Positioner(opts) {
	opts = opts || {};
	this.currentAnnotation = null;
	this.annotations = opts.annotations || [];
	this.Annotations = opts.Annotations || null;
	this.idOfCurrentAnnotation = null;
}

Positioner.prototype.setIdOfCurrentAnnotation = function(id) {
	this.idOfCurrentAnnotation = id;
	this.render();
};
Positioner.prototype.getIdOfCurrentAnnotation = function() {
	return this.idOfCurrentAnnotation;
};
Positioner.prototype.getCurrentAnnotation = function() {
	var currentAnnotation;
	var self = this;
	currentAnnotation = this.annotations.filter(function(ann) {
		return self.Annotations.getIdByAnnotation(ann) === self.idOfCurrentAnnotation;
	});
	if (!currentAnnotation || !currentAnnotation.length) {
		return null;
	}
	currentAnnotation = currentAnnotation[0];
	return currentAnnotation;
};

Positioner.prototype.setAnnotations = function(annotations) {
	this.annotations = annotations;
	var self = this;
	setTimeout(function() {
		self.render();
	}, 100);
};

Positioner.prototype.renderWithoutCurrentNote = function() {
	var annotationInsideTextSelector = null;
	var topOfAnnotationInsideText = null;
	var annotationId = null;
	var currentAnnotation = null;

	if (!this.annotations || !this.annotations.length) {
		return;
	}

	currentAnnotation = this.annotations[0];
	annotationId = this.Annotations.getIdByAnnotation(currentAnnotation);

	annotationInsideTextSelector = '.annotation-highlight#' + annotationId;
	topOfAnnotationInsideText = $(annotationInsideTextSelector).offset();
	if (!topOfAnnotationInsideText) {
		return;
	}
	topOfAnnotationInsideText = topOfAnnotationInsideText.top;

	var acc = 0;
	for (var i = 0; i < this.annotations.length; i++) {
		this.annotations[i].top = topOfAnnotationInsideText + acc;
		acc += 75;
	}

	console.log(this.annotations);
};
Positioner.prototype.render = function() {

	var annotationInsideTextSelector, annotationInSidebarSelector;
	var topOfAnnotationInsideText;
	var currentAnnotation, currentAnnotationId;

	

	if (!this.idOfCurrentAnnotation && this.idOfCurrentAnnotation !== 0) {
		return this.renderWithoutCurrentNote();
	}
	currentAnnotation = this.getCurrentAnnotation();
	if (!currentAnnotation) {
		return this.renderWithoutCurrentNote();
	}
	currentAnnotationId = this.Annotations.getIdByAnnotation(currentAnnotation);
	annotationInsideTextSelector = '.annotation-highlight#' + currentAnnotationId;
	annotationInSidebarSelector = '.annotation-list-item#' + currentAnnotationId

	topOfAnnotationInsideText = $(annotationInsideTextSelector).offset();

	if (!topOfAnnotationInsideText) {
		return;
	}
	
	topOfAnnotationInsideText = topOfAnnotationInsideText.top;
	topOfAnnotationInsideText = topOfAnnotationInsideText-50; // keep navbar into consideration
	currentAnnotation.top = topOfAnnotationInsideText;
	var element_ = $(annotationInSidebarSelector);
	element_.offset({
		top: top + 'px'
	});
};

function AnnotationListCtrl($scope, $rootScope, HtmlKeywordsUnderliner, TextReview, notify, Annotations, ReviewsUtils) {
	this.htmlKeywordsUnderliner = HtmlKeywordsUnderliner;
	this.textReview = TextReview;
	this.Annotations = Annotations;
	this.notify = notify;
	this.Positioner = new Positioner({
		Annotations: Annotations
	});
	this.ReviewsUtils = ReviewsUtils;

	var self = this;
	self.scope = $scope;
	self.$rootScope = $rootScope;
	$scope.Annotations = Annotations;
	$scope.$watch(function(){return self.annotations}, function(newAnnotations) {

		
		
		if (newAnnotations) {
			newAnnotations = newAnnotations.filter(function(a) {
				return a.version === 'v2';
			});
			if (newAnnotations.length) {
				newAnnotations.sort(Annotations.getSortCb());
				self.Positioner.setAnnotations(newAnnotations);
			} else {
				alert('This review was analyzed with the previous system');
				return;
			}
		}

	});
	$rootScope.$on('aspectAnnotationWasClicked', function(ev, annotationId) {
		self.setIdOfCurrentAnnotation(annotationId);
	});
	$rootScope.$on('annotationEditorIsRendered', function(ev, annotationId) {
		self.setIdOfCurrentAnnotation(annotationId);
	});

	

	$rootScope.$on('ANNOTATION_SAVED', function() {

			


		// if ($scope.annotations && $scope.annotations.length) {
		if (self.annotations && self.annotations.length) {

			self.annotations.sort(Annotations.getSortCb());


			self.Positioner.setAnnotations(self.annotations);
		}

	});
}


AnnotationListCtrl.prototype.changeAnnotationSentiment = function(review, annotation, sentiment) {

	var self = this;

	this.textReview.changeAnnotationSentimentByReviewHashAndAnnotation(review.review.hash, annotation, sentiment)
		.then(function() {
			self.notify.show('Annotation changed','/img/notify.png');
			annotation.sentiment = sentiment;
			self.$rootScope.$emit('ANNOTATION_SAVED');
		})
		.catch(function() {
			self.notify.show("Error while editing annotation",'/img/notify.png');
		});

};
AnnotationListCtrl.prototype.isAnnotationOpenByAnnotationId = function(id) {
	return this.getIdOfCurrentAnnotation() === id;
};
AnnotationListCtrl.prototype.getZIndexByAnnotationId = function(index) {
	return (this.Positioner.getIdOfCurrentAnnotation() === index) ? 2000 : 1000;
};
AnnotationListCtrl.prototype.getLeftByAnnotationId = function(id) {
	return (this.Positioner.getIdOfCurrentAnnotation() === id) ? '-30px' : '0px';
};


AnnotationListCtrl.prototype.setIdOfCurrentAnnotation = function(id) {
	this.Positioner.setIdOfCurrentAnnotation(id);
};
AnnotationListCtrl.prototype.getIdOfCurrentAnnotation = function() {
	return this.Positioner.getIdOfCurrentAnnotation();
};

AnnotationListCtrl.prototype.removeAnnotation = function(review, annotation) {

	var self = this;

	this.textReview.removeAnnotationByReviewHashAndAnnotation(review.review.hash, annotation)

	.then(function() {
			self.notify.show('Annotation removed','/img/notify.png');
			self.$rootScope.$emit('ANNOTATION_SAVED');
		})
		.catch(function() {
			self.notify.show('Error while removing annotation','/img/notify.png');
		});

	var index = -1;
	for (var i = 0; i < review.feature2sentiment.length && index < 0; i++) {
		var currentItem = review.feature2sentiment[i];
		if (currentItem.name === annotation.name && currentItem.start === annotation.start && currentItem.offset === annotation.offset) {
			index = i;
		}
	}

	if (index >= 0) {
		review.feature2sentiment.splice(index, 1);
	}
};

AnnotationListCtrl.prototype.isCreatedByHuman = function(annotation) {
	return this.ReviewsUtils.isAnnotationCreatedByHuman(annotation);
};

AnnotationListCtrl.prototype.isCreatedByMachine = function(annotation) {
	return this.ReviewsUtils.isAnnotationCreatedByMachine(annotation);
};

AnnotationListCtrl.prototype.hasAnnotationBeenReviewed = function(annotation) {
	return this.ReviewsUtils.hasAnnotationBeenReviewed(annotation);
};

AnnotationListCtrl.prototype.hasAnnotationIssues = function(annotation) {
	return this.ReviewsUtils.hasAnnotationIssues(annotation);
};


angular.module('app')
	.directive('annotationList', function() {
		return {
	        template: require('./annotation-list.html'),
			restrict: 'E',
			scope: {
				review: '=',
				annotations: '='
			},
			controller: ['$scope', '$rootScope', 'HtmlKeywordsUnderliner', 'TextReview', 'notify', 'Annotations', 'ReviewsUtils', AnnotationListCtrl],
			controllerAs: 'annotationListCtrl',
			bindToController: true

		};
	});