/**
 * RedirectZen admin: inline row actions.
 *
 * Interaction rules (Zen Plugins spec): destructive actions keep a
 * blocking confirm(); reversible actions apply instantly and offer an
 * inline Undo.
 */
( function ( $ ) {
	'use strict';

	function post( action, data ) {
		return $.post( rdzAdmin.ajaxUrl, $.extend( { action: action, nonce: rdzAdmin.nonce }, data ) );
	}

	function rowOf( el ) {
		return $( el ).closest( 'tr' );
	}

	function fadeOutRow( $row ) {
		$row.css( 'background', '#fcf0f1' ).fadeOut( 400, function () {
			$( this ).remove();
		} );
	}

	function showNotice( text, undo ) {
		$( '.rdz-undo-notice' ).remove();
		var $notice = $( '<div class="notice notice-success rdz-undo-notice"><p></p></div>' );
		$notice.find( 'p' ).text( text + ' ' );
		if ( undo ) {
			$notice.find( 'p' ).append(
				$( '<a href="#" class="rdz-undo-link"></a>' ).text( rdzAdmin.i18n.undo ).on( 'click', function ( e ) {
					e.preventDefault();
					undo();
					$notice.remove();
				} )
			);
		}
		var $anchor = $( '.rdz-wrap hr.wp-header-end' );
		if ( $anchor.length ) {
			$anchor.after( $notice );
		} else {
			$( '.rdz-wrap .zen-main' ).first().prepend( $notice );
		}
	}

	/* ------------------------------------------------------------------
	 * Redirects table
	 * ---------------------------------------------------------------- */

	$( document ).on( 'click', '.rdz-action', function ( e ) {
		e.preventDefault();

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

		if ( action === 'edit' ) {
			$row.find( '.rdz-inline-edit' ).toggle().find( '.rdz-edit-target' ).trigger( 'focus' );
			return;
		}

		if ( action === 'delete' ) {
			if ( ! window.confirm( rdzAdmin.i18n.confirmDelete ) ) {
				return;
			}
			$link.text( rdzAdmin.i18n.working );
			post( 'rdz_delete_redirect', { redirect_id: id } )
				.done( function ( r ) {
					if ( r.success ) {
						fadeOutRow( $row );
					} else {
						window.alert( ( r.data && r.data.message ) || rdzAdmin.i18n.error );
					}
				} )
				.fail( function () { window.alert( rdzAdmin.i18n.error ); } );
			return;
		}

		if ( action === 'toggle' ) {
			var active = parseInt( $link.attr( 'data-active' ), 10 ) ? 0 : 1;
			post( 'rdz_toggle_redirect', { redirect_id: id, active: active } )
				.done( function ( r ) {
					if ( ! r.success ) {
						window.alert( ( r.data && r.data.message ) || rdzAdmin.i18n.error );
						return;
					}
					if ( ! active ) {
						showNotice( rdzAdmin.i18n.disabled, function () {
							post( 'rdz_toggle_redirect', { redirect_id: id, active: 1 } ).done( function () {
								window.location.reload();
							} );
						} );
					}
					window.setTimeout( function () { window.location.reload(); }, active ? 0 : 1200 );
				} )
				.fail( function () { window.alert( rdzAdmin.i18n.error ); } );
			return;
		}

		if ( action === 'verify' ) {
			$link.text( rdzAdmin.i18n.verifying );
			post( 'rdz_verify_redirect', { redirect_id: id } )
				.done( function ( r ) {
					if ( r.success ) {
						window.location.reload();
					} else {
						window.alert( ( r.data && r.data.message ) || rdzAdmin.i18n.error );
					}
				} )
				.fail( function () { window.alert( rdzAdmin.i18n.error ); } );
		}
	} );

	$( document ).on( 'click', '.rdz-save-edit', function () {
		var $box = $( this ).closest( '.rdz-inline-edit' );
		var $row = rowOf( this );
		var $btn = $( this );

		$btn.prop( 'disabled', true ).text( rdzAdmin.i18n.working );

		post( 'rdz_update_redirect', {
			redirect_id: $box.data( 'id' ),
			source: $box.find( '.rdz-edit-source' ).val(),
			target: $box.find( '.rdz-edit-target' ).val(),
			redirect_type: $box.find( '.rdz-edit-type' ).val()
		} )
			.done( function ( r ) {
				if ( r.success ) {
					window.location.reload();
				} else {
					window.alert( ( r.data && r.data.message ) || rdzAdmin.i18n.error );
					$btn.prop( 'disabled', false ).text( 'Save' );
				}
			} )
			.fail( function () {
				window.alert( rdzAdmin.i18n.error );
				$btn.prop( 'disabled', false ).text( 'Save' );
			} );
	} );

	$( document ).on( 'click', '.rdz-cancel-edit', function () {
		$( this ).closest( '.rdz-inline-edit' ).hide();
	} );

	/* ------------------------------------------------------------------
	 * 404 log table
	 * ---------------------------------------------------------------- */

	$( document ).on( 'click', '.rdz-404-action', function ( e ) {
		e.preventDefault();

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

		if ( action === 'open-create' ) {
			$row.find( '.rdz-inline-create' ).toggle().find( '.rdz-create-target' ).trigger( 'focus' );
			return;
		}

		if ( action === 'delete' ) {
			if ( ! window.confirm( rdzAdmin.i18n.confirmDelete ) ) {
				return;
			}
			post( 'rdz_delete_404', { log_id: id } )
				.done( function ( r ) {
					if ( r.success ) {
						fadeOutRow( $row );
					} else {
						window.alert( ( r.data && r.data.message ) || rdzAdmin.i18n.error );
					}
				} );
			return;
		}

		if ( action === 'ignore' || action === 'unignore' ) {
			var status = action === 'ignore' ? 1 : 0;
			post( 'rdz_404_status', { log_id: id, status: status } )
				.done( function ( r ) {
					if ( ! r.success ) {
						window.alert( ( r.data && r.data.message ) || rdzAdmin.i18n.error );
						return;
					}
					if ( status === 1 ) {
						showNotice( rdzAdmin.i18n.ignored, function () {
							post( 'rdz_404_status', { log_id: id, status: 0 } ).done( function () {
								window.location.reload();
							} );
						} );
					}
					fadeOutRow( $row );
				} );
		}
	} );

	$( document ).on( 'click', '.rdz-save-create', function () {
		var $box = $( this ).closest( '.rdz-inline-create' );
		var $row = rowOf( this );
		var $btn = $( this );

		$btn.prop( 'disabled', true ).text( rdzAdmin.i18n.working );

		post( 'rdz_create_from_404', {
			log_id: $box.data( 'id' ),
			target: $box.find( '.rdz-create-target' ).val(),
			redirect_type: $box.find( '.rdz-create-type' ).val()
		} )
			.done( function ( r ) {
				if ( r.success ) {
					showNotice( rdzAdmin.i18n.redirectMade );
					fadeOutRow( $row );
				} else {
					window.alert( ( r.data && r.data.message ) || rdzAdmin.i18n.error );
					$btn.prop( 'disabled', false ).text( 'Redirect' );
				}
			} )
			.fail( function () {
				window.alert( rdzAdmin.i18n.error );
				$btn.prop( 'disabled', false ).text( 'Redirect' );
			} );
	} );

	$( document ).on( 'click', '.rdz-cancel-create', function () {
		$( this ).closest( '.rdz-inline-create' ).hide();
	} );

	/* Pro: suggestions */
	$( document ).on( 'click', '.rdz-suggest-btn', function () {
		var $btn = $( this );
		var $box = $btn.closest( '.rdz-inline-create' );

		$btn.prop( 'disabled', true ).text( rdzAdmin.i18n.working );
		post( 'rdz_suggest', { path: $btn.data( 'path' ) } )
			.done( function ( r ) {
				$btn.prop( 'disabled', false ).text( 'Suggest target' );
				var $out = $box.find( '.rdz-suggestions' ).empty();
				if ( ! r.success || ! r.data.suggestions || ! r.data.suggestions.length ) {
					$out.text( rdzAdmin.i18n.noSuggestions );
					return;
				}
				r.data.suggestions.forEach( function ( s ) {
					$( '<button type="button" class="button-link rdz-suggestion"></button>' )
						.text( s.title + ' (' + Math.round( s.score * 100 ) + '%)' )
						.attr( 'data-url', s.url )
						.appendTo( $out );
				} );
			} );
	} );

	$( document ).on( 'click', '.rdz-suggestion', function () {
		$( this ).closest( '.rdz-inline-create' ).find( '.rdz-create-target' ).val( $( this ).data( 'url' ) );
	} );

	/* ------------------------------------------------------------------
	 * Shared: task dismissal + opt-in card
	 * ---------------------------------------------------------------- */

	$( document ).on( 'click', '.zen-hide-task', function () {
		var $row = $( this ).closest( '.zen-issue, .zen-review-ask, .rdz-optin-card' );
		post( 'rdz_hide_task', { task: $( this ).data( 'task' ) } )
			.done( function ( response ) {
				if ( response.success ) {
					$row.slideUp( 200, function () {
						$( this ).remove();
					} );
				}
			} );
	} );

	$( document ).on( 'click', '.rdz-optin-submit', function () {
		var $card = $( this ).closest( '.rdz-optin-card' );
		var email = $card.find( '.rdz-optin-email' ).val();
		$( this ).prop( 'disabled', true ).text( rdzAdmin.i18n.working );
		post( 'rdz_subscribe', { email: email } )
			.done( function ( r ) {
				if ( r.success ) {
					$card.find( '.rdz-optin-form, .rdz-optin-note, .zen-card-sub' ).remove();
					$card.append( '<p class="rdz-optin-note">✓ Subscribed. Thank you!</p>' );
				} else {
					window.alert( ( r.data && r.data.message ) || rdzAdmin.i18n.error );
					$card.find( '.rdz-optin-submit' ).prop( 'disabled', false ).text( 'Subscribe' );
				}
			} );
	} );
} )( jQuery );
