import { URL_PREFIX, buildUrl,  buildCountUrl, buildDistinctUrl, User } from  './../../../before-each';
/* global buildUrl, buildCountUrl, buildDistinctUrl, sinon, URL_PREFIX */
'use strict';

describe('Controller - ReviewsController', function() {

	var ReviewsController, TextReview, scope, products, reviews, CurrentSession;
	var $httpBackend, $controller;
	beforeEach(inject(function(
		_$httpBackend_, 
		_$controller_, 
		$rootScope, 
		_Model_, 
		_Products_, 
		_CurrentSession_,
		_Reviews_, 
		_TextReview_,
		_HostFactory_) {

		$httpBackend = _$httpBackend_;
		$controller = _$controller_;
		products = _Products_;
		TextReview =  _TextReview_;
		reviews =  _Reviews_;
		CurrentSession = _CurrentSession_;
		var Model = _Model_;
		var HostFactory = _HostFactory_;
		scope = $rootScope.$new();


		var product = {
            _id: 'aabbcc',
            insightsDraft: [{
                'createdAt': '2015-03-16T11:03:25.901Z',
                'text': 'Insight 1',
                'reviewIds': [
                    '54f8d13ee365e512625ed487',
                    '54f8d13ee365e512625ed491',
                ],
                'isHidden': false
            }, {
                'createdAt': '2015-03-16T11:03:25.901Z',
                'text': 'Insight 2',
                'reviewIds': [
                    '54f8d13ee365e512625ed4b2',
                    '54f8d13ee365e512625ed4b8',
                    '54f8d13ee365e512625ed4c4',
                    '54f8d13ee365e512625ed4d0',
                    '54fb0534034c18ac0bef3db3'
                ],
                'isHidden': false
            }, {
                'createdAt': '2015-03-16T11:03:25.901Z',
                'text': 'Insight 3',
                'reviewIds': [
                    '54fb0535034c18ac0bef3dd4',
                    '54fb0535034c18ac0bef3dde',
                    '54fb0535034c18ac0bef3de9',
                    '54fb0535034c18ac0bef3dff',
                    '54fb0535034c18ac0bef3e00',
                    '54fb0535034c18ac0bef3e08'
                ],
                'isHidden': true
            }]
        };
        CurrentSession.setCurrentProduct(products[0]);

		$httpBackend.whenGET(URL_PREFIX + '/products?rawQuery=%7B%7D').respond(200, products);

		ReviewsController = $controller('ReviewsController', {
			$scope: scope
		});
		ReviewsController.$onInit();
		

		scope.$digest();

		$httpBackend.flush();

	}));

	describe('$scope.nextReviewToAnnotateByTitlesAndDataSource', function() {

		beforeEach(function() {

			var query, options, fields;

			// fixed with review.language after review.appearedOn
			query = {
			    '$or': [
			        {
			            'analysis.hasBeenManuallyAnnotated': false
			        },
			        {
			            'analysis.hasBeenManuallyAnnotated': null
			        }
			    ],
			    'review.appearedOn': {
			        '$elemMatch': {
			            'title': {
			                '$in': ['t1', 't2']
			            },
			            'channel': {
			                '$regex': '/^((?!call.center).)*$/gi'
			            }
			        }
			    },
			    'review.language': {
			        '$in': ['en']
			    }
			};

			options = {
			    'limit': 1,
			    'sort': {
			        '_id': 1
			    }
			};

			fields = {
			    'text': 0,
			    'review.paragraphs': 0
			};

			$httpBackend.whenGET(buildUrl('text_reviews_for_analytics', query, options, fields)).respond(200, reviews);

			query = {
			    'review.appearedOn': {
			        '$elemMatch': {
			            'title': {
			                '$in': ['t1', 't2']
			            },
			            'channel': {
			            	$regex: '/^((?!call.center).)*$/gi'
			            }
			        }
			    },
			    'review.language': {
			        '$in': ['en']
			    }
			};

			$httpBackend.whenGET(buildCountUrl(query, 'text_reviews_for_analytics')).respond(200, '10');

			query = {
			    'review.appearedOn': {
			        '$elemMatch': {
			            'title': {
			                '$in': [
			                    't1',
			                    't2'
			                ]
			            },
			            'channel': {
			                '$regex': '/^((?!call.center).)*$/gi'
			            }
			        }
			    },
			    'analysis.hasBeenManuallyAnnotated': true,
			    'review.language': {
			        '$in': [
			            'en'
			        ]
			    }
			};

			$httpBackend.whenGET(buildCountUrl(query, 'text_reviews_for_analytics')).respond(200, '5');

			query = {
			    'review.appearedOn': {
			        '$elemMatch': {
			            'title': {
			                '$in': ['t1','t2']
			            },
			            'channel': {
			                '$regex': '/^((?!call.center).)*$/gi'
			            }
			        }
			    },
			    'review.language': {
			        '$in': [
			            'en'
			        ]
			    }
			};


			$httpBackend.whenGET(buildDistinctUrl(query, '_id', 'text_reviews_for_analytics')).respond(200, ['1', '2']);
			
			ReviewsController.nextReviewToAnnotateByTitlesAndDataSource(products[0], 'reviews');
			$httpBackend.flush();

		});

		it('$scope.review should be the correct review', function() {
			expect(ReviewsController.review).to.not.be.undefined;
			expect(ReviewsController.review).to.deep.equal(reviews[0]);
		});

		it('$scope.numberOfReviews should be defined and correct', function() {
			expect(ReviewsController.numberOfReviews).to.not.be.undefined;
			expect(ReviewsController.numberOfReviews).to.equal(10);
		});

		it('$scope.numberOfAnnotatedReviews should be defined and correct', function() {
			expect(ReviewsController.numberOfAnnotatedReviews).to.not.be.undefined;
			expect(ReviewsController.numberOfAnnotatedReviews).to.equal(5);
		});

		it('$scope.percentageOfAnnotatedReviews should be defined and correct', function() {
			expect(ReviewsController.percentageOfAnnotatedReviews).to.not.be.undefined;
			expect(ReviewsController.percentageOfAnnotatedReviews).to.equal(50);
		});

	});
	describe('$scope.previousReview', function() {

		beforeEach(function() {
			
			scope.review = reviews[0];

			var query, options, fields;
		    query = {
		    	'review.appearedOn': {
			        '$elemMatch': {
			            'title': {
			                '$in': [
			                    't1',
			                    't2'
			                ]
			            },
			            'channel': {
			                '$regex': '/^((?!call.center).)*$/gi'
			            }
			        }
			    },
			    '_id': {
			        '$lt': 'ObjectId(\'1\')'
			    },
			    'review.language': {
			        '$in': [
			            'en'
			        ]
			    }
			};

			options = {
			    'limit': 1,
			    'sort': {
			        '_id': -1
			    },
			    'skip': 0
			};

			fields = {
			    'text': 0,
			    'review.paragraphs': 0
			};

			$httpBackend.whenGET(buildUrl('text_reviews_for_analytics', query, options, fields)).respond(200, reviews);

			query = {
			    'review.appearedOn': {
			        '$elemMatch': {
			            'title': {
			                '$in': ['t1','t2']
			            },
			            'channel': {
			                '$regex': '/^((?!call.center).)*$/gi'
			            }
			        }
			    },
			    'review.language': {
			        '$in': [
			            'en'
			        ]
			    }
			};

			$httpBackend.whenGET(buildDistinctUrl(query, '_id', 'text_reviews_for_analytics')).respond(200, ['1', '2']);

			ReviewsController.previousReview(products[0]);

			//$httpBackend.flush();
		});

		it('Should update $scope.review with the new review', function() {
			expect(scope.review).to.not.be.undefined;
			expect(scope.review).to.deep.equal(reviews[0]);
		});
	});

	describe('$scope.nextReview', function() {

		var spy;

		beforeEach(function() {
			spy = sinon.spy(ReviewsController.TextReview, 'getNextReviewByProductAndLowerBoundIdAndLanguages');	
			ReviewsController.review = reviews[0];
			ReviewsController.nextReview(products[0]);
		});

		it('Should', function() {
			expect(spy).to.have.been.called;
		});

	});

	describe('$scope._saveAnnotatedReview', function() {

		var spy;

		beforeEach(function() {
			spy = sinon.spy(ReviewsController.TextReview, 'update');
			ReviewsController._saveAnnotatedReview(reviews[0]);
		});

		it('Should call correctly TextReview.update', function() {
			expect(spy).to.have.been.called;
		});

		it('Should update correctly the review', function() {
			expect(reviews[0].analysis.hasBeenManuallyAnnotated).to.equal(true);
			expect(reviews[0].analysis.manualAnnotations.length).to.equal(1);
		});
	});
});