(function ($) {
	'use strict';

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

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

	function openModal() {
		$( '#momcs-contact-us-modal' ).attr( 'aria-hidden', 'false' ).addClass( 'active' );
		$( '#momcs_contact_email' ).trigger( 'focus' );
	}

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

	function showError(message) {
		$( '#momcs-contact-us-success' ).hide();
		$( '#momcs-contact-us-error' ).text( message || 'Something went wrong.' ).show();
		setTimeout(
			function () {
				$( '#momcs-contact-us-error' ).fadeOut( 200 ); },
			3000
		);
	}

	function showSuccess(message) {
		$( '#momcs-contact-us-error' ).hide();
		$( '#momcs-contact-us-success' ).text( message || 'Your message has been sent.' ).show();
		setTimeout(
			function () {
				closeModal(); $( '#momcs-contact-us-success' ).hide(); },
			4000
		);
	}

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

	function buildPayload(email, query) {
		var vars   = getVars();
		var prefix = '[miniOrange ' + (vars.plugin_name || 'Plugin') + '] ' + (vars.version || '') + ' - ';
		return {
			email: email,
			query: prefix + query,
			ccEmail: vars.support_email || '',
			company: vars.site_url || '',
			firstName: vars.first_name || '',
			lastName: vars.last_name || ''
		};
	}

	function submitContactUs() {
		var vars  = getVars();
		var email = $( '#momcs_contact_email' ).val();
		var query = $( '#momcs_contact_query' ).val();

		if ( ! isValidEmail( email )) {
			showError( 'Please enter a valid email.' );
			return;
		}
		if ( ! query || ! query.trim()) {
			showError( 'Please enter your message.' );
			return;
		}

		var url = vars.contact_us_url;
		if ( ! url) {
			showError( 'Contact endpoint is not configured.' );
			return;
		}

		var payload = buildPayload( email, query.trim() );
		toggleLoading( true );

		$.ajax(
			{
				url: url,
				method: 'POST',
				data: JSON.stringify( payload ),
				dataType: 'text',
				crossDomain: true,
				contentType: 'application/json; charset=utf-8'
			}
		).done(
			function () {
				toggleLoading( false );
				// Clear form
				$( '#momcs_contact_email' ).val( '' );
				$( '#momcs_contact_query' ).val( '' );
				showSuccess( 'Thanks! We will get back to you shortly.' );
			}
		).fail(
			function (jqXHR) {
				toggleLoading( false );
				var msg = (jqXHR && jqXHR.responseText) ? jqXHR.responseText : 'Failed to send message.';
				showError( msg );
			}
		);
	}

	$( document ).on(
		'click',
		'#momcs-contact-us-button',
		function (e) {
			e.preventDefault();
			openModal();
		}
	);

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

	$( document ).on(
		'click',
		'#momcs-contact-us-submit',
		function (e) {
			e.preventDefault();
			submitContactUs();
		}
	);

	// close on overlay click
	$( document ).on(
		'click',
		'#momcs-contact-us-modal',
		function (e) {
			if (e.target && e.target.id === 'momcs-contact-us-modal') {
				closeModal();
			}
		}
	);
})( jQuery );
