jQuery(document).ready(function ($) {
	$('[data-toggle="tooltip"]').tooltip();
	$('.color-field').wpColorPicker();

	var iupStopLoop = false;
	var iupProcessingLoop = false;
	var iupLoopErrors = 0;
	var iupAjaxCall = false;

	//show a confirmation warning if leaving page during a bulk action
	$(window).on("unload", function () {
		if (iupProcessingLoop) {
			return iup_data.strings.leave_confirmation;
		}
	});

	//show an error at top of main settings page
	var showError = function (error_message) {
		$('#iup-error').text(error_message.substr(0, 200)).show();
		$('html, body').animate({scrollTop: 0}, 1000);
	};

	var buildFilelist = function (scan_continue, nonce = '') {
		if (iupStopLoop) {
			iupStopLoop = false;
			iupProcessingLoop = false;
			return false;
		}
		iupProcessingLoop = true;

		var data = {};
		if (scan_continue) {
			data.scan_continue = 1;
		}
		if (nonce) {
			data.nonce = nonce;
		} else {
			data.nonce = iup_data.nonce.scan;
		}
		$.post(
			ajaxurl + '?action=infinite-uploads-filelist',
			data,
			function (json) {
				if (json.success) {
					$('#iup-scan-storage').text(json.data.local_size);
					$('#iup-scan-files').text(json.data.local_files);
					$('#iup-scan-progress').show();
					if ( !json.data.is_done) {
						buildFilelist(
							true,
							json.data.nonce
						);
					} else {
						iupProcessingLoop = false;
						location.reload();
						return true;
					}
				} else {
					showError(json.data);
					$('.modal').modal('hide');
				}
			},
			'json'
		).fail(function () {
			showError(iup_data.strings.ajax_error);
			$('.modal').modal('hide');
		});
	};

	var fetchRemoteFilelist = function (next_token, nonce = '') {
		if (iupStopLoop) {
			iupStopLoop = false;
			iupProcessingLoop = false;
			return false;
		}
		iupProcessingLoop = true;

		var data = {next_token: next_token};
		if (nonce) {
			data.nonce = nonce;
		} else {
			data.nonce = iup_data.nonce.scan;
		}
		$.post(
			ajaxurl + '?action=infinite-uploads-remote-filelist',
			data,
			function (json) {
				if (json.success) {
					$('#iup-scan-remote-storage').text(
						json.data.cloud_size
					);
					$('#iup-scan-remote-files').text(json.data.cloud_files);
					$('#iup-scan-remote-progress').show();
					if ( !json.data.is_done) {
						fetchRemoteFilelist(
							json.data.next_token,
							json.data.nonce
						);
					} else {
						if ('upload' === window.iupNextStep) {
							//update values in next modal
							$('#iup-progress-size').text(
								json.data.remaining_size
							);
							$('#iup-progress-files').text(
								json.data.remaining_files
							);
							if ('0' == json.data.remaining_files) {
								$('#iup-upload-progress').hide();
							} else {
								$('#iup-upload-progress').show();
							}
							$('#iup-sync-progress-bar')
								.css('width', json.data.pcnt_complete + '%')
								.attr(
									'aria-valuenow',
									json.data.pcnt_complete
								)
								.text(json.data.pcnt_complete + '%');

							$('#iup-sync-button').attr(
								'data-target',
								'#upload-modal'
							);
							$('.modal').modal('hide');
							$('#upload-modal').modal('show');
						} else if ('download' === window.iupNextStep) {
							$('.modal').modal('hide');
							$('#download-modal').modal('show');
						} else {
							location.reload();
						}
					}
				} else {
					showError(json.data);
					$('.modal').modal('hide');
				}
			},
			'json'
		).fail(function () {
			showError(iup_data.strings.ajax_error);
			$('.modal').modal('hide');
		});
	};

	var syncFilelist = function (nonce = '') {
		if (iupStopLoop) {
			iupStopLoop = false;
			iupProcessingLoop = false;
			return false;
		}
		iupProcessingLoop = true;

		var data = {};
		if (nonce) {
			data.nonce = nonce;
		} else {
			data.nonce = iup_data.nonce.sync;
		}
		iupAjaxCall = $.post(
			ajaxurl + '?action=infinite-uploads-sync',
			data,
			function (json) {
				iupLoopErrors = 0;
				if (json.success) {
					//$('.iup-progress-pcnt').text(json.data.pcnt_complete);
					$('#iup-progress-size').text(json.data.remaining_size);
					$('#iup-progress-files').text(
						json.data.remaining_files
					);
					$('#iup-upload-progress').show();
					$('#iup-sync-progress-bar')
						.css('width', json.data.pcnt_complete + '%')
						.attr('aria-valuenow', json.data.pcnt_complete)
						.text(json.data.pcnt_complete + '%');
					if ( !json.data.is_done) {
						data.nonce = json.data.nonce; //save for future errors
						syncFilelist(json.data.nonce);
					} else {
						iupStopLoop = true;
						$('#iup-upload-progress').hide();
						//update values in next modal
						$('#iup-enable-errors span').text(
							json.data.permanent_errors
						);
						if (json.data.permanent_errors) {
							$('.iup-enable-errors').show();
						}
						$('#iup-sync-button').attr(
							'data-target',
							'#enable-modal'
						);
						$('.modal').modal('hide');
						$('#enable-modal').modal('show');
					}
					if (
						Array.isArray(json.data.errors) &&
						json.data.errors.length
					) {
						$.each(json.data.errors, function (i, value) {
							$('#iup-sync-errors ul').append(
								'<li><span class="dashicons dashicons-warning"></span> ' +
								value +
								'</li>'
							);
						});
						$('#iup-sync-errors').show();
						var scroll = $('#iup-sync-errors')[0].scrollHeight;
						$('#iup-sync-errors').animate(
							{scrollTop: scroll},
							5000
						);
					}
				} else {
					showError(json.data);
					$('.modal').modal('hide');
				}
			},
			'json'
		).fail(function () {
			//if we get an error like 504 try up to 6 times with an exponential backoff to let the server cool down before giving up.
			iupLoopErrors++;
			if (iupLoopErrors > 6) {
				showError(iup_data.strings.ajax_error);
				$('.modal').modal('hide');
				iupLoopErrors = 0;
				iupProcessingLoop = false;
			} else {
				var exponentialBackoff = Math.floor(
					Math.pow(iupLoopErrors, 2.5) * 1000
				); //max 90s
				console.log(
					'Server error. Waiting ' +
					exponentialBackoff +
					'ms before retrying'
				);
				setTimeout(function () {
					syncFilelist(data.nonce);
				}, exponentialBackoff);
			}
		});
	};

	var getSyncStatus = function () {
		if ( !iupProcessingLoop) {
			return false;
		}

		$.get(
			ajaxurl + '?action=infinite-uploads-status',
			function (json) {
				if (json.success) {
					$('#iup-progress-size').text(json.data.remaining_size);
					$('#iup-progress-files').text(
						json.data.remaining_files
					);
					$('#iup-upload-progress').show();
					$('#iup-sync-progress-bar')
						.css('width', json.data.pcnt_complete + '%')
						.attr('aria-valuenow', json.data.pcnt_complete)
						.text(json.data.pcnt_complete + '%');
				} else {
					showError(json.data);
				}
			},
			'json'
		)
			.fail(function () {
				showError(iup_data.strings.ajax_error);
			})
			.always(function () {
				setTimeout(function () {
					getSyncStatus();
				}, 15000);
			});
	};

	var deleteFiles = function () {
		if (iupStopLoop) {
			iupStopLoop = false;
			return false;
		}

		$.post(
			ajaxurl + '?action=infinite-uploads-delete',
			{nonce: iup_data.nonce.delete},
			function (json) {
				if (json.success) {
					//$('.iup-progress-pcnt').text(json.data.pcnt_complete);
					$('#iup-delete-size').text(json.data.deletable_size);
					$('#iup-delete-files').text(json.data.deletable_files);
					if ( !json.data.is_done) {
						deleteFiles();
					} else {
						location.reload();
						return true;
					}
				} else {
					showError(json.data);
					$('.modal').modal('hide');
				}
			},
			'json'
		).fail(function () {
			showError(iup_data.strings.ajax_error);
			$('.modal').modal('hide');
		});
	};

	var downloadFiles = function (nonce = '') {
		if (iupStopLoop) {
			iupStopLoop = false;
			iupProcessingLoop = false;
			return false;
		}
		iupProcessingLoop = true;

		var data = {};
		if (nonce) {
			data.nonce = nonce;
		} else {
			data.nonce = iup_data.nonce.download;
		}
		$.post(
			ajaxurl + '?action=infinite-uploads-download',
			data,
			function (json) {
				iupLoopErrors = 0;
				if (json.success) {
					//$('.iup-progress-pcnt').text(json.data.pcnt_complete);
					$('#iup-download-size').text(json.data.deleted_size);
					$('#iup-download-files').text(json.data.deleted_files);
					$('#iup-download-progress').show();
					$('#iup-download-progress-bar')
						.css('width', json.data.pcnt_downloaded + '%')
						.attr('aria-valuenow', json.data.pcnt_downloaded)
						.text(json.data.pcnt_downloaded + '%');
					if ( !json.data.is_done) {
						data.nonce = json.data.nonce; //save for future errors
						downloadFiles(json.data.nonce);
					} else {
						iupProcessingLoop = false;
						location.reload();
						return true;
					}
					if (
						Array.isArray(json.data.errors) &&
						json.data.errors.length
					) {
						$.each(json.data.errors, function (i, value) {
							$('#iup-download-errors ul').append(
								'<li><span class="dashicons dashicons-warning"></span> ' +
								value +
								'</li>'
							);
						});
						$('#iup-download-errors').show();
						var scroll = $('#iup-download-errors')[0]
							.scrollHeight;
						$('#iup-download-errors').animate(
							{scrollTop: scroll},
							5000
						);
					}
				} else {
					showError(json.data);
					$('.modal').modal('hide');
				}
			},
			'json'
		).fail(function () {
			//if we get an error like 504 try up to 6 times before giving up.
			iupLoopErrors++;
			if (iupLoopErrors > 6) {
				showError(iup_data.strings.ajax_error);
				$('.modal').modal('hide');
				iupLoopErrors = 0;
				iupProcessingLoop = false;
			} else {
				var exponentialBackoff = Math.floor(
					Math.pow(iupLoopErrors, 2.5) * 1000
				); //max 90s
				console.log(
					'Server error. Waiting ' +
					exponentialBackoff +
					'ms before retrying'
				);
				setTimeout(function () {
					downloadFiles(data.nonce);
				}, exponentialBackoff);
			}
		});
	};

	//Scan
	$('#scan-modal')
		.on('show.bs.modal', function () {
			$('#iup-error').hide();
			iupStopLoop = false;
			buildFilelist(false);
		})
		.on('hide.bs.modal', function () {
			iupStopLoop = true;
			iupProcessingLoop = false;
		});

	// Disconnect confirmation when folders are in use.
	$('#iup-disconnect-btn').on('click', function (e) {
		var folderCount = parseInt($(this).data('folder-count'), 10) || 0;
		if (folderCount > 0) {
			var confirmed = confirm(
				'Disconnecting will place your site in an unlicensed state. Your folder structure and media customizations (' + folderCount + ' folder(s)) will be removed, but no media files will be deleted.\n\n' +
				'All items will remain safely available in the default WordPress Media Library. If you reconnect and license this site again, your folders may be restored.\n\n' +
				'Are you sure you want to disconnect?'
			);
			if (!confirmed) {
				e.preventDefault();
				e.stopPropagation();
				return false;
			}
		}
	});

	//Compare to live
	$('#scan-remote-modal')
		.on('show.bs.modal', function (e) {
			$('#iup-error').hide();
			iupStopLoop = false;
			var button = $(e.relatedTarget); // Button that triggered the modal
			window.iupNextStep = button.data('next'); // Extract info from data-* attributes
			fetchRemoteFilelist(null);
		})
		.on('hide.bs.modal', function () {
			iupStopLoop = true;
			iupProcessingLoop = false;
		});

	//Sync
	$('#upload-modal')
		.on('show.bs.modal', function () {
			$('.iup-enable-errors').hide(); //hide errors on enable modal
			$('#iup-collapse-errors').collapse('hide');
			$('#iup-error').hide();
			$('#iup-sync-errors').hide();
			$('#iup-sync-errors ul').empty();
			iupStopLoop = false;
			syncFilelist();
			setTimeout(function () {
				getSyncStatus();
			}, 15000);
		})
		.on('shown.bs.modal', function () {
			$('#scan-remote-modal').modal('hide');
		})
		.on('hide.bs.modal', function () {
			iupStopLoop = true;
			iupProcessingLoop = false;
			iupAjaxCall.abort();
		});

	//Make sure upload modal closes
	$('#enable-modal')
		.on('shown.bs.modal', function () {
			$('#upload-modal').modal('hide');
		})
		.on('hidden.bs.modal', function () {
			$('#iup-enable-spinner').addClass('text-hide');
			$('#iup-enable-button').show();
		});

	$('#iup-collapse-errors').on('show.bs.collapse', function () {
		// load up list of errors via ajax
		$.get(
			ajaxurl + '?action=infinite-uploads-sync-errors',
			function (json) {
				if (json.success) {
					$('#iup-collapse-errors .list-group').html(json.data);
				}
			},
			'json'
		);
	});

	$('#iup-resync-button').on('click', function (e) {
		$('.iup-enable-errors').hide(); //hide errors on enable modal
		$('#iup-collapse-errors').collapse('hide');
		$('#iup-enable-button').hide();
		$('#iup-enable-spinner').removeClass('text-hide');
		$.post(
			ajaxurl + '?action=infinite-uploads-reset-errors',
			{foo: 'bar'},
			function (json) {
				if (json.success) {
					$('.modal').modal('hide');
					$('#upload-modal').modal('show');
					return true;
				}
			},
			'json'
		).fail(function () {
			showError(iup_data.strings.ajax_error);
			$('.modal').modal('hide');
		});
	});

	//Download
	$('#download-modal')
		.on('show.bs.modal', function () {
			$('#iup-error').hide();
			$('#iup-download-errors').hide();
			$('#iup-download-errors ul').empty();
			iupStopLoop = false;
			downloadFiles();
		})
		.on('hide.bs.modal', function () {
			iupStopLoop = true;
			iupProcessingLoop = false;
		});

	//Delete
	$('#delete-modal')
		.on('show.bs.modal', function () {
			$('#iup-error').hide();
			iupStopLoop = false;
			$('#iup-delete-local-button').show();
			$('#iup-delete-local-spinner').hide();
		})
		.on('hide.bs.modal', function () {
			iupStopLoop = true;
		});

	//Delete local files
	$('#iup-delete-local-button').on('click', function () {
		$(this).hide();
		$('#iup-delete-local-spinner').show();
		deleteFiles();
	});

	//Enable infinite uploads
	$('#iup-enable-button').on('click', function () {
		$('.iup-enable-errors').hide(); //hide errors on enable modal
		$('#iup-collapse-errors').collapse('hide');
		$('#iup-enable-button').hide();
		$('#iup-enable-spinner').removeClass('text-hide');
		$.post(
			ajaxurl + '?action=infinite-uploads-toggle',
			{enabled: true, nonce: iup_data.nonce.toggle},
			function (json) {
				if (json.success) {
					location.reload();
					return true;
				}
			},
			'json'
		).fail(function () {
			showError(iup_data.strings.ajax_error);
			$('#iup-enable-spinner').addClass('text-hide');
			$('#iup-enable-button').show();
			$('.modal').modal('hide');
		});
	});

	//Enable video cloud
	$('#iup-enable-video-button').on('click', function () {
		$('#iup-enable-video-button').hide();
		$('#iup-enable-video-spinner').removeClass('d-none').addClass('d-block');
		$.post(
			ajaxurl + '?action=infinite-uploads-video-activate',
			{nonce: iup_data.nonce.video},
			function (json) {
				if (json.success) {
					location.reload();
					return true;
				} else {
					$('#iup-enable-video-spinner').addClass('d-none').removeClass('d-block');
					$('#iup-enable-video-button').show();
				}
			},
			'json'
		).fail(function () {
			showError(iup_data.strings.ajax_error);
			$('#iup-enable-video-spinner').addClass('d-none').removeClass('d-block');
			$('#iup-enable-video-button').show();
		});
	});

	//refresh api data
	$('.iup-refresh-icon .dashicons').on('click', function () {
		$(this).hide();
		$('.iup-refresh-icon .spinner-grow').removeClass('text-hide');
		window.location = $(this).attr('data-target');
	});

	//Charts
	var bandwidthFormat = function (bytes) {
		if (bytes < 1024) {
			return bytes + ' B';
		} else if (bytes < 1024 * 1024) {
			return Math.round(bytes / 1024) + ' KB';
		} else if (bytes < 1024 * 1024 * 1024) {
			return Math.round((bytes / 1024 / 1024) * 10) / 10 + ' MB';
		} else {
			return (
				Math.round((bytes / 1024 / 1024 / 1024) * 100) / 100 + ' GB'
			);
		}
	};

	var sizelabel = function (tooltipItem, data) {
		var label = ' ' + data.labels[tooltipItem.index] || '';
		return label;
	};

	window.onload = function () {
		var pie1 = document.getElementById('iup-local-pie');
		if (pie1) {
			var config_local = {
				type: 'pie',
				data: iup_data.local_types,
				options: {
					responsive: true,
					legend: false,
					tooltips: {
						callbacks: {
							label: sizelabel,
						},
						backgroundColor: '#F1F1F1',
						bodyFontColor: '#2A2A2A',
					},
					title: {
						display: true,
						position: 'bottom',
						fontSize: 18,
						fontStyle: 'normal',
						text: iup_data.local_types.total,
					},
				},
			};

			var ctx = pie1.getContext('2d');
			window.myPieLocal = new Chart(ctx, config_local);
		}

		var pie2 = document.getElementById('iup-cloud-pie');
		if (pie2) {
			var config_cloud = {
				type: 'pie',
				data: iup_data.cloud_types,
				options: {
					responsive: true,
					legend: false,
					tooltips: {
						callbacks: {
							label: sizelabel,
						},
						backgroundColor: '#F1F1F1',
						bodyFontColor: '#2A2A2A',
					},
					title: {
						display: true,
						position: 'bottom',
						fontSize: 18,
						fontStyle: 'normal',
						text: iup_data.cloud_types.total,
					},
				},
			};

			var ctx = pie2.getContext('2d');
			window.myPieCloud = new Chart(ctx, config_cloud);
		}
	};


	let saveTimeout;
	const excludedFiles = $('#iu_excluded_files');

	// Load saved value
	excludedFiles.val(iup_data.excludedFiles || '');

	// Save on change with debounce
	excludedFiles.on('input', function () {
		clearTimeout(saveTimeout);

		$('.iu-success-message').remove();

		saveTimeout = setTimeout(function () {
			$.ajax({
				url: ajaxurl,
				type: 'POST',
				data: {
					action: 'save_iu_excluded_files',
					excluded_files: excludedFiles.val(),
					nonce: iup_data.nonce.excludedFiles
				},
				success: function (response) {
					if (response.success) {
						// Create and insert success message
						const successMessage = $('<div class="iu-success-message" style="color: #008000; margin-top: 8px;">Settings saved successfully!</div>');
						excludedFiles.after(successMessage);

						// Remove the message after 3 seconds
						setTimeout(function () {
							successMessage.fadeOut(400, function () {
								$(this).remove();
							});
						}, 3000);
					}
				}
			});
		}, 1000);
	});


	function initFolderTree() {
		if ($.jstree.reference('#folderTree')) {
			return; // Already initialized.
		}
		$('#folderTree').jstree({
			'plugins': ["checkbox"],
			'checkbox': {
				// Select all children when parent is selected
				'three_state': true,
				'cascade': 'up+down',
				'tie_selection': true
			},
			'core': {
				'data': {
					"url": ajaxurl,
					"data": function (node) {
						var params = {
							"action": "get_directory_tree",
							"nonce": iup_data.nonce.getTree
						};
						if (node.id !== '#') {
							params.dir = node.data.path;
						}
						return params;
					},
					"dataType": "json"
				}
			}
		})
			// When folder checkbox clicked → open folder
			.on("select_node.jstree", function (e, data) {
				if (data.node.children.length) {
					$('#folderTree').jstree('open_node', data.node);
				}
			});
	}

	// Show/hide folder tree based on file exclusion radio buttons.
	$('input[name="iu_file_exclusion_enabled"]').on('change', function () {
		if ($(this).val() === 'yes') {
			$('#folderTreeWrapper').show();
			initFolderTree();
		} else {
			$('#folderTreeWrapper').hide();
		}
	});

	// Initialize tree on page load if file exclusion is already enabled.
	if ($('input[name="iu_file_exclusion_enabled"]:checked').val() === 'yes') {
		initFolderTree();
	}

	// Get selected paths
	$('#saveExcludedFilesSettings').on("click", function () {
		var selected = $('#folderTree').jstree("get_selected", true);

		var excludedFiles = [];
		if (selected.length) {
			excludedFiles = selected.map(node => node.data.path);
		}

		const excludedFilesOption = $('input[name="iu_file_exclusion_enabled"]:checked').val();

		$.ajax({
			url: ajaxurl,
			type: 'POST',
			data: {
				action: 'save_iu_excluded_files',
				excluded_files: excludedFiles,
				enabled_excluded_files: excludedFilesOption,
				nonce: iup_data.nonce.saveExcludedFiles
			},
			success: function (response) {
				if (response.success) {
					// Create and insert success message
					const successMessage = $('<span class="iu-success-message" style="color: #008000; margin-top: 8px;">Settings saved successfully! Syncing is in progress. We will notify you once it\'s done.</span>');
					// Remove any existing success messages before adding a new one
					$('.iu-success-message').remove();
					// Append the new success message
					$('#saveExcludedFilesSettings').after(successMessage);

					// Remove the message after 3 seconds
					setTimeout(function () {
						successMessage.fadeOut(400, function () {
							$(this).remove();
						});
					}, 3000);
				}
			}
		});
	});

	// Save media folders setting.
	$('#saveMediaFoldersSetting').on('click', function () {
		var $btn = $(this);
		var enabled = $('input[name="iu_media_folders_enabled"]:checked').val();
		var folderCount = parseInt($btn.data('folder-count'), 10) || 0;

		// Show confirmation if disabling while folders exist.
		if (enabled === 'no' && folderCount > 0) {
			var confirmed = confirm(
				'You currently have ' + folderCount + ' folder(s) in use. ' +
				'Disabling advanced features will hide the folder sidebar and all media will appear in the default WordPress view as uncategorized.\n\n' +
				'Your folder structure will be preserved and can be restored by re-enabling this setting.\n\n' +
				'Are you sure you want to disable?'
			);
			if (!confirmed) {
				// Revert radio back to enabled.
				$('input[name="iu_media_folders_enabled"][value="yes"]').prop('checked', true);
				return;
			}
		}

		$btn.prop('disabled', true);
		$('#mediaFoldersSaveStatus').hide();

		$.ajax({
			url: ajaxurl,
			type: 'POST',
			data: {
				action: 'save_iu_media_folders_setting',
				enabled: enabled,
				nonce: iup_data.nonce.saveMediaFolders
			},
			success: function (response) {
				$btn.prop('disabled', false);
				if (response.success) {
					$('#mediaFoldersSaveStatus').fadeIn();
					setTimeout(function () {
						$('#mediaFoldersSaveStatus').fadeOut();
					}, 5000);
				}
			},
			error: function () {
				$btn.prop('disabled', false);
			}
		});
	});

});
