/* jshint node:true */
'use strict';

var URL_PREFIX = null;
var buildUrl, buildCountUrl, buildDistinctUrl, User;

beforeEach(function() {
	angular.module('EnterpriseDashboardApp.HostFactory', []);
	angular.mock.module(function($provide) {
		$provide.service('HostFactory', function() {
			this.getScheme = function() {
				return 'http';
			};
			this.getHost = function() {
				return 'development.api.wonderflow.co';
			};
			this.getDbUrlComponent = function() {
				return 'enterprise_dashboard_development_data';
			};
			this.getDataExportServerHost = function() {
				return 'api.wonderflow.co';
			};
			this.getEmailSenderHost = function() {
				return 'api.wonderflow.co';
			};
			this.getWebSocketUrl = function() {
				return 'ws://wonderflow';
			};
		});
	});
});

beforeEach(function(){
	window.mixpanel={
		track:function(){}
	};
	window.sessionStorage.user = JSON.stringify({
		username: 'username@wonderflow.co'
	});
});
beforeEach(angular.mock.module("app"));
beforeEach(angular.mock.module("Data"));

beforeEach(inject(function( _Model_, _HostFactory_, _User_) {

	var Model = _Model_;
	var HostFactory = _HostFactory_;
	User = _User_;

	URL_PREFIX = HostFactory.getScheme() + '://' + HostFactory.getHost() + '/api/' + HostFactory.getDbUrlComponent();
	buildUrl = function(collectionName, query, options, fields) {
		return new Model({
			scheme: HostFactory.getScheme(),
			host: HostFactory.getHost(),
			dbName: HostFactory.getDbUrlComponent(),
			collectionName: collectionName
		})._buildUrl(query, options, fields);
	};

	buildCountUrl = function(query, collectionName) {
		query = JSON.stringify(query);
		query = encodeURIComponent(query);

		var urlPath = URL_PREFIX + '/' + collectionName;
		urlPath += '/count?rawQuery=' + query;

		return urlPath;
	};

	buildDistinctUrl = function(query, fieldName, collectionName) {

		query = JSON.stringify(query);
		query = encodeURIComponent(query);

		var urlPath = URL_PREFIX + '/' + collectionName;
		urlPath += '/distinct/' + fieldName + '?rawQuery=' + query;

		return urlPath;
	};

	
	var user = {
        username: 'username@wonderflow.co',
        role: 'admin'
    };
	User.get = function() {
        return user;
    };
    window.sessionStorage.user = JSON.stringify(user);
}));

beforeEach(inject(function(_$httpBackend_) {
	var $httpBackend = _$httpBackend_;
	$httpBackend.whenGET('angular-notify.html').respond(200, '<div></div>');
}));

export { URL_PREFIX, buildUrl,  buildCountUrl, buildDistinctUrl, User};