window.W2PShopProofing = new function () {
	// Elements
	this.proofing_overlay_element              = '#w2pshop-overlay';
	this.proofing_container_element            = '#w2pshop-udraw-proof-container';
	this.proofing_approve_button_element       = '#w2pshop_approve_button';
	this.proofing_back_button_element          = '.w2pshop-proof-back-button';
	this.proofing_approve_checkbox_element     = '#w2pshop_proof_approval';
	this.proofing_preview_container_element    = '#w2pshop-preview-container';
	this.proofing_carousel_buttons_element     = '#w2pshop-carousel-buttons';
	this.proofing_carousel_preview_element	   = '.w2pshop-preview-image';
	this.proofing_carousel_next_button_element = '.proofing-next-button';
	this.proofing_carousel_prev_button_element = '.proofing-prev-button';
	this.proofing_carousel_dot_element         = '.proofing-dot';

	// Properties
	this.proofing_initiator = 'default';

	// Force the UI into non-PDF mode (hide PDF Editor buttons, show standard buttons)
	this.set_non_pdf_mode = function () {
		let proofing_container = jQuery( this.proofing_container_element );
		let pdfeditor_buttons  = jQuery('.w2pshop-pdfeditor-buttons');
		let non_pdf_buttons    = jQuery('.w2pshop-button-container').filter(function () {
			return !jQuery(this).hasClass('w2pshop-pdfeditor-buttons') && !jQuery(this).hasClass('w2pshop-top-bar');
		});

		proofing_container.removeClass('w2pshop-proofing-pdfeditor');
		pdfeditor_buttons.hide();
		non_pdf_buttons.show().css('display', 'flex');
	};

	this.display_proofing_window = function (initiator, allow_download) {
		let proofing_container = jQuery( this.proofing_container_element );
		proofing_container.show().css('pointer-events', 'auto').removeClass('w2pshop-proofing-pdfeditor');
		jQuery( this.proofing_overlay_element ).show().css('pointer-events', 'auto');
		let pdfeditor_buttons = jQuery('.w2pshop-pdfeditor-buttons');
		let non_pdf_buttons   = jQuery('.w2pshop-button-container').filter(function () {
			return !jQuery(this).hasClass('w2pshop-pdfeditor-buttons') && !jQuery(this).hasClass('w2pshop-top-bar');
		});

		// Reset button visibility first to avoid stale state when switching initiators
		pdfeditor_buttons.hide();
		non_pdf_buttons.hide();

		// Determine active initiator (explicit argument takes precedence)
		let current_initiator = (typeof initiator !== 'undefined') ? initiator : 'default';
		this.proofing_initiator = current_initiator;
		
		// Only treat as PDF editor when explicitly invoked as such.
		var is_pdfeditor = current_initiator === 'pdfeditor';
		var is_gosendex  = current_initiator === 'gosendex';
		var is_udraw     = current_initiator === 'udraw';
		
		if (is_pdfeditor) {
			proofing_container.addClass('w2pshop-proofing-pdfeditor');
			pdfeditor_buttons.show().css('display', 'flex');
			
			if (typeof allow_download !== 'undefined') {
				if (allow_download === false || allow_download === 'false') {
					jQuery('#w2pshop_download_proof_button').hide();
				} else {
					jQuery('#w2pshop_download_proof_button').show();
				}
			} else {
				jQuery('#w2pshop_download_proof_button').show();
			}
		} else if (is_gosendex || is_udraw || current_initiator === 'default') {
			// Explicit non-PDF flows: ensure PDF controls never render.
			this.set_non_pdf_mode();
		} else {
			this.set_non_pdf_mode();
		}
	};

	this.hide_proofing_window = function () {
		jQuery( this.proofing_container_element ).hide();
		jQuery( this.proofing_overlay_element ).hide();
	};

	this.proofing_back_button_event = function () {
		this.hide_proofing_window();
	};

	this.set_approve_button_state = function () {
		let is_approved_checked = jQuery( W2PShopProofing.proofing_approve_checkbox_element ).is( ':checked' );
		jQuery( W2PShopProofing.proofing_approve_button_element ).prop( 'disabled', ! is_approved_checked );
		jQuery( '#w2pshop_approve_button_pdfeditor' ).prop( 'disabled', ! is_approved_checked );
	};

	this.setup_carousel = function () {
		let imageCount = jQuery( this.proofing_preview_container_element + " img" ).length;

		// Hide Carousel Buttons if there are only 1 image to display.
		if (imageCount >= 2) {
			jQuery( this.proofing_carousel_buttons_element ).show();
		}

		let prev_button             = jQuery( this.proofing_carousel_prev_button_element );
		let next_button             = jQuery( this.proofing_carousel_next_button_element );
		let slide_numbers_container = document.querySelector( '.slide-numbers' );
		let dots                    = slide_numbers_container.querySelectorAll( '.dot' );
		let preview_images          = jQuery( this.proofing_carousel_preview_element );
		let total_images            = preview_images.length;
		let image_position          = 0;

		// Event Listeners
		next_button.on( 'click', next_img );
		prev_button.on( 'click', prev_img );

		// Update Position
		function update_position() {
			// Images
			for (let image of preview_images) {
				image.classList.remove( 'visible' );
				image.classList.add( 'hidden' );
			}
			preview_images[image_position].classList.remove( 'hidden' );
			preview_images[image_position].classList.add( 'visible' );

			// Dots
			for (let dot of dots) {
				dot.className = dot.className.replace( " active", "" );
			}
			dots[ image_position ].classList.add( 'active' );
		}

		// Next Img
		function next_img() {
			if (image_position === total_images - 1) {
				image_position = 0;
			} else {
				image_position++;
			}
			update_position();
		}

		// Previous Image
		function prev_img() {
			if (image_position === 0) {
				image_position = total_images - 1;
			} else {
				image_position--;
			}
			update_position();
		}

		// Dot Position
		dots.forEach(
			function ( dot, dot_position ) {
				dot.addEventListener(
					"click",
					function () {
						image_position = dot_position;
						update_position();
					}
				);
			}
		);
	};

	this.set_preview_images = function ( images, contains_pdf) {
		contains_pdf          = typeof contains_pdf !== 'undefined' ? contains_pdf : false;
		let preview_container = document.getElementById( this.proofing_preview_container_element.replace( '#' ,  '' ) );

		if ( ! preview_container ) {
			return; } // Preview Containers does not exist.

		//clear existing images from preview container
		while ( preview_container.firstChild ) {
			preview_container.removeChild( preview_container.firstChild );
		}

		// Check in incoming images is a PDF or not.
		if ( contains_pdf ) {

			for (let j = 0; j < images.length; j++) {
				let preview_iframe       = document.createElement( "iframe" );
				preview_iframe.src       = images[j] + "#toolbar=0";
				preview_iframe.className = "w2pshop-preview-image";
				preview_iframe.alt       = "Proofing Preview";
				jQuery( this.proofing_preview_container_element ).append( preview_iframe );
			}

		} else {

			// clear existing dots from slide-numbers container except the first one
			let slide_numbers_container = document.querySelector( '.slide-numbers' );
			let dots                    = slide_numbers_container.querySelectorAll( '.dot' );
			for (let i = dots.length - 1; i > 0; i--) {
				slide_numbers_container.removeChild( dots[i] );
			}

			// add dot spans for image carousel, starting from the second image
			for (let i = 1; i < images.length; i++) {
				let dot_span = document.createElement( 'span' );
				dot_span.classList.add( 'dot' );
				slide_numbers_container.appendChild( dot_span );
			}

			for (let i = 0; i < images.length; i++) {
				//push the images to screen, only first image is visible
				let preview_string = images[i];
				let preview_img    = document.createElement( 'img' );
				preview_img.src    = preview_string;
				preview_img.classList.add( 'w2pshop-preview-image' );
				if (i === 0) {
					preview_img.classList.add( 'visible' );
				} else {
					preview_img.classList.add( 'hidden' );
				}
				preview_container.appendChild( preview_img );
			}

			// Once completed, we will set up the carousel which binds UI events.
			this.setup_carousel();
		}
	};

	this.register_events = function () {
		// Register Proofing Events
		jQuery( this.proofing_back_button_element ).on(
			'click',
			function () {
				W2PShopProofing.proofing_back_button_event();
			}
		);

		jQuery( this.proofing_approve_button_element ).on(
			'click',
			function () {
				let checkbox_element = jQuery( W2PShopProofing.proofing_approve_checkbox_element );
				if (checkbox_element.length === 0 || (checkbox_element && checkbox_element.is( ':checked' ))) {
					W2PShopProofing.hide_proofing_window();
					// Trigger jQuery Event to notify listeners that proof was accepted.
					jQuery( document ).trigger( 'w2pshop-proof-accepted' );
				} else if (checkbox_element && ! checkbox_element.is( ':checked' )) {
					W2PShopCommon.display_error_message( 'Oops...', 'Please click the checkbox to proceed.' );
					W2PShopProofing.set_approve_button_state();
				}
			}
		);

		// PDF Editor specific approve button
		jQuery( document ).on(
			'click',
			'#w2pshop_approve_button_pdfeditor',
			function () {
				let checkbox_element = jQuery( W2PShopProofing.proofing_approve_checkbox_element );
				if (checkbox_element.length === 0 || (checkbox_element && checkbox_element.is( ':checked' ))) {
					W2PShopProofing.hide_proofing_window();
					// Trigger jQuery Event to notify listeners that proof was accepted.
					jQuery( document ).trigger( 'w2pshop-proof-accepted' );
				} else if (checkbox_element && ! checkbox_element.is( ':checked' )) {
					W2PShopCommon.display_error_message( 'Oops...', 'Please click the checkbox to proceed.' );
					W2PShopProofing.set_approve_button_state();
				}
			}
		);

		if (this.proofing_approve_checkbox_element) {
			jQuery( this.proofing_approve_checkbox_element ).on(
				'change',
				function () {
					W2PShopProofing.set_approve_button_state();
				}
			)
		}
	};
}

jQuery( document ).ready(
	function () {
		W2PShopProofing.register_events(); }
);