<!--
 ~ Copyright 2026 youngmonkeys.org
 ~
 ~ Licensed under the ezyplatform, Version 1.0.0 (the "License");
 ~ you may not use this file except in compliance with the License.
 ~ You may obtain a copy of the License at
 ~
 ~     https://youngmonkeys.org/licenses/ezyplatform-1.0.0.txt
 ~
 ~ Unless required by applicable law or agreed to in writing, software
 ~ distributed under the License is distributed on an "AS IS" BASIS,
 ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 ~ See the License for the specific language governing permissions and
 ~ limitations under the License.
-->

<!DOCTYPE html>

<html xmlns:th="http://www.thymeleaf.org">
<body>
<th:block th:fragment="content">
	<div class="modal fade pt-4rem" role="dialog" id="reduce-selected-media-files-confirmation">
		<div class="modal-dialog" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<h5 class="modal-title">[[#{confirmation}]]</h5>
					<button type="button" class="close" data-dismiss="modal" aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
				</div>
				<div class="modal-body">
					<span id="reduce-selected-media-files-confirmation-body-content">[[${body}]]</span>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-primary"
							onclick="ezyadmin.onReduceSelectedMediaFilesPromptConfirmed()">
						[[#{confirm}]]
					</button>
					<button type="button" class="btn btn-secondary" data-dismiss="modal">
						[[#{cancel}]]
					</button>
				</div>
			</div>
		</div>
	</div>
</th:block>
<script th:fragment="scripts">
ezyadmin.onReduceSelectedMediaFilesClick = function() {
	$('#reduce-selected-media-files-confirmation-body-content').text(
		ezyadmin
			.messages
			.do_you_want_to_reduce_selected_media_file_size
			.replace('{0}', ezyadmin.checkedMediaItemIds.length)
	);
	$('#reduce-selected-media-files-confirmation').modal('show');
}

ezyadmin.onReduceSelectedMediaFilesPromptConfirmed = async function() {
	$('#reduce-selected-media-files-confirmation').modal('hide');
	var errorMimeTypes = [];
	var successCount = 0;
	var errorCount = 0;
	var totalSelectedMedia = ezyadmin.checkedMediaItemIds.length;
	ezyadmin.showLoadingScreen();
	for(var i = 0; i < totalSelectedMedia; ++i) {
		var reduceFailed = false;
		var mediaId = ezyadmin.checkedMediaItemIds[i];
		var media = ezyadmin.mediaById[mediaId] || {};
		try {
			var result = await ezyadmin.reduceMediaFileSizeAsync({
				mediaId: mediaId,
				expectedFileSize: ezyadmin.maxReducedImageFileSize
			});
			if (result.reduced) {
				++successCount;
			} else if (media.size > ezyadmin.maxReducedImageFileSize) {
				reduceFailed = true;
			}
		} catch(e) {
			reduceFailed = true;
		}
		if (reduceFailed) {
			++errorCount;
			var mimeType = media.mimeType || '';
			if (mimeType && errorMimeTypes.indexOf(mimeType) < 0) {
				errorMimeTypes.push(mimeType);
			}
		}
	}
	ezyadmin.hideLoadingScreen();
	var message = ezyadmin
			.messages.reduced_the_file_size_of_files_summary
			.replace('{0}', successCount)
			.replace('{1}', totalSelectedMedia);
	if (errorCount) {
		message += '. ' + ezyadmin
			.messages.reduce_file_size_failed_message
			.replace('{0}', errorMimeTypes.join(', '));
	}
	ezyadmin.showAlert(message);
	ezyadmin.fetchMediaListAndClearCheckedMediaItems();
}
</script>
</body>
</html>
