/**
 * Audit class to perform an async analysis of cookies and external domains
 * 
 * @package GDPR::AUDIT::administrator::components::com:gdpr
 * @subpackage js
 * @author Joomla! Extensions Store
 * @copyright (C) 2019 Joomla! Extensions Store
 * @license GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html
 */
//'use strict';
(function($) {
	var Audit = function() {
		/**
		 * Target audit link, default on the website home page
		 * 
		 * @access private
		 * @var String
		 */
		var targetAuditLink = null;
		
		/**
		 * Register user events for interface controls
		 * 
		 * @access private
		 * @param Boolean initialize
		 * @return Void
		 */
		var addListeners = function(initialize) {
			// Append a dialog for the audit tool
			$('div.audit.icons').on('click.audit', function(jqEvent){
				// Remove any previous instance
				$('#audit_dialog').remove();
				showAuditInterface(gdprBaseAuditHome);
			});
			
			// Bind the start audit button
			$(document).on('click.audit', '#start_analysis', function(jqEvent){
				$('#link_to_analyze').removeClass('error');
				
				// Validate the required field
				targetAuditLink = $('#link_to_analyze').val();
				if(!targetAuditLink) {
					$('#link_to_analyze').addClass('error').focus();
					return false;
				}
				
				// Set the running process interface
				$('span.audit-cogicon', this).addClass('running');
				$('span.audit-labelicon-start', this).text(COM_GDPR_AUDIT_STARTED);
				$('#audit_status').text(COM_GDPR_AUDIT_CURRENT_STATUS_RUNNING).addClass('running');
				
				// Start here the real analysis process
				startAudit(targetAuditLink);
			});
			
			$(document).on('keyup.audit', '#link_to_analyze', function(jqEvent){
				$(this).removeClass('error');
			});
			
			// Closer dialog button
			$(document).on('click.audit', 'label.closedialog', function(jqEvent){
				$(this).parents('#audit_dialog').remove();
			});
			
			// Opener of the analysis link
			$(document).on('click.audit', '#linkopener', function(jqEvent){
				var currentLinkValue = $('#link_to_analyze').val();
				window.open(currentLinkValue, '_blank');
			});
		};
		
		/**
		 * Render the Audit dialog box
		 * 
		 * @access private
		 * @return Void
		 */
		var showAuditInterface = function(linkToAnalyze) {
			var contentsString = '';
			
			// Build modal dialog
			var analysisDialog = '<div id="audit_dialog" class="panel panel-primary">' +
									'<div class="panel-heading">' +
								    	'<h3 class="panel-title">' + COM_GDPR_AUDIT_DIALOG_TITLE + '</h3>' +
								    	'<label class="closedialog icon-cancel"></label>' +
								    '</div>' +
								    '<div class="panel-body">' +
								    	'<label class="label label-info analysis-labels" id="linkopener">' + COM_GDPR_AUDIT_TARGET_LINK + '</label>' +
								    	'<input id="link_to_analyze" class="analysis-input" autocomplete="off" type="text" value="' + linkToAnalyze + '" placeholder="' + COM_GDPR_AUDIT_CHOOSE_TARGET_LINK + '"/>' +
								    	'<button id="start_analysis" class="btn btn-success active">' +
								    		'<span class="audit-cogicon"></span><span class="audit-labelicon-start">' + COM_GDPR_AUDIT_START  + '</span>' +
								    	'</button>' +
								    	'<div class="flexbox-container-analysis">' +
									    	'<div id="cookie_results_container"></div>' +
									    	'<div id="domains_results_container"></div>' +
								    	'</div>' +
								    '</div>' +
								    '<div class="panel-footer">' + 
								    	'<span class="label label-warning label-with-badge">' + COM_GDPR_AUDIT_FOOTER + '<span id="audit_status" class="badge pull-right">' + COM_GDPR_AUDIT_CURRENT_STATUS_IDLE + '</span></span>' +
								    '</div>' +
								 '</div>';
			// Inject elements into content body
			$('body').append(analysisDialog);
		}

		
		/**
		 * Process the asyncronous audit of the website link that has been specified
		 * It performs an analysis of both cookies (local) and external domains found in the page source code
		 *
		 * @access private
		 * @param String linkToAnalyze
		 * @return Void
		 */
		var startAudit = function(linkToAnalyze) {
			// No ajax request if no valid link and keyword specified
			if(!linkToAnalyze) {
				// Reset the analyzing button
				$('span.audit-cogicon').removeClass('running');
				$('span.audit-labelicon-start').text(COM_GDPR_AUDIT_START);
				$('#audit_status').text(COM_GDPR_AUDIT_CURRENT_STATUS_IDLE).removeClass('running');
				return;
			}
			
			// Clear previous results
			$('label.label-results, ul.analysis-results-cookies, ul.analysis-results-domains').fadeOut(100);
			
			// Async request
			var auditPromise = $.Deferred(function(defer) {
				$.ajax({
					type : "GET",
					url : linkToAnalyze,
				}).done(function(data, textStatus, jqXHR) {
					// Check response HTTP status code
					defer.resolve(data, jqXHR.status, jqXHR.getAllResponseHeaders());
				}).fail(function(jqXHR, textStatus, errorThrown) {
					// Error found
					defer.resolve(null, jqXHR.status + ' ' + errorThrown);
				});
			}).promise();

			auditPromise.then(function(responseData, status, headers) {
				if(status == 200 && responseData) {
					// All went ok, now analyze and report data
					// STEP 1 - Set the parsed wrapped set
					var responseDataString = responseData.trim();

					/**
					 * STEP 2 Analyze cookies
					 */
					$('#audit_dialog div.panel-body #cookie_results_container').append('<label class="label label-success analysis-labels label-results">' + COM_GDPR_AUDIT_RESULTS_COOKIE + '</label>');
					var ulCookieResults = $('<ul/>');
					ulCookieResults.addClass('analysis-results-cookies');
					ulCookieResults.append('<li><span>' + COM_GDPR_AUDIT_RESULTS_COOKIE_NAME + '</span><span>' + COM_GDPR_AUDIT_RESULTS_COOKIE_VALUE + '</span></li>');
					
					// STEP 3 - Get the local cookies found
					var addedFoundCookies = false;
					var documentCookies = document.cookie.trim();
					if(documentCookies) {
						var cookies = documentCookies.split("; ");
						// Loop through all cookies
						for (var c = 0; c < cookies.length; c++) {
							var splittedCookie = cookies[c].split("=");
							if(splittedCookie[0] == 'cookieconsent_status') {
								continue;
							}
							ulCookieResults.append(	'<li>' +
															'<span>' +
															'<a class="hasAuditTooltip" title="' + COM_GDPR_AUDIT_SEARCH_COOKIE + '" href="https://www.google.com/search?q=' + encodeURIComponent(splittedCookie[0]) + '" target="_blank">' + splittedCookie[0] + '</a>' +
															'</span>' +
															'<span class="hasCookieValue">' + 
															'<span>' +
															splittedCookie[1] +
															'</span>' +
															'</span>' +
													'</li>');
							addedFoundCookies = true;
						}
					}
					if(!addedFoundCookies) {
						ulCookieResults.append('<li><span>' + COM_GDPR_AUDIT_RESULTS_NOCOOKIES + '</span></li>');
					}
					
					$('#audit_dialog div.panel-body #cookie_results_container').append(ulCookieResults);
					
					/**
					 * STEP 4 - Analyze domains/resources
					 */
					$('#audit_dialog div.panel-body #domains_results_container').append('<label class="label label-success analysis-labels label-results">' + COM_GDPR_AUDIT_RESULTS_DOMAINS + '</label>');
					var ulDomainsResults = $('<ul/>');
					ulDomainsResults.addClass('analysis-results-domains');
					ulDomainsResults.append('<li><span>URL</span></li>');
					
					// Focus keyword regular expression object
					var reObject = new RegExp(/\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}/, 'gi');
					
					// STEP 5 - Get the external domains/resources of the page
					var addedDomains = [];
					var foundDomains = responseDataString.match(reObject);
					if(foundDomains) {
						// Cycle all found domains
						$.each(foundDomains, function(index, domain) {
							// Exclude domains that are already added and the local domain
							if($.inArray(domain, addedDomains) == -1 && domain.indexOf(gdprBaseDomain) == -1) {
								var purifiedDomain = domain.replace(/^\/\//gi, '');
								ulDomainsResults.append('<li><a class="hasAuditTooltip" title="' + COM_GDPR_AUDIT_SEARCH_DOMAIN + '" href="https://www.google.com/search?q=' + encodeURIComponent(purifiedDomain) + '" target="_blank">' + purifiedDomain + '</a></li>');
								addedDomains[index] = domain;
							}
						});
					}
					if(addedDomains.length == 0) {
						ulDomainsResults.append('<li><span>' + COM_GDPR_AUDIT_RESULTS_NODOMAINS + '</span></li>');
					}
					
					$('#audit_dialog div.panel-body #domains_results_container').append(ulDomainsResults);
				} else {
					// An error in the AJAX request occurred, kindly inform user and return
					$('#audit_dialog div.panel-body').append('<label class="label label-important analysis-labels label-results">' + COM_GDPR_AUDIT_AUDIT_ERROR + status + '</label>');
					return;
				}
			}).always(function(){
				// Reset the analyzing button
				$('span.audit-cogicon').removeClass('running');
				$('span.audit-labelicon-start').text(COM_GDPR_AUDIT_START);
				$('#audit_status').text(COM_GDPR_AUDIT_CURRENT_STATUS_IDLE).removeClass('running');
				
				// Initialize tooltips of the audit results
				$('a.hasAuditTooltip').tooltip({trigger:'hover', placement:'top', html : 1});
			});
		};

		/**
		 * Function dummy constructor
		 * 
		 * @access private
		 * @param String
		 *            contextSelector
		 * @method <<IIFE>>
		 * @return Void
		 */
		(function __construct() {
			// Add UI events
			addListeners.call(this, true);
		}).call(this);
	}

	// On DOM Ready
	$(function() {
		window.GDPRAudit = new Audit();
	});
})(jQuery);