(function ($) {
	'use strict';

	var state = {
		deactivationUrl: '',
		isSubmitting: false
	};

	function getVars() {
		return window.momcsDeactivationFeedback || {};
	}

	function isValidEmail(email) {
		if ( ! email ) {
			return true;
		}
		var re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
		return re.test( String( email ).toLowerCase() );
	}

	function openModal() {
		$( '#momcs-deactivation-feedback-modal' ).attr( 'aria-hidden', 'false' ).addClass( 'active' );
	}

	function closeModal() {
		$( '#momcs-deactivation-feedback-modal' ).attr( 'aria-hidden', 'true' ).removeClass( 'active' );
	}

	function showStep(step) {
		if ( step === 'form' ) {
			$( '#momcs-deactivation-feedback-step-initial' ).hide();
			$( '#momcs-deactivation-feedback-step-form' ).show();
			$( '#momcs_deactivation_details' ).trigger( 'focus' );
			return;
		}
		$( '#momcs-deactivation-feedback-step-form' ).hide();
		$( '#momcs-deactivation-feedback-step-initial' ).show();
	}

	function showError(message) {
		$( '#momcs-deactivation-feedback-success' ).hide();
		$( '#momcs-deactivation-feedback-error' ).text( message || getString( 'genericError', 'Something went wrong.' ) ).show();
	}

	function showSuccess(message) {
		$( '#momcs-deactivation-feedback-error' ).hide();
		$( '#momcs-deactivation-feedback-success' ).text( message || getString( 'successMessage', 'Thanks for the feedback.' ) ).show();
	}

	function toggleLoading(isLoading) {
		if ( isLoading ) {
			$( '#momcs-deactivation-feedback-loading' ).show();
			$( '#momcs-deactivation-feedback-submit' ).prop( 'disabled', true );
			$( '#momcs-deactivation-feedback-skip' ).prop( 'disabled', true );
		} else {
			$( '#momcs-deactivation-feedback-loading' ).hide();
			$( '#momcs-deactivation-feedback-submit' ).prop( 'disabled', false );
			$( '#momcs-deactivation-feedback-skip' ).prop( 'disabled', false );
		}
	}

	function resetForm() {
		state.isSubmitting = false;
		toggleLoading( false );
		$( '#momcs-deactivation-feedback-error' ).hide().text( '' );
		$( '#momcs-deactivation-feedback-success' ).hide().text( '' );
		$( '#momcs_deactivation_details' ).val( '' );
		$( 'input[name="momcs_deactivation_reason"]' ).prop( 'checked', false );
	}

	function getReason() {
		return $( 'input[name="momcs_deactivation_reason"]:checked' ).val() || '';
	}

	function proceedWithDeactivation() {
		if ( state.deactivationUrl ) {
			window.location.href = state.deactivationUrl;
			return;
		}
		closeModal();
	}

	function submitFeedback() {
		var vars = getVars();
		var reason = getReason();
		var details = $( '#momcs_deactivation_details' ).val();
		var email = $( '#momcs_deactivation_email' ).val();

		if ( ! reason ) {
			showError( getString( 'selectReason', 'Please select a reason.' ) );
			return;
		}
		if ( ! isValidEmail( email ) ) {
			showError( getString( 'invalidEmail', 'Please enter a valid email.' ) );
			return;
		}
		if ( ! vars.ajaxUrl || ! vars.action || ! vars.nonce ) {
			showError( getString( 'genericError', 'Something went wrong.' ) );
			return;
		}
		if ( state.isSubmitting ) {
			return;
		}

		state.isSubmitting = true;
		toggleLoading( true );

		$.ajax(
			{
				url: vars.ajaxUrl,
				method: 'POST',
				dataType: 'json',
				data: {
					action: vars.action,
					nonce: vars.nonce,
					reason: reason,
					details: details || '',
					email: email || ''
				}
			}
		).done(
			function (response) {
				state.isSubmitting = false;
				toggleLoading( false );

				if ( response && response.success ) {
					var message = response.data && response.data.message ? response.data.message : getString( 'successMessage', 'Thanks for the feedback.' );
					showSuccess( message );
					setTimeout( proceedWithDeactivation, 1200 );
					return;
				}

				var errorMessage = response && response.data && response.data.message ? response.data.message : getString( 'genericError', 'Something went wrong.' );
				showError( errorMessage );
			}
		).fail(
			function () {
				state.isSubmitting = false;
				toggleLoading( false );
				showError( getString( 'genericError', 'Something went wrong.' ) );
			}
		);
	}

	function getString(key, fallback) {
		var vars = getVars();
		if ( vars.strings && vars.strings[key] ) {
			return vars.strings[key];
		}
		return fallback;
	}

	function handleDeactivationClick(e) {
		var vars = getVars();
		if ( vars.hasSubmitted ) {
			return;
		}

		e.preventDefault();
		state.deactivationUrl = $( this ).attr( 'href' ) || '';
		resetForm();
		showStep( 'initial' );
		openModal();
	}

	function bindEvents() {
		var vars = getVars();
		if ( vars.hasSubmitted ) {
			return;
		}
		if ( ! vars.pluginBasename || $( '#momcs-deactivation-feedback-modal' ).length === 0 ) {
			return;
		}

		var selector = 'tr[data-plugin="' + vars.pluginBasename + '"] .deactivate a';
		$( document ).on( 'click', selector, handleDeactivationClick );

		$( document ).on(
			'click',
			'#momcs-deactivation-feedback-close',
			function (e) {
				e.preventDefault();
				closeModal();
			}
		);

		$( document ).on(
			'click',
			'#momcs-deactivation-feedback-stay',
			function (e) {
				e.preventDefault();
				closeModal();
			}
		);

		$( document ).on(
			'click',
			'#momcs-deactivation-feedback-continue',
			function (e) {
				e.preventDefault();
				showStep( 'form' );
			}
		);

		$( document ).on(
			'click',
			'#momcs-deactivation-feedback-back',
			function (e) {
				e.preventDefault();
				showStep( 'initial' );
			}
		);

		$( document ).on(
			'click',
			'#momcs-deactivation-feedback-skip',
			function (e) {
				e.preventDefault();
				proceedWithDeactivation();
			}
		);

		$( document ).on(
			'submit',
			'#momcs-deactivation-feedback-form',
			function (e) {
				e.preventDefault();
				submitFeedback();
			}
		);

		$( document ).on(
			'click',
			'#momcs-deactivation-feedback-modal',
			function (e) {
				if ( e.target && e.target.id === 'momcs-deactivation-feedback-modal' ) {
					closeModal();
				}
			}
		);
	}

	$( document ).ready( function () {
		bindEvents();
	} );
})( jQuery );

