/**
 * Script for settings syncing in WP-admin.
 *
 * @package northmill-online-shared
 */

/* global northmill_online_shared_admin_sync_settings */

let NMS_Sync_Settings_Instance;
jQuery( document ).ready(
	function () {
		NMS_Sync_Settings_Instance = new NMS_Sync_Settings();
	}
);

class NMS_Sync_Settings {
	constructor() {
		this.enable_sync  = this.enable_sync.bind( this );
		this.disable_sync = this.disable_sync.bind( this );
		this.sync         = this.sync.bind( this );

		this.sync_done    = this.sync_done.bind( this );
		this.sync_updated = this.sync_updated.bind( this );
		this.ajax_failed  = this.ajax_failed.bind( this );

		this.$btn_enable_sync  = jQuery( '#nms-enable-sync' );
		this.$btn_disable_sync = jQuery( '#nms-disable-sync' );
		this.$btn_sync         = jQuery( '#nms-sync-now' );
		this.$log 			   = jQuery( '#nms-sync-log' );
		this.$sync_status 	   = jQuery( '#nms-sync-status' );
		this.$data_preview     = jQuery( '#data-preview' );

		this.$btn_enable_sync.on( 'click', this.enable_sync );
		this.$btn_disable_sync.on( 'click', this.disable_sync );
		this.$btn_sync.on( 'click', this.sync );

		this.is_sync_enabled = northmill_online_shared_admin_sync_settings.is_enabled;

		this.check_sync_status();
	}

	check_sync_status() {
		if ( this.is_sync_enabled ) {
			this.$btn_enable_sync.hide();
			this.$btn_disable_sync.show();
		} else {
			this.$btn_enable_sync.show();
			this.$btn_disable_sync.hide();
		}
	}

	enable_sync() {
		this.disable_buttons();
		this.clear_log();
		this.log( 'start', 'enable' );
		NMS_AJAX.post(
			northmill_online_shared_admin_sync_settings.ajax_actions.enable_sync,
			{},
			this.sync_updated,
			this.ajax_failed,
			'enable'
		);
	}

	disable_sync() {
		this.disable_buttons();
		this.clear_log();
		this.log( 'start', 'disable' );
		NMS_AJAX.post(
			northmill_online_shared_admin_sync_settings.ajax_actions.disable_sync,
			{},
			this.sync_updated,
			this.ajax_failed,
			'disable'
		);
	}

	sync_updated( response, action ) {
		console.debug( response );
		this.is_sync_enabled = 'enable' === action;
		this.$sync_status.html( response.data.sync_status );
		this.check_sync_status();
		this.log( 'done', action );

		this.enable_buttons();
	}

	ajax_failed( response, action ) {
		console.debug( response );
		this.log( 'failed', action );
	}

	sync() {
		this.disable_buttons();
		this.clear_log();
		this.log( 'start', 'sync' );
		NMS_AJAX.post(
			northmill_online_shared_admin_sync_settings.ajax_actions.sync_now,
			{},
			this.sync_done,
			this.ajax_failed,
			'sync'
		);
	}

	sync_done( response ) {
		console.debug( response );
		this.$data_preview.html( response.data.data_preview_html );
		this.$sync_status.html( response.data.sync_status );
		this.log( 'done', 'sync' );

		this.enable_buttons();
	}

	disable_buttons() {
		this.$btn_enable_sync.prop( 'disabled', true );
		this.$btn_disable_sync.prop( 'disabled', true );
		this.$btn_sync.prop( 'disabled', true );
	}

	enable_buttons() {
		this.$btn_enable_sync.prop( 'disabled', false );
		this.$btn_disable_sync.prop( 'disabled', false );
		this.$btn_sync.prop( 'disabled', false );
	}

	log( status, action, type = 'info' ) {
		const date     = new Date();
		const datetime = date.getFullYear() + '-' + ('0' + (date.getMonth() + 1)).slice( -2 ) + '-' + ('0' + date.getDate()).slice( -2 )
				+ ' ' + ('0' + date.getHours()).slice( -2 ) + ':' + ('0' + date.getMinutes()).slice( -2 ) + ':' + ('0' + date.getSeconds()).slice( -2 );

		const status_text = northmill_online_shared_admin_sync_settings.l10n.status[ status ];
		const action_text = northmill_online_shared_admin_sync_settings.l10n.action[ action ];
		this.$log.append( '<div class="' + type + '">[' + datetime + '] ' + status_text + ': ' + action_text + '</div>' );
	}

	clear_log() {
		this.$log.html( '' );
	}
}
