(function ( $ ) {
	'use strict';

	//Hide error message after 5 sec
	setTimeout(
		function () {
			$( '.verfacto-error-message' ).fadeOut( 'fast' );
		},
		5000
	);

	//Handle Sign Up/Login style/behavior
	$( '.verfacto-form' ).find( 'input' ).on(
		'keyup blur focus',
		function (e) {

			var $this = $( this ),
			label     = $this.prev( 'label' );

			if (e.type === 'keyup') {
				if ($this.val() === '') {
					label.removeClass( 'active highlight' );
				} else {
					label.addClass( 'active highlight' );
				}
			} else if (e.type === 'blur') {
				if ($this.val() === '' ) {
					label.removeClass( 'active highlight' );
				} else {
					label.removeClass( 'highlight' );
				}
			} else if (e.type === 'focus') {

				if ($this.val() === '' ) {
						label.removeClass( 'highlight' );
				} else if ($this.val() !== '' ) {
					label.addClass( 'highlight' );
				}
			}

		}
	);

	$( '.verfacto-tab a' ).on(
		'click',
		function (e) {

			e.preventDefault();

			$( this ).parent().addClass( 'active' );
			$( this ).parent().siblings().removeClass( 'active' );

			let target = $( this ).attr( 'href' );

			$( '.verfacto-tab-content > div' ).not( target ).hide();

			$( target ).fadeIn( 600 );

		}
	);

	//Handle Sign Up/Login form submits

	$( '#verfacto-login-form, #verfacto-signup-form' ).submit(
		function (e) {
			e.preventDefault();
			$( '.verfacto-loader' ).css( "visibility", "visible" );

			let form      = $( this );
			let form_data = new FormData( form[0] );
			form_data.append( "action", "activate_verfacto" );
			form_data.append( "vf_nonce", verfacto_ajax.vf_nonce );

			if (form_data.get( 'auth_method' ) === 'sign-up' && form_data.get( 'user_password' ) != form_data.get( 'user_password_confirm' )) {
				show_error_message( 'Password does not match' );
				$( '.verfacto-loader' ).css( "visibility", "hidden" );
				return;
			}

			$.ajax(
				{
					type: "POST",
					url: verfacto_ajax.admin_ajax,
					contentType: false,
					processData: false,
					dataType: 'JSON',
					data: form_data,
					cache: false,
					success: function (data) {
						form.trigger( 'reset' );
						$( '.verfacto-loader' ).css( "visibility", "hidden" );
						$( '.verfacto-error-message.verfacto-error-danger.ajax' ).hide();
						window.open( data.url, data.open_in );
					}
				}
			).fail(
				function (jqXHR) {
					if (jqXHR.responseText === 'internal-server-error') {
						show_error_message( "Something unexpected happened please try again" );
						$( '.verfacto-loader' ).css( "visibility", "hidden" );
						return;
					}

					if ( ! jqXHR.responseJSON.success && jqXHR.responseJSON.data !== '') {
						show_error_message( jqXHR.responseJSON.data );
						$( '.verfacto-loader' ).css( "visibility", "hidden" );
					} else {
						show_error_message( 'Some issue with api service please try again later' )
						$( '.verfacto-loader' ).css( "visibility", "hidden" );
					}
				}
			);

		}
	);

	function show_error_message(message_text) {
		let error_block = $( '.verfacto-error-message.verfacto-error-danger.ajax' ).show();
		error_block.find( '.verfacto-error-text' ).html( message_text );
	}

})( jQuery );
