/* globals jQuery:true, ajaxurl:true, sn_block_ui:true, secninScheduledScanner:true */
/**
 * Security Ninja - Scheduled Scanner
 * (c) Web factory 2015
 * Updated 2020 - Larsik Corp
 */

var logsLoaded = false;

function fetchLogs() {
	var data = {
		action: 'sn_ss_get_logs',
		nonce: secninScheduledScanner.nonce
	};

	jQuery.post( ajaxurl, data, function( response ) {
		if ( response.success ) {
			logsLoaded = true;
			jQuery( '#wf-sn-ss-log tbody' ).html( response.data );
		} else {
			jQuery( '#wf-sn-ss-log tbody' ).html( '<tr><td colspan="4"><span class="no-logs">' + response.data + '</span></td></tr>' );
		}
	} );
}

function maybeLoadLogs() {
	if ( window.location.hash !== '#sn_schedule' ) {
		return;
	}
	if ( logsLoaded ) {
		return;
	}
	jQuery( '#wf-sn-ss-log tbody' ).html( '<tr><td colspan="4"><span class="spinner is-active"></span> Loading…</td></tr>' );
	fetchLogs();
}

function whenSchedulerTabFocused() {
	if ( window.location.hash !== '#sn_schedule' ) {
		return;
	}
	maybeLoadLogs();
}

jQuery( document ).ready( function( $ ) {

	jQuery( window ).on( 'hashchange', whenSchedulerTabFocused );
	jQuery( document ).on( 'click', '#wf-sn-tabs a[href="#sn_schedule"]', function() {
		whenSchedulerTabFocused();
	} );
	if ( window.location.hash === '#sn_schedule' ) {
		whenSchedulerTabFocused();
	}

	// Run scheduler test via AJAX.
	jQuery( '#sn-ss-test' ).on( 'click', function() {
		if ( ! confirm( 'Please remember to save settings before testing them. Continue?' ) ) {
			return;
		}
		var data = {
			action: 'sn_ss_cs_test',
			nonce: secninScheduledScanner.nonce
		};
		jQuery( '#wf-ss-output' ).show();

		var startTime = Date.now();
		var $output = jQuery( '#wf-ss-output' );
		var timer = setInterval( function() {
			var elapsedSeconds = Math.floor( ( Date.now() - startTime ) / 1000 );
			$( '#wf-ss-output #sn-timer' ).text( 'Time elapsed: ' + elapsedSeconds + ' seconds' );
		}, 1000 );

		jQuery.ajax( {
			url: ajaxurl,
			type: 'POST',
			data: data,
			success: function( response ) {
				clearInterval( timer );
				$output.empty();
				$output.append( '<p>Success: ' + response.data.message + '</p>' );
			},
			error: function() {
				clearInterval( timer );
				$output.empty();
			}
		} );
	} );

	// Truncate log table.
	$( '#wf-sn-ss-truncate-log' ).on( 'click', function() {
		var answer = confirm( 'Are you sure you want to delete all log entries?' );
		if ( answer ) {
			var data = {
				action: 'sn_ss_truncate_log',
				nonce: secninScheduledScanner.nonce
			};
			$.post( ajaxurl, data, function( response ) {
				if ( ! response ) {
					alert( 'Bad AJAX response. Please reload the page.' );
				} else {
					fetchLogs();
				}
			} );
		}
		return false;
	} );

	// Security Ninja results details.
	$( document ).on( 'click', '.ss-details-sn', function() {
		var $this = $( this );
		var data = {
			action: 'sn_ss_sn_details',
			nonce: secninScheduledScanner.nonce,
			row_id: $this.attr( 'data-row-id' )
		};
		var timestamp = $this.attr( 'data-timestamp' );

		$( '.ss-details-sn' ).prop( 'disabled', true );
		$this.append( '<span class="spinner is-active"></span>' );

		$.post( ajaxurl, data, function( response ) {
			$( '.spinner' ).remove();
			$( '.ss-details-sn' ).prop( 'disabled', false );
			if ( ! response ) {
				alert( 'Bad AJAX response. Please reload the page.' );
			} else {
				$( '#sn-scheduled-scanner-results' ).html( response )
					.dialog( { title: 'Security Ninja results from ' + timestamp } )
					.dialog( 'open' );
			}
		} ).fail( function() {
			alert( 'An error occurred. Please try again.' );
			$( '.spinner' ).remove();
			$( '.ss-details-sn' ).prop( 'disabled', false );
		} );
		return false;
	} );

	// Core Scanner results details.
	$( document ).on( 'click', '.ss-details-cs', function() {
		var $this = $( this );
		var data = {
			action: 'sn_ss_cs_details',
			row_id: $this.attr( 'data-row-id' ),
			nonce: secninScheduledScanner.nonce
		};
		var timestamp = $this.attr( 'data-timestamp' );

		$( '.ss-details-cs' ).prop( 'disabled', true );
		$this.append( '<span class="spinner is-active"></span>' );

		$.post( ajaxurl, data, function( response ) {
			$( '.spinner' ).remove();
			$( '.ss-details-cs' ).prop( 'disabled', false );
			if ( ! response ) {
				alert( 'Bad AJAX response. Please reload the page.' );
			} else {
				jQuery( '#sn-scheduled-scanner-results' ).html( response )
					.dialog( { title: 'Core Scanner results from ' + timestamp } )
					.dialog( 'open' );
			}
		} ).fail( function() {
			alert( 'An error occurred. Please try again.' );
			$( '.spinner' ).remove();
			$( '.ss-details-cs' ).prop( 'disabled', false );
		} );
		return false;
	} );

	// Results dialog.
	jQuery( '#sn-scheduled-scanner' ).dialog( {
		dialogClass: 'wp-dialog',
		modal: true,
		resizable: true,
		zIndex: 9999,
		width: 900,
		height: '550',
		hide: 'fade',
		show: 'fade',
		autoOpen: false,
		closeOnEscape: true
	} );

} );
