/**
 * Status page script.
 *
 * @package northmill-online-shared
 */

jQuery( document ).on(
	'ready',
	function () {
		new NMF_Status_Page();
	}
);
/* global northmill_online_shared_admin_status */
class NMF_Status_Page {
	constructor() {
		// Bindings.
		this.logging_test                         = this.logging_test.bind( this );
		this.change_latest_sync_time              = this.change_latest_sync_time.bind( this );
		this.remove_cron_lock                     = this.remove_cron_lock.bind( this );
		this.remove_update_transient              = this.remove_update_transient.bind( this );
		this.empty_wc_tax_rates_table             = this.empty_wc_tax_rates_table.bind( this );
		this.remove_woocommerce_tax_rates_mf_rows = this.remove_woocommerce_tax_rates_mf_rows.bind( this );

		jQuery( '.nm-status-logging-test' ).on( 'click', this.logging_test );

		jQuery( '.nmf-latest-sync' ).on( 'click', this.click_latest_sync_time );
		jQuery( document ).on( 'keyup', '.nmf-latest-sync input', this.change_latest_sync_time );

		jQuery( '.nmf-cron-remove' ).on( 'click', this.remove_cron_lock );

		jQuery( '.nmf-plugin-update-transient-remove' ).on( 'click', this.remove_update_transient );

		jQuery( '#nm-empty-woocommerce-tax-rates' ).on( 'click', this.empty_wc_tax_rates_table );
		jQuery( '#nm-remove-woocommerce-tax-rates-mf-rows' ).on( 'click', this.remove_woocommerce_tax_rates_mf_rows );

	}

	/**
	 * Logging test
	 */
	logging_test( event ) {
		event.preventDefault();

		const _this   = this;
		const $button = jQuery( event.currentTarget );
		$button.prop( 'disabled', true );

		NMS_AJAX.post(
			northmill_online_shared_admin_status.log_test_ajax,
			{
				level_name: $button.data( 'level' ),
			},
			_this.log_test_success,
			_this.log_test_error,
			$button
		);
	}

	log_test_success( response, $button ) {
		alert( 'Log test successful!' );
		$button.prop( 'disabled', false );
	}

	log_test_error( response, $button ) {
		alert( 'Logging failed, read console.' );
		$button.prop( 'disabled', false );
	}

	/**
	 * Edit latest sync time
	 */
	click_latest_sync_time( event ) {
		const $target = jQuery( event.currentTarget );

		if ( ! $target.hasClass( 'editing' ) ) {
			$target.addClass( 'editing' );
			const latest_sync = $target.text();
			const syncer      = $target.data( 'syncer' );
			const $input      = jQuery( '<input type="text" />' );
			$input.val( latest_sync );
			$input.data( 'syncer', syncer );
			$input.data( 'original', latest_sync );
			$target.html( $input );
			$input.focus();
		}
	}

	change_latest_sync_time( event ) {
		const _this  = this;
		const $input = jQuery( event.currentTarget );

		if ( 13 === event.keyCode ) {
			const syncer      = $input.data( 'syncer' );
			const latest_sync = $input.val();
			$input.prop( 'disabled', true );

			NMS_AJAX.post(
				northmill_online_shared_admin_status.update_last_synced_ajax,
				{
					latest_sync: latest_sync,
					syncer: syncer,
				},
				_this.change_last_sync_time_success,
				_this.change_last_sync_time_error,
				{
					input: $input,
					latest_sync: latest_sync,
				}
			);

		} else if ( 27 === event.keyCode ) {
			$input.closest( '.nmf-latest-sync' ).removeClass( 'editing' );
			$input.closest( '.nmf-latest-sync' ).text( $input.data( 'original' ) );
		}
	}

	change_last_sync_time_success( response, callback_data ) {
		callback_data['input'].closest( '.nmf-latest-sync' ).removeClass( 'editing' );
		callback_data['input'].closest( '.nmf-latest-sync' ).text( callback_data['latest_sync'] );
	}

	change_last_sync_time_error( response, callback_data ) {
		alert( 'Error updating latest sync time.' );
		callback_data['input'].prop( 'disabled', false );
	}

	/**
	 * Remove cron lock
	 */
	remove_cron_lock( event ) {
		event.preventDefault();

		const _this = this;

		const $link = jQuery( event.currentTarget );
		$link.hide();

		NMS_AJAX.post(
			northmill_online_shared_admin_status.remove_cron_lock_ajax,
			{
				lock_name: $link.data( 'lock-name' ),
			},
			_this.remove_cron_lock_success,
			_this.remove_cron_lock_error,
			$link
		);
	}

	remove_cron_lock_success( response, $link ) {
		$link.closest( 'tr' ).remove();
	}

	remove_cron_lock_error( response, $link ) {
		console.log( response );
		alert( 'Error message: ' + response.responseJSON.data );
		$link.show();
	}

	/**
	 * Remove transient lock
	 */
	remove_update_transient( event ) {
		event.preventDefault();

		const _this = this;

		const $btn = jQuery( event.currentTarget );
		$btn.hide();

		NMS_AJAX.post(
			northmill_online_shared_admin_status.remove_update_transient_ajax,
			{
				transient_key: $btn.data( 'transient-name' ),
			},
			_this.remove_update_transient_success,
			_this.remove_update_transient_error,
			$btn
		);
	}

	remove_update_transient_success( response, $btn ) {
		alert( 'Deleted!' );
		location.reload();
	}

	remove_update_transient_error( response, $btn ) {
		alert( 'Error message: ' + response.responseJSON.data );
		$btn.show();
	}

	/**
	 * Empty table woocommerce_tax_rates
	 */
	empty_wc_tax_rates_table( event ) {
		event.preventDefault();

		// Confirm action.
		if ( ! confirm( northmill_online_shared_admin_status.l10n.confirm ) ) {
			return;
		}

		const $btn = jQuery( event.currentTarget );
		$btn.hide();

		NMS_AJAX.post(
			northmill_online_shared_admin_status.empty_wc_tax_rates_table_ajax,
			{},
			NMS_AJAX.success_reload,
			NMS_AJAX.fail_alert
		);
	}

	/**
	 * Remove woocommerce_tax_rates table rows where column tax_rate_class string contains 'mf-'
	 */
	remove_woocommerce_tax_rates_mf_rows( event ) {
		event.preventDefault();

		// Confirm action.
		if ( ! confirm( northmill_online_shared_admin_status.l10n.confirm ) ) {
			return;
		}

		const $btn = jQuery( event.currentTarget );
		$btn.hide();

		NMS_AJAX.post(
			northmill_online_shared_admin_status.remove_wc_tax_rates_mf_rows,
			{},
			NMS_AJAX.success_reload,
			NMS_AJAX.fail_alert
		);
	}
}
