/**
 * CacheLoop – one-click "Optimize with CacheLoop" / "Revert" from the Media
 * Library (List view row action + Grid view attachment details modal).
 *
 * @package CacheLoop
 */
( function ( $ ) {
	'use strict';

	function clAjaxAction( action, attachmentId, onDone ) {
		$.ajax(
			{
				url:    clMediaData.ajaxUrl,
				method: 'POST',
				data: {
					action:        action,
					nonce:         clMediaData.nonce,
					attachment_id: attachmentId,
				},
				success: function ( response ) {
					onDone( response.success, response.success ? response.data.message : ( response.data && response.data.message ? response.data.message : 'Error' ) );
				},
				error: function () {
					onDone( false, 'Request failed.' );
				},
			}
		);
	}

	// List view row action: Optimize.
	$( document ).on(
		'click',
		'.cl-media-optimize-link',
		function ( e ) {
			e.preventDefault();
			var $link = $( this );
			var id    = $link.data( 'id' );

			$link.text( 'Optimizing…' );

			clAjaxAction(
				'cacheloop_optimize_single_image',
				id,
				function ( ok, message ) {
					if ( ok ) {
						$link.replaceWith(
							'<span class="cl-media-optimized-status" style="color:#198754;">' + message + '</span>'
							+ ' — <a href="#" class="cl-media-revert-link" data-id="' + id + '">Revert</a>'
						);
					} else {
						$link.text( 'Optimize (CacheLoop)' );
						window.alert( message );
					}
				}
			);
		}
	);

	// List view row action: Revert.
	$( document ).on(
		'click',
		'.cl-media-revert-link',
		function ( e ) {
			e.preventDefault();
			if ( ! window.confirm( 'Revert this image? The WebP version will be deleted; the original stays untouched.' ) ) {
				return;
			}

			var $link = $( this );
			var id    = $link.data( 'id' );

			$link.text( 'Reverting…' );

			clAjaxAction(
				'cacheloop_revert_image',
				id,
				function ( ok, message ) {
					if ( ok ) {
						// WP core wraps each row action entry in its own
						// <span class="cl_optimize"> — replace that whole
						// span's contents rather than just the <a>, since the
						// "Optimized — " status text/separator lives there too.
						$link.parent().html( '<a href="#" class="cl-media-optimize-link" data-id="' + id + '">Optimize (CacheLoop)</a>' );
					} else {
						$link.text( 'Revert' );
						window.alert( message );
					}
				}
			);
		}
	);

	// Grid view modal / single edit screen: Optimize button.
	$( document ).on(
		'click',
		'.cl-media-optimize-btn',
		function ( e ) {
			e.preventDefault();
			var $btn    = $( this );
			var id      = $btn.closest( '.cl-media-optimize-wrap' ).data( 'id' );
			var $status = $btn.siblings( '.cl-media-optimize-status' );

			$btn.prop( 'disabled', true );
			$status.text( 'Optimizing…' );

			clAjaxAction(
				'cacheloop_optimize_single_image',
				id,
				function ( ok, message ) {
					if ( ok ) {
						$status.css( 'color', '#198754' ).text( message );
						$btn.text( 'Revert' ).prop( 'disabled', false ).removeClass( 'cl-media-optimize-btn' ).addClass( 'cl-media-revert-btn' );
					} else {
						$btn.prop( 'disabled', false );
						$status.css( 'color', '#dc3545' ).text( message );
					}
				}
			);
		}
	);

	// Grid view modal / single edit screen: Revert button.
	$( document ).on(
		'click',
		'.cl-media-revert-btn',
		function ( e ) {
			e.preventDefault();
			if ( ! window.confirm( 'Revert this image? The WebP version will be deleted; the original stays untouched.' ) ) {
				return;
			}

			var $btn    = $( this );
			var id      = $btn.closest( '.cl-media-optimize-wrap' ).data( 'id' );
			var $status = $btn.siblings( '.cl-media-optimize-status, .cl-media-revert-status' );

			$btn.prop( 'disabled', true );
			$status.text( 'Reverting…' );

			clAjaxAction(
				'cacheloop_revert_image',
				id,
				function ( ok, message ) {
					if ( ok ) {
						$status.css( 'color', '' ).text( '' );
						$btn.text( 'Optimize with CacheLoop' ).prop( 'disabled', false ).removeClass( 'cl-media-revert-btn' ).addClass( 'cl-media-optimize-btn' );
					} else {
						$btn.prop( 'disabled', false );
						$status.css( 'color', '#dc3545' ).text( message );
					}
				}
			);
		}
	);
} )( jQuery );
