/*global QUnit */
sap.ui.define([
	"sap/ui/test/_LogCollector",
	"sap/ui/test/_OpaLogger",
	"sap/ui/test/autowaiter/_resourceWaiter",
	"sap/m/Image",
	"sap/ui/thirdparty/URI"
], function (_LogCollector, _OpaLogger, _resourceWaiter, Image, URI) {
	"use strict";

	// before running this test in IE11, clear cached images

	var oLogCollector = _LogCollector.getInstance();

	QUnit.module("ResourceWaiter", {
		beforeEach: function () {
			this.defaultLogLevel = _OpaLogger.getLevel();
			_OpaLogger.setLevel("trace");
			var oCurrentUri = new URI();
			var sPrefix = oCurrentUri.path().split('/')[1] === "testsuite" ? "/testsuite" : "";
			var sBase = oCurrentUri.origin() + sPrefix + "/test-resources/sap/ui/core/images/";
			this.sExistingImageSrc = sBase + "Mobile.png";
			this.sReplacerImageSrc = sBase + "Notebook.png";
			this.sNotFoundSrc = sBase + "noSuchImage.jpg";
		},
		afterEach: function () {
			_OpaLogger.setLevel(this.defaultLogLevel);
			oLogCollector.getAndClearLog(); // cleanup
		}
	});

	function assertImagePending(sSrc, assert) {
		var bHasPending = _resourceWaiter.hasPending();
		var sLogs = oLogCollector.getAndClearLog();
		if (bHasPending) {
			assert.ok(true, "Has pending resources");
			// image could be cached or loaded in previous steps
			assert.ok(sLogs.match("Pending resource: " + sSrc) ||
				sLogs.match("Image with src '" + sSrc + "' is updated and pending again"),
				"Image with src " + sSrc + " is pending");
		} else {
			// when CPU is very slow, hasPending may be called before load handler
			assert.ok(true, "Has no pending resources");
			assert.ok(sLogs.match("Image with src '" + sSrc + "' completed") ||
				sLogs.match("Image with src '" + sSrc + "' loaded successfully"),
				"Image already loaded");
		}
	}

	// workaround for image loading (async) - there can multiple images created for 1 test, but we only check one at a time
	function assertNoRelevantPending(sRelevantSrc, bPass, assert) {
		_resourceWaiter.hasPending();
		var sLogs = oLogCollector.getAndClearLog();
		var aRelevantPending = sLogs.match("Pending resource: " + sRelevantSrc);
		assert.ok(!aRelevantPending, "Should have no pending resources with src " + sRelevantSrc);
		assert.ok(sLogs.match("Image with src '" + sRelevantSrc + (bPass ? "' loaded successfully" : "' failed to load")),
			"Image with src " + sRelevantSrc + " is not pending");
	}

	QUnit.test("Should wait for image to load", function (assert) {
		var fnDone = assert.async();
		this.oImageExistingSrc = new Image({
			src: this.sExistingImageSrc
		});
		this.oImageExistingSrc.placeAt("qunit-fixture");
		sap.ui.getCore().applyChanges();

		// wait for the rendering to start
		setTimeout(function () {
			assertImagePending(this.sExistingImageSrc, assert);

			this.oImageExistingSrc.attachLoad(fnOnComplete(true).bind(this));
		}.bind(this), 0);

		function fnOnComplete(bPass) {
			return function () {
				// wait for other load handlers
				setTimeout(function () {
					assertNoRelevantPending(this.sExistingImageSrc, bPass, assert);

					// cleanup
					this.oImageExistingSrc.destroy();
					sap.ui.getCore().applyChanges();
					fnDone();
				}.bind(this), 0);
			};
		}
	});

	QUnit.test("Should wait for image to complete with error", function (assert) {
		var fnDone = assert.async();
		this.oImageWrongSrc = new Image({
			src: this.sNotFoundSrc
		});
		this.oImageWrongSrc.placeAt("qunit-fixture");
		sap.ui.getCore().applyChanges();

		// wait for the rendering to start
		setTimeout(function () {
			assertImagePending(this.sNotFoundSrc, assert);

			this.oImageWrongSrc.attachError(function () {
				// wait for other load handlers
				setTimeout(function () {
					assertNoRelevantPending(this.sNotFoundSrc, false, assert);

					// cleanup
					this.oImageWrongSrc.destroy();
					sap.ui.getCore().applyChanges();
					fnDone();
				}.bind(this), 0);
			}.bind(this));
		}.bind(this), 0);
	});

	QUnit.test("Should wait for image to load when src is changed", function (assert) {
		var fnDone = assert.async();
		this.oImageWrongSrc = new Image({
			src: this.sNotFoundSrc
		});
		this.oImageWrongSrc.placeAt("qunit-fixture");
		sap.ui.getCore().applyChanges();

		var bSrcChanged = false;

		this.oImageWrongSrc.attachError(function () {
			if (!bSrcChanged) {
				bSrcChanged = true;
				setTimeout(function () {
					assertNoRelevantPending(this.sNotFoundSrc, false, assert);

					// update image - from sNotFoundSrc to sReplacerImageSrc
					this.oImageWrongSrc.setSrc(this.sReplacerImageSrc);
					sap.ui.getCore().applyChanges();

					// wait for the rendering to start
					setTimeout(function () {
						assertImagePending(this.sReplacerImageSrc, assert);

						this.oImageWrongSrc.attachLoad(fnOnComplete(true).bind(this));
					}.bind(this), 0);
				}.bind(this), 0);
			}
		}.bind(this));

		function fnOnComplete(bPass) {
			return function () {
				// wait for load handlers
				setTimeout(function () {
					assertNoRelevantPending(this.sReplacerImageSrc, bPass, assert);

					// cleanup
					this.oImageWrongSrc.destroy();
					sap.ui.getCore().applyChanges();
					fnDone();
				}.bind(this), 0);
			};
		}
	});
});

@@ -145,34 +145,8 @@
 age%22
-,%0A%09%22sap/ui/thirdparty/URI%22
 %0A%5D, 
@@ -208,13 +208,8 @@
 mage
-, URI
 ) %7B%0A
@@ -489,170 +489,17 @@
 var 
-oCurrentUri = new URI();%0A%09%09%09var sPrefix = oCurrentUri.path().split('/')%5B1%5D === %22testsuite%22 ? %22/testsuite%22 : %22%22;%0A%09%09%09var sBase = oCurrentUri.origin() + sPrefix + %22/
+sBase = %22
 test
@@ -1120,24 +1120,26 @@
 g resource: 
+.*
 %22 + sSrc) %7C%7C
@@ -1164,32 +1164,34 @@
 Image with src '
+.*
 %22 + sSrc + %22' is
@@ -1434,32 +1434,34 @@
 Image with src '
+.*
 %22 + sSrc + %22' co
@@ -1502,24 +1502,26 @@
 e with src '
+.*
 %22 + sSrc + %22
@@ -1902,16 +1902,18 @@
 source: 
+.*
 %22 + sRel
@@ -2008,32 +2008,32 @@
  sRelevantSrc);%0A
-
 %09%09assert.ok(sLog
@@ -2057,16 +2057,18 @@
 th src '
+.*
 %22 + sRel

fix image path is resourceWaiter test