export default class LinkedController {



    constructor($scope, $log, $location, $stateParams, $q, $filter, $window, $rootScope, RealtimeProductResource, TextReview) {
        'ngInject';
        this.$scope = $scope;
        this.$log = $log;
        this.$q = $q;
		this.$filter = $filter;
        this.$rootScope = $rootScope;
        this.RealtimeProductResource = RealtimeProductResource;
        this.TextReview = TextReview;
		this.$location = $location;
		this.$window = $window;
		this.$stateParams = $stateParams;
    }

    $onInit() {
		this.product = null;
		this.productId = this.$stateParams.productId;
		this.createdAt = this.$stateParams.createdAt;
		this.fieldName = this.$stateParams.fieldName;
		this.page = 0;
		this.linkedReviews = [];
		this.insight = null;
		this.areReviewsFinished = false;
		this.activeOps = 0;
		if (!this.productId || !this.createdAt) {
			return alert('Please provide better URL!');
		}
		this.subscribeToRealtimeDataEvent();
		this.loadData();

    }

	subscribeToRealtimeDataEvent() {
		var ProductEventNamesByHttpMethods = this.RealtimeProductResource.getRestEventNames();
		this.$rootScope.$on(ProductEventNamesByHttpMethods.PUT, (e, product) => {
			this.$log.info('Some collaborative user updated a product', product);
			this.product = product;
			this.$scope.$apply();
			this._retrieveLinkedReviewsByProduct(product)
				.then((linkedReviews) => {
					this._boundLinkedReviewsToScope(linkedReviews);
				});
		});
	};

	loadData() {
		this.RealtimeProductResource.getOneById(this.productId)
			.then((product) => {
				this.product = product;
				this.$log.info('>>> Product is', product);
				return product;
			})
			.then((product) => {
				return this._retrieveLinkedReviewsByProduct(product);
			})
			.then((linkedReviews) => {
				// console.log(linkedReviews)
				this._boundLinkedReviewsToScope(linkedReviews);
			})
			.catch( (msg) => {
				alert('Problem:' + msg);
			});
	};

	nextPage() {
		this.page = this.page + 1;
		this.activeOps = this.activeOps + 1;
		this._retrieveLinkedReviewsByProduct(this.product)
		.then( (linkedReviews) => {
			this._boundLinkedReviewsToScope(linkedReviews);
		})
		.catch( (msg) => {
			alert('Problem:' + msg);
		})
		.finally( () => {
			this.activeOps = this.activeOps - 1;
		});
	};


	_boundLinkedReviewsToScope(linkedReviews) {
		this.$log.info('>>> LinkedReviews:', linkedReviews.length);
		if (this.page === 0) {
			this.linkedReviews = [];
		}
		if (linkedReviews.length < 10) {
			this.areReviewsFinished = true;
		}
		this.linkedReviews = this.linkedReviews.concat(linkedReviews);
	};

	_retrieveLinkedReviewsByProduct(product){
		var insights, insight, reviewIds;

		insights = product.insightsDraft;
		insight = this.$filter('filter')(insights, {
			createdAt: this.createdAt
		});
		if (!insight || !insight.length) {
			return alert('Problem finding the insight');
		}
		this.insight = insight = insight[0];
		reviewIds = insight[this.fieldName];
		if (!reviewIds) {
			return $q.reject('This insight has no linked reviews!');
		}
		return this.TextReview.getByReviewTitlesAndReviewIds(product.reviewTitles, reviewIds, this.page);
	};

	unlinkReviewFromInsight(review, insight) {
		var isConfirmed;
		var isCallCenterData;

		isConfirmed = confirm('Are you sure to unlink this review from this insight?  \n\nOnce this review will be unlinked, there will be no way to recover it in case the unlinking was wrong.');
		if (!isConfirmed) {
			return;
		}
		this.activeOps = this.activeOps + 1;

		var reviewId, product;

		reviewId = review._id;
		isCallCenterData = review.review.channel && review.review.channel.match(/call\-center$/gi);
		product = this.product;

		this.RealtimeProductResource
			.unlinkInsightByProductAndInsightAndReviewIdAndIsCallCenterData(product, insight, reviewId, isCallCenterData)
			.then((product) => {
				this.product = product;
				this.page = 0;
				return this._retrieveLinkedReviewsByProduct(this.product);
			})
			.then( (linkedReviews) => {
				return this._boundLinkedReviewsToScope(linkedReviews);
			})
			.catch( (msg) => {
				return alert('Ooooops, problem: ' + msg);
			})
			.finally( () => {
				this.activeOps = this.activeOps - 1;
			});
	};

	back() {
		this.$window.history.back();
	}

}


