(function( $ ) {
	'use strict';

	function copyAttendeeDetails( event ) {
		event.preventDefault();

		var $button = $( this );
		var $group  = $button.closest( '.fooevents-attendee-group' );

		if ( ! $group.length ) {
			return;
		}

		var $headings = $group.find( 'h3' );
		var $firstH3  = $headings.first();
		var $firstAttendee = $group.find('.fooevents-attendee-item').first();
		var attendee1 = $firstAttendee.find('input, select, textarea');


		$headings.each( function( index ) {
			if ( index === 0 ) {
				return;
			}

			var $container = $( this ).nextUntil( 'h3' );

			attendee1.each( function() {
				var $source   = $( this );
				var fieldName = $source.attr( 'name' );

				if ( ! fieldName ) {
					return;
				}

				var match = fieldName.match( /\[[^\]]+\]$/ );

				if ( ! match ) {
					return;
				}

				var suffix = match[0];
				var $target = $container.find( "[name$='" + suffix + "']" );
				var tagName = this.tagName.toLowerCase();
				var fieldType = $source.attr( 'type' );

				if ( $source.hasClass( 'fooevents-ignore-copy' ) || $target.hasClass( 'fooevents-ignore-copy' ) ) {
					return;
				}

				if ( tagName === 'input' ) {
					if ( fieldType === 'checkbox' || fieldType === 'radio' ) {
						$target.prop( 'checked', $source.prop( 'checked' ) );
					} else {
						$target.val( $source.val() );
					}
				} else {
					$target.val( $source.val() );
				}

				$target.trigger( 'change' );
			} );
		} );
	}

	function valueIsSet( value ) {
		return value !== undefined && value !== null && $.trim( value ).length > 0;
	}

	function isEmailField( $field ) {
		var name = $field.attr( 'name' ) || '';
		var type = ( $field.attr( 'type' ) || '' ).toLowerCase();

		return type === 'email' || name.indexOf( '[email]' ) !== -1;
	}

	function isValidEmail( value ) {
		return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test( value );
	}

	function fieldHasValue( $field, $attendee ) {
		if ( $field.is( ':checkbox' ) || $field.is( ':radio' ) ) {
			return $attendee.find( "[name='" + $field.attr( 'name' ) + "']:checked" ).length > 0;
		}

		var value = $field.val();

		if ( ! valueIsSet( value ) ) {
			return false;
		}

		return ! isEmailField( $field ) || isValidEmail( value );
	}

	function attendeeBookingsComplete( $attendee ) {
		var $dateSelect = $attendee.find( '.fooevents-bookings-date-select' );
		var $slotSelect = $attendee.find( '.fooevents-bookings-slot-select' );

		if ( $dateSelect.length || $slotSelect.length ) {
			return valueIsSet( $dateSelect.val() ) && valueIsSet( $slotSelect.val() );
		}

		var $dateHidden = $attendee.find( "input[name$='[booking][date_id]']" );
		var $slotHidden = $attendee.find( "input[name$='[booking][slot_id]']" );

		if ( $dateHidden.length || $slotHidden.length ) {
			return valueIsSet( $dateHidden.val() ) && valueIsSet( $slotHidden.val() );
		}

		return true;
	}

	function updateAttendeeStatus( $attendee ) {
		var $heading  = $attendee.children( 'h3' ).first();
		var $required = $attendee.find( 'input[required], select[required], textarea[required]' );
		var allValid  = true;

		$required.each( function() {
			if ( ! fieldHasValue( $( this ), $attendee ) ) {
				allValid = false;
				return false;
			}
		} );

		if ( allValid && ! attendeeBookingsComplete( $attendee ) ) {
			allValid = false;
		}

		var $status = $heading.find( '.fooevents-attendee-status' );

		if ( allValid && ( $required.length || $attendee.find( "input[name$='[booking][date_id]']" ).length ) ) {
			if ( ! $status.length ) {
				$status = $( '<span class="fooevents-attendee-status dashicons dashicons-yes-alt" aria-hidden="true"></span>' );

				if ( window.FooEventsAttendeeVars && FooEventsAttendeeVars.completeLabel ) {
					$status.attr( 'title', FooEventsAttendeeVars.completeLabel );
				}

				$heading.append( $status );
			}

			$heading.addClass( 'fooevents-attendee-complete' );
		} else {
			$heading.removeClass( 'fooevents-attendee-complete' );
			$status.remove();
		}
	}

	function refreshStatus( $element ) {
		var $attendee = $element.closest( '.fooevents-attendee-item' );

		if ( $attendee.length ) {
			updateAttendeeStatus( $attendee );
		}
	}

	$( document ).on(
		'click',
		'.copy-btn',
		copyAttendeeDetails
	);

	$( document ).on(
		'input change',
		'.fooevents-attendee-item input, .fooevents-attendee-item select, .fooevents-attendee-item textarea',
		function() {
			refreshStatus( $( this ) );
		}
	);

	$( function() {
		$( '.fooevents-attendee-item' ).each( function() {
			updateAttendeeStatus( $( this ) );
		} );
	} );
})( jQuery );
