'use strict';
/* global  beforeEach */


// var screenshots = require('protractor-screenshots');



/*beforeEach(function() {
	var width,
		height;

	width = 1366;
	height = 768;

	return browser.driver.manage().window().setSize(width, height).then(function() {
		// screenshots.checkScreenshot(this, '___BEFORE');
		//return browser.waitForAngular();
	});

});*/

var MongoIo = require('mongo-io');
var async   = require('async');
var path    = require('path');
var Logger  = require('logb').getLogger(module.filename);

beforeEach(function prepareDatabase(done) {
	console.log('--------------------> Loading database...',process.env.DB_HOST,process.env.DB_PORT,process.env.DB_NAME);
	var collectionNames = ['products', 'textcontents', 'text_reviews_for_analytics', 'users', 'tags'];
	var collectionData = collectionNames.map(function(collectionName) {
		var tmp = {
			host       : process.env.DB_HOST,
			port       : process.env.DB_PORT,
			db         : process.env.DB_NAME,
			collection : collectionName,
			path       : path.resolve(__dirname, '../data/' + collectionName + '.json')
		};
		return tmp;
	});
	async.each(collectionData, function(item, next) {
		// console.log('1');
		MongoIo.emptyCollection(item, function(err) {
			if (err) {
				Logger.error('Error', err);
				return next(err);
			}
			// console.log('1. Waiting 10sec');
			// setTimeout(function() {
			MongoIo.mongoimport(item, function(err) {
				if (err) {
					Logger.error('Error', err);
					process.exit();
					return next(err);
				}
				// console.log('3', item);
				// console.log('2. Waiting 10sec');
				// setTimeout(function() {
				next();
				// }, 10 * 1000);
			});
			// }, 10 * 1000);
			// console.log('2');
			// console.log('item', item)

		});
	}, function(err) {
		if (err) {
			Logger.error('Error', err);
			process.exit();
		}
		// console.log('4');

		done();
		console.log('--------------------> Finished loading database...');
	});
});