'use strict';
var path = require('path');
var fs = require('fs-path');
var glob = require('glob');
var AxeBuilder = require('axe-webdriverjs');
var WebDriver = require('selenium-webdriver');
var Promise = require('promise');
var fileUrl = require('file-url');
var reporter = require('./lib/reporter');
require('chromedriver');

//setup custom phantomJS capability
var phantomjs_exe = require('phantomjs-prebuilt').path;
var customPhantom = WebDriver.Capabilities.phantomjs().set('phantomjs.binary.path', phantomjs_exe);

module.exports = function (customOptions, done) {

	var defaultOptions = {
		folderOutputReport: 'aXeReports',
		browser: 'chrome',
		saveOutputIn: '',
		tags: null,
		urls: [],
		threshold: 0
	};

	var urlToGet = '';
	var options = customOptions ? Object.assign(defaultOptions, customOptions) : defaultOptions;
	var driver = options.browser === 'phantomjs' ? new WebDriver.Builder().withCapabilities(customPhantom).build() : new WebDriver.Builder().forBrowser(options.browser).build();
	var tagsAreDefined = (!Array.isArray(options.tags) && options.tags !== null && options.tags !== '') || (Array.isArray(options.tags) && options.tags.length > 0);

	var isRemoteUrl = function(url) {
		return url.indexOf('http://') >= 0 || url.indexOf('https://') >= 0;
	};

	var flatten = function(arr) {
		return [].concat.apply([], arr);
	};

	var createResults = function(results) {

		var dest = '';
		if(options.saveOutputIn !== '') {
			dest = path.join(options.folderOutputReport, options.saveOutputIn);
			fs.writeFileSync(dest, JSON.stringify(results, null, '  '));
		}
		reporter(results, options.threshold);
		driver.quit().then(function() {
				done();
		});

	};

	var findGlobPatterns = function(urls) {
		return urls.map(function(url) {
			return isRemoteUrl(url) ? url : glob.sync(url);
		});
	};

	var urls = flatten(findGlobPatterns(options.urls));

	Promise.all(urls.map(function(url) {
			urlToGet = isRemoteUrl(url) ? url : fileUrl(url); 
			return new Promise(function(resolve) {
				driver
					.get(urlToGet)
					.then(function() {
						var startTimestamp = new Date().getTime();
						var axeBuilder = new AxeBuilder(driver);

						if (tagsAreDefined) {
							axeBuilder.withTags(options.tags);
						}

						axeBuilder.analyze(function(results) {
							results.url = urlToGet;
							results.timestamp = new Date().getTime();
							results.time = results.timestamp - startTimestamp;
							resolve(results);
						});
					});
			});
		})).then(createResults);

};

@@ -716,28 +716,8 @@
 %7D;%0A%0A
-%09var urlToGet = '';%0A
 %09var
@@ -806,16 +806,16 @@
 ptions;%0A
+
 %09var dri
@@ -1321,24 +1321,106 @@
  arr);%0A%09%7D;%0A%0A
+%09var getUrl = function(url) %7B%0A%09%09return isRemoteUrl(url) ? url : fileUrl(url);%0A%09%7D%0A%0A
 %09var createR
@@ -1930,16 +1930,17 @@
 rls));%0A%0A
+%0A
 %09Promise
@@ -1972,61 +1972,8 @@
 l) %7B
-%0A%09%09%09urlToGet = isRemoteUrl(url) ? url : fileUrl(url);
  %0A%09%09
@@ -2033,24 +2033,27 @@
 %09%09%09.get(
-urlToGet
+getUrl(url)
 )%0A%09%09%09%09%09.
@@ -2287,24 +2287,24 @@
 (results) %7B%0A
+
 %09%09%09%09%09%09%09resul
@@ -2319,13 +2319,8 @@
  url
-ToGet
 ;%0A%09%09

change how to show the url to test