'use strict';

var utils = require('../../lib/utils');
var fs = require('fs');

describe('TagDictionaryAside', function() {

	beforeEach(function() {

		browser.get(utils.getEndPoint() + '/#/');

		browser.sleep(3 * 1000);
		element(by.css('.w-toggle-product-list-to-analyze')).click();
		browser.sleep(3 * 1000);

		browser.takeScreenshot().then(function(png) {

			var stream = fs.createWriteStream(require('path').resolve('/usr/src/app/node_modules', 'ola.png'));

			stream.write(new Buffer(png, 'base64'));
			stream.end();
			element(by.css('.w-product-name-to-analyze')).click();

		});



	});

	afterAll(function() {
		browser.manage().logs().get('browser').then(function(browserLogs) {
			browserLogs.forEach(function(log) {
				console.log(log.message);
			});
			// browserLogs is an array of objects with level and message fields
			browserLogs.forEach(function(log) {
				if (log.level.value > 900) { // it's an error log
					console.log('Browser console error!');
					console.log(log.message);
				}
			});
		});
	});

	xit('When the user clicks the dictionary button the TagDictionaryAside should be open if it is closed', function() {


		var el;

		el = element(by.css('#tag-dictionary'));

		browser.driver.wait(protractor.until.elementIsNotVisible(el));

		el.isDisplayed().then(function(isDisplayed) {
			expect(isDisplayed).toBe(false);
			return element(by.css('.w-dictionary-button')).click();
		}).then(function() {
			return element(by.css('.tag-dictionary')).isDisplayed();
		}).then(function(isDisplayed) {
			expect(isDisplayed).toBe(true);
		});
	});

	xit('When the user clicks the dictionary button the TagDictionaryAside should be closed if open', function() {

		expect(element(by.css('.tag-dictionary')).isDisplayed()).toBe(false);
		element(by.css('.w-dictionary-button')).click();
		expect(element(by.css('.tag-dictionary')).isDisplayed()).toBe(true);
		element(by.css('.w-dictionary-button')).click();
		expect(element(by.css('.tag-dictionary')).isDisplayed()).toBe(false);

	});

	xit('When the user clicks the dismiss button of the TagDictionaryAside, the TagDictionaryAside should be closed', function() {

		expect(element(by.css('.tag-dictionary')).isDisplayed()).toBe(false);
		element(by.css('.w-dictionary-button')).click();
		expect(element(by.css('.tag-dictionary')).isDisplayed()).toBe(true);
		element(by.css('.w-dictionary-close')).click();
		expect(element(by.css('.tag-dictionary')).isDisplayed()).toBe(false);

	});

	xdescribe('TagDictionaryView', function() {

		beforeEach(function() {

			element(by.css('.w-dictionary-button')).click();

		});

		it('When the user access the dictionary sidebar,  “Visualization Mode Switcher” should be set to `tree`', function() {

			expect(element(by.css('.tag-visualization-mode')).isPresent()).toBe(true);
			expect(utils.hasClass(element(by.id('w-tree-view-tab')), 'tag-visualization-selected')).toBe(true);
			expect(utils.hasClass(element(by.id('w-list-view-tab')), 'tag-visualization-selected')).toBe(false);
			expect(element(by.id('w-tree-tag-view')).isPresent()).toBe(true);
			expect(element(by.id('w-list-tag-view')).isPresent()).toBe(false);

		});

		it('when the user clicks  on `list` of  “Visualization Mode Switcher”,  then the TagListComponent is present and Tag Search Widget is present and TagTreeComponent is not present', function() {

			element(by.id('w-list-view-tab')).click();
			expect(element(by.id('w-tree-tag-view')).isPresent()).toBe(false);
			expect(element(by.id('w-list-tag-view')).isPresent()).toBe(true);
			expect(element(by.id('tag-search')).isPresent()).toBe(true);
			expect(element.all(by.css('.tag-list-view-item')).count()).toEqual(3);

		});

		describe('TagListComponent - when the user clicks on list of  “Visualization Mode Switcher”', function() {

			beforeEach(function() {

				element(by.id('w-list-view-tab')).click();

			});

			it('The list of Tag Component id should be ordered alphabetically according to UID values in ascending order', function() {

				var tagsId = element.all(by.css('.tag-list-view-item .panel-heading .panel-title a span')).getText();

				tagsId.then(function(result) {
					for (var i = 0; i < result.length - 1; i++) {
						expect(result[i] < result[i + 1]).toBe(true);
					}
				});

			});

			it('The `list view` should contain a search input field', function() {

				expect(element(by.id('tag-search')).isPresent()).toBe(true);

			});

			it('when user types "accessories", then only <n> matching tags component appears', function() {

				element(by.id('tag-search')).sendKeys('accessories');
				expect(element.all(by.css('.tag-list-view-item')).count()).toEqual(1);

			});

		});
	});

});