/** Sibs function to show feedback pop up
	*
	* @package Sibs/js
	*/


// { 
// host: "website.com",
// pathname: "/.../",
// merchant_id: 123,
// terminal_id: 123,
// platform: "WooCommerce",
// platformVersion: "x.x",
// pluginVersion: "x.x",
// browser: "Chrome",
// connection_speed: "xxx Mbps",
// iframe_load_start: datetime,
// iframe_load_end: datetime
// }

jQuery(function($) {

	var sibs_information 	= [];
	var time_ready 			= null;
	var time_load 			= null;
	var sToken = "NA";
	var url_site = window.location.href;

	sibs_information.push({
		host: window.location.host
	});
	sibs_information.push({
		pathname: window.location.pathname
	});
	sibs_information.push({
		search: window.location.search
	});
	sibs_information.push({
		sToken: sToken
	});
	sibs_information.push({
		browser: navigator.userAgent
	});
	// languages
	sibs_information.push({
		languages: navigator.languages
	});


	testConnectionSpeed.run(1.5, function(mbps) {
		console.log(">= 1.5Mbps (" + mbps + "Mbps)");
		sibs_information.push({
			connection_speed: mbps + "Mbps)"
		});
		logSibs(sibs_information, url_site);
	}, function(mbps) {
		console.log("< 1.5Mbps(" + mbps + "Mbps)");
		sibs_information.push({
			connection_speed: mbps + "Mbps)"
		});
		logSibs(sibs_information, url_site);
	})

	function time() {
		var currentdate = new Date();
		var datetime = "" + currentdate.getFullYear() + "-" +
			(currentdate.getMonth() + 1) + "-" +
			currentdate.getDate() + " " +
			currentdate.getHours() + ":" +
			currentdate.getMinutes() + ":" +
			currentdate.getSeconds();
		console.log(datetime);
		return datetime;
	}

	function logSibs(data, url) {
		console.log(JSON.stringify(data))
		$.ajax({
			type: "POST",
			url: url + "/wp-json/sibs-stg/webhook/log",
			data: JSON.stringify(data),
			dataType: "json",
		}).done(function() {
			console.log("second success");
		});
	}

	jQuery(document).ready(function() {
		console.log("ready: ");
		var time1 = time();
		sibs_information.push({
			ready_page: time1
		});
		time_ready = time1;
	});

	jQuery(window).load(function() {
		console.log("load: ");
		var time1 = time();
		sibs_information.push({
			lead_page: time1
		});
		time_load = time1;
		logSibs(sibs_information, url_site);

		var date2 = new Date(time_ready);
		var date1 = new Date(time_load);
		var date3 = new Date(Date.parse(date1) - Date.parse(date2));
		console.log(date3)

	});

	jQuery(document).ready(function() {
		
		jQuery('#spgframe').ready(function() {
			console.log("spgframe -> ready:: ");
			var time1 = time();
		//	jQuery(".paymentSPG").before("<div class='spg_loading'><img src='https://upload.wikimedia.org/wikipedia/commons/b/b1/Loading_icon.gif?20151024034921'></div>");
			sibs_information.push({
				iframe_load_start: "" + time1
			});
			console.log(sibs_information);
			logSibs(sibs_information, url_site);
		});
		jQuery('#spgframe').load(function() {
			console.log("spgframe -> load:: ");
			var time2 = time();
			jQuery('.spg_loading').remove();
			sibs_information.push({
				iframe_load_end: "" + time2
			});
			console.log(sibs_information);
			logSibs(sibs_information, url_site);
		});

	});


});



var testConnectionSpeed = {
	imageAddr: "https://upload.wikimedia.org/wikipedia/commons/a/a6/Brandenburger_Tor_abends.jpg",
	// this is just an example, you rather want an image hosted on your server
	downloadSize: 2707459,
	// Must match the file above (from your server ideally)
	run: function(mbps_max, cb_gt, cb_lt) {
		testConnectionSpeed.mbps_max = parseFloat(mbps_max) ? parseFloat(mbps_max) : 0;
		testConnectionSpeed.cb_gt = cb_gt;
		testConnectionSpeed.cb_lt = cb_lt;
		testConnectionSpeed.InitiateSpeedDetection();
	},
	InitiateSpeedDetection: function() {
		window.setTimeout(testConnectionSpeed.MeasureConnectionSpeed, 1);
	},
	result: function() {
		var duration = (endTime - startTime) / 1000;
		var bitsLoaded = testConnectionSpeed.downloadSize * 8;
		var speedBps = (bitsLoaded / duration).toFixed(2);
		var speedKbps = (speedBps / 1024).toFixed(2);
		var speedMbps = (speedKbps / 1024).toFixed(2);
		if (speedMbps >= (testConnectionSpeed.max_mbps ? testConnectionSpeed.max_mbps : 1)) {
			testConnectionSpeed.cb_gt ? testConnectionSpeed.cb_gt(speedMbps) : false;
		} else {
			testConnectionSpeed.cb_lt ? testConnectionSpeed.cb_lt(speedMbps) : false;
		}
	},
	MeasureConnectionSpeed: function() {
		var download = new Image();
		download.onload = function() {
			endTime = (new Date()).getTime();
			testConnectionSpeed.result();
		}
		startTime = (new Date()).getTime();
		var cacheBuster = "?nnn=" + startTime;
		download.src = testConnectionSpeed.imageAddr + cacheBuster;
	}
}