/* global wphbGlobal */

import HBDeactivationSurvey from './hb-deactivation-survey';

( function() {
	'use strict';

	const WPHBGlobal = {
		init() {
			this.registerUpsellClick();
			this.registerClearAllCache();
			this.registerClearNetworkCache();
			this.registerClearCacheFromNotice();
			this.registerClearCloudflare();
			this.registerSwitchCriticalCSS();
			this.registerDeactivationSurvey();
			this.registerCopyText();
			this.registerPublishSafeModeChanges();
		},

		/**
		 * Register copy text button.
		 *
		 * @since 3.18.0
		 */
		registerCopyText() {
			const btn = document.getElementById(
				'wphb-copy-test-link'
			);

			if ( ! btn ) {
				return;
			}

			btn.addEventListener( 'click', () => {
				navigator.clipboard.writeText( wphbGlobal.previewLink ).then( () => {
					const originalText = btn.innerText;
					const copiedText = wphbGlobal.copyText;

					btn.innerText = copiedText;
					btn.disabled = true;

					setTimeout( () => {
						btn.innerText = originalText;
						btn.disabled = false;
					}, 2000 );
				} );
			} );
		},

		/**
		 * Register publish safe mode changes button.
		 *
		 * @since 3.18.0
		 */
		registerPublishSafeModeChanges() {
			const btn = document.getElementById(
				'wphb-publish-safe-mode-changes'
			);

			if ( btn ) {
				btn.addEventListener( 'click', () => {
					btn.disabled = true;
					btn.textContent = wphbGlobal.publishing;
					jQuery
						.ajax( {
							url: wphbGlobal.ajaxurl,
							method: 'POST',
							data: {
								nonce: wphbGlobal.nonce,
								action: 'wphb_publish_safe_mode',
							},
						} )
						.done( function() {
							// Redirect to a response page
							window.location.href = wphbGlobal.hb_dashboard_url + '&wphb_safemode_published=1';
						} );
				} );
			}

			const dismissBtn = document.querySelector( '.safe-mode-dashboard-notice .wphb-dismiss' );
			if ( dismissBtn ) {
				dismissBtn.addEventListener( 'click', function( e ) {
					e.preventDefault();
					const parent = dismissBtn.closest( '.safe-mode-dashboard-notice' );
					if ( parent ) {
						parent.style.display = 'none';
					}
				} );
			}
		},

		/**
		 * Track deactivation survey.
		 *
		 * @since 3.10.0
		 */
		registerDeactivationSurvey() {
			const deactivatePluginLink = document.querySelector( 'a[id^="deactivate-hummingbird-pro"]' ) || document.querySelector( 'a[id^="deactivate-hummingbird-performance"]' );
			if ( ! deactivatePluginLink ) {
				return;
			}

			// Deactivation survey modal.
			( new HBDeactivationSurvey() ).init();
		},

		/**
		 * Switch to critical CSS from legacy.
		 *
		 * @since 3.9.3
		 */
		registerSwitchCriticalCSS() {
			const btn = document.getElementById(
				'wphb-switch-critical-from-legacy'
			);

			if ( ! btn ) {
				return;
			}

			btn.addEventListener( 'click', () =>
				jQuery
					.ajax( {
						url: wphbGlobal.ajaxurl,
						method: 'POST',
						data: {
							nonce: wphbGlobal.nonce,
							action: 'wphb_switch_to_critical_css_from_legacy',
						},
					} )
					.done( function() {
						// Redirect to a response page
						window.location.href = wphbGlobal.minify_url + '&view=tools';
					} )
			);
		},

		/**
		 * Clear selected module from admin bar.
		 *
		 * @since 3.0.1
		 *
		 * @param {string} module Module ID.
		 */
		clearCache( module ) {
			jQuery
				.ajax( {
					url: wphbGlobal.ajaxurl,
					method: 'POST',
					data: {
						nonce: wphbGlobal.nonce,
						action: 'wphb_clear_caches',
						modules: [ module ],
					},
				} )
				.done( function() {
					location.reload();
				} );
		},

		/**
		 * Clear all cache from admin bar.
		 *
		 * @since 3.0.1
		 */
		registerClearAllCache() {
			const btn = document.getElementById(
				'wp-admin-bar-wphb-clear-all-cache'
			);

			if ( ! btn ) {
				return;
			}

			btn.addEventListener( 'click', function() {
				const action = 'wphb_global_clear_cache';
				this.post( action + '&all_clear=1' );
			}.bind( this )
			);
		},

		/**
		 * Track upsell menu click.
		 *
		 * @since 3.9.0
		 */
		registerUpsellClick() {
			const upsellSubmenuLink = document.querySelector( '#toplevel_page_wphb a[href^="https://wpmudev.com"]' );

			if ( ! upsellSubmenuLink ) {
				return;
			}

			if ( typeof wphb === 'undefined' || ! wphb.mixpanel.enabled ) {
				return;
			}

			if ( ! wphbGlobal.is_hb_page ) {
				require( './mixpanel' );
			}

			upsellSubmenuLink.addEventListener( 'click', ( event ) => {
				window.wphbMixPanel.trackHBUpsell( 'pro_general', 'submenu', 'cta_clicked', event.currentTarget.href, 'hb_pro_upsell' );
			} );
		},

		/**
		 * Clear network cache.
		 */
		registerClearNetworkCache() {
			const btn = document.querySelector(
				'#wp-admin-bar-wphb-clear-cache-network-wide > a'
			);

			if ( ! btn ) {
				return;
			}

			btn.addEventListener( 'click', () => {
				if ( 'undefined' === typeof window.WPHB_Admin ) {
					window.location.href =
						'/wp-admin/network/admin.php?page=wphb-caching&update=open-ccnw';
					return;
				}

				window.SUI.openModal(
					'ccnw-modal',
					'wpbody',
					'ccnw-clear-now'
				);
			} );
		},

		/**
		 * Clear cache from notice regarding plugin/theme updates.
		 */
		registerClearCacheFromNotice() {
			const btn = document.getElementById(
				'wp-admin-notice-wphb-clear-cache'
			);

			if ( ! btn ) {
				return;
			}

			btn.addEventListener( 'click', () =>
				this.post( 'wphb_global_clear_cache' + '&all_clear=1' )
			);
		},

		/**
		 * Clear Cloudflare browser cache.
		 *
		 * @since 2.7.2
		 */
		registerClearCloudflare() {
			const btn = document.querySelector(
				'#wp-admin-bar-wphb-clear-cloudflare > a'
			);

			if ( ! btn ) {
				return;
			}

			btn.addEventListener( 'click', () =>
				this.post( 'wphb_front_clear_cloudflare' )
			);
		},

		/**
		 * Send AJAX request.
		 *
		 * @param {string}          action
		 * @param {boolean}         reload
		 * @param {Function | null} callback
		 */
		post( action, reload = true, callback = null ) {
			this.request( action )
				.then( ( response ) => {
					if ( callback && typeof callback === 'function' ) {
						callback( response );
					}

					if ( reload ) {
						location.reload( action );
					}
				} );
		},

		request( action ) {
			return new Promise( ( resolve ) => {
				const xhr = new XMLHttpRequest();
				xhr.open( 'POST', wphbGlobal.ajaxurl + '?action=' + action + '&_ajax_nonce=' + wphbGlobal.nonce );
				xhr.onload = function() {
					if ( xhr.status === 200 ) {
						resolve();
					}
				};

				xhr.send();
			} );
		}
	};

	document.addEventListener( 'DOMContentLoaded', function() {
		WPHBGlobal.init();
	} );

	window.WPHBGlobal = WPHBGlobal;
}() );
