jQuery(
	function(jQuery) {

		var file_frame,
		awl_image_gallery = {
			ul: '',
			init: function() {
				this.ul = jQuery( '.sbox' );
				this.ul.sortable(
					{
						placeholder: '',
						revert: true,
					}
				);

				/**
				 * Add Slide Callback Function - Optimized with batch processing
				 */
				jQuery( '#add-new-slider' ).on(
					'click',
					function(event) {
						event.preventDefault();
						if (file_frame) {
							file_frame.open();
							return;
						}
						file_frame = wp.media.frames.file_frame = wp.media(
							{
								multiple: true
							}
						);

						file_frame.on(
							'select',
							function() {
								var images = file_frame.state().get( 'selection' ).toJSON(),
									length = images.length;
								
								if (length === 0) return;
								
								// Collect all image IDs for batch processing
								var imageIds = [];
								for (var i = 0; i < length; i++) {
									imageIds.push(images[i]['id']);
								}
								
								// Use batch AJAX request for better performance
								awl_image_gallery.get_thumbnails_batch(imageIds);
							}
						);
						file_frame.open();
					}
				);

				/**
				 * Delete Slide Callback Function
				 */
				this.ul.on(
					'click',
					'#remove-slide',
					function() {
						if (confirm( 'Are sure to delete this images?' )) {
							jQuery( this ).parent().fadeOut(
								700,
								function() {
									jQuery( this ).remove();
								}
							);
						}
						return false;
					}
				);

				/**
				 * Delete All Slides Callback Function
				 */
				jQuery( '#remove-all-slides' ).on(
					'click',
					function() {
						if (confirm( 'Are sure to delete all images?' )) {
							awl_image_gallery.ul.empty();
						}
						return false;
					}
				);

			},
			
			/**
			 * Show loading indicator
			 */
			showLoading: function(count) {
				var loadingHtml = '<div id="itg-loading-indicator" style="text-align:center; padding:20px; background:#f1f1f1; border-radius:8px; margin:10px 0;">' +
					'<span class="spinner is-active" style="float:none; margin:0 10px 0 0;"></span>' +
					'<strong>Loading ' + count + ' image(s)...</strong>' +
					'</div>';
				this.ul.before(loadingHtml);
			},
			
			/**
			 * Hide loading indicator
			 */
			hideLoading: function() {
				jQuery('#itg-loading-indicator').fadeOut(300, function() {
					jQuery(this).remove();
				});
			},
			
			/**
			 * Batch fetch thumbnails - single AJAX request for all images
			 */
			get_thumbnails_batch: function(ids) {
				var self = this;
				
				// Show loading indicator
				self.showLoading(ids.length);
				
				var data = {
					action: 'insta_gallery_js_batch',
					slideIds: ids
				};
				
				jQuery.post(
					ajaxurl,
					data,
					function(response) {
						self.hideLoading();
						if (response && response.success && response.data) {
							// Append all images at once
							self.ul.append(response.data);
						}
					}
				).fail(function() {
					self.hideLoading();
					alert('Error loading images. Please try again.');
				});
			},
			
			/**
			 * Legacy single thumbnail fetch (kept for backward compatibility)
			 */
			get_thumbnail: function(id, cb) {
				cb = cb || function() {};
				var data = {
					action: 'insta_gallery_js',
					slideId: id
				};
				jQuery.post(
					ajaxurl,
					data,
					function(response) {
						awl_image_gallery.ul.append( response );
						cb();
					}
				);
			}
		};
		awl_image_gallery.init();
	}
);
