'use strict';


var Analyst = require('../../lib/user-analyst'),
	Administrator = require('../../lib/user');

describe('Insights Editor', function() {
	var browser1;
	var analyst, administrator;
	var user1Waiting, user2Waiting;

	var productName;


	beforeEach(function() {
		browser1 = browser;
		user1Waiting = 5;
		user2Waiting = 5;
	});

	afterAll(function(){
		// TO RE-USE WISSE CREDENTIALS INSTEAD OF CHIARA.LAGANA - A SORT OF LOGOUT
		return new Administrator().login();
	});

	describe('Use case: link an insight', function(){
		xit('when a user link an insights to a review, then another user is notified of the same update', function(){

		}).pend('Not yet implemented!');
	});



	describe('UseCase: add a new insight', function() {
		var textOfInsight;
		var browser2;
		var analyst2;


		describe('when an analyst adds a new insight', function() {


			it('then the insight has unpublished as state and then another user is notified', function() {
				textOfInsight = 'this is the text of the insight';

				analyst = new Analyst();
				administrator = new Administrator();
				browser2 = browser.forkNewDriverInstance();
				analyst2 = new Analyst({
					browser: browser2
				});

				// console.log('analyst', analyst);
				// console.log('analyst2', analyst2);

				return analyst.login()
					.then(function() {
						console.log('Waiting for 5 secs...')
						return browser.sleep(5 * 1000);
					})
					.then(function() {
						return analyst.takeScreenshot('opening list of products to analyze');
					})
					.then(function() {
						console.log('Opening list of products to analyze')
						return analyst.openListOfProductToAnalyze();
					})
					.then(function() {
						// TODO: replace as expeced cond waiting for w-product-name-to-analyze
						return browser.sleep(5 * 1000);
					})
					.then(function() {
						return analyst.selectProductToAnalyzeByHumanIdentifier('Cooltech no CS - gb');
					})
					.then(function() {
						return analyst.openInsightsEditor();
					})
					.then(function() {
						return analyst2.login();
					})
					.then(function() {
						return analyst2.openListOfProductToAnalyze();
					})
					.then(function() {
						return analyst2.selectProductToAnalyzeByHumanIdentifier('Cooltech no CS - gb');
					})

				.then(function() {
						// TODO: replace as expeced cond waiting for w-product-name-to-analyze
						return browser.sleep(5 * 1000);
					})
					.then(function() {
						return analyst2.openInsightsEditor();
					})
					.then(function() {
						return analyst.clickOnAddNewInsightButton();
					})
					.then(function() {
						return analyst.setTextOfInsight(textOfInsight);
					})
					.then(function() {
						return analyst.saveInsight();
					})
					.then(function() {
						return analyst.pause();
					})
					.then(function() {
						return analyst.pause();
					})
					.then(function() {
						return administrator.goToInsightsListByProductId('5521d4767983010100185c01');
					})
					.then(function() {
						return browser.sleep(3 * 1000);
					})
					.then(function() {
						administrator.takeScreenshot('insights-editor');

						return administrator.countInsightByInsightText(textOfInsight)
							.then(function(numberOfInsights) {
								expect(numberOfInsights).toBe(1);
							});
					})
					.then(function(){
						return analyst2.browser.element.all(by.css('.w-insight-item')).map(function(e){
							return e.getText();
						})
						.then(function(ts){
							console.log('ts',ts);
						})
					})
					.then(function() {
						expect(administrator.readStateOfLastInsight()).toBe('PUBLISH');

						return analyst2.countInsights();
					})
					.then(function(numberOfInsights) {
						expect(numberOfInsights).toBe(72);
					});

			});
		});



	});



});