<?php
/**
 * WordPress.org free build plugin runtime.
 *
 * @package Consortia_Quick_Find_Replace
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Plugin runtime for the editor-side Quick Find & Replace tools.
 */
class Consortia_Qfr_Plugin {

	/**
	 * Supported post types.
	 *
	 * @var string[]
	 */
	private static $post_types = array( 'post', 'page', 'docs', 'product' );

	/**
	 * Plugin version.
	 *
	 * @var string
	 */
	const VERSION = CONSORTIA_QFR_VERSION;

	/**
	 * Boot.
	 *
	 * @return void
	 */
	public static function init() {
		add_action( 'add_meta_boxes', array( __CLASS__, 'add_meta_box' ) );
		add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_assets' ) );
	}

	/**
	 * Add meta box.
	 *
	 * @param string  $post_type Post type.
	 * @param WP_Post $post      Post object.
	 * @return void
	 */
	public static function add_meta_box( $post_type, $post = null ) {
		$allowed_post_types = self::get_post_types();

		if ( ! in_array( $post_type, $allowed_post_types, true ) ) {
			return;
		}

		if ( $post instanceof WP_Post && ! current_user_can( 'edit_post', $post->ID ) ) {
			return;
		}

		add_meta_box(
			'consortiaqfr_quick_find_replace',
			__( 'Quick Find & Replace', 'consortia-quick-find-replace' ),
			array( __CLASS__, 'render_meta_box' ),
			$post_type,
			'side',
			'default'
		);
	}

	/**
	 * Render meta box.
	 *
	 * @param WP_Post $post Post object.
	 * @return void
	 */
	public static function render_meta_box( $post ) {
		wp_nonce_field( 'consortiaqfr_quick_find_replace', 'consortiaqfr_quick_find_replace_nonce' );

		if ( ! $post instanceof WP_Post ) {
			echo '<p>' . esc_html__( 'Post object not available.', 'consortia-quick-find-replace' ) . '</p>';
			return;
		}
		?>
			<div id="consortia-qfr" class="consortia-qfr">
				<p>
					<label for="consortia-qfr-find"><strong><?php esc_html_e( 'Find', 'consortia-quick-find-replace' ); ?></strong></label>
					<span class="consortia-qfr-input-row consortia-qfr-find-row">
						<input type="text" id="consortia-qfr-find" class="widefat" autocomplete="off">
						<button type="button" class="button-link consortia-qfr-clear-find" id="consortia-qfr-clear-find" aria-label="<?php esc_attr_e( 'Clear search', 'consortia-quick-find-replace' ); ?>">
							<span class="dashicons dashicons-no-alt" aria-hidden="true"></span>
						</button>
					</span>
				</p>

				<details id="consortia-qfr-replace-wrap" class="consortia-qfr-replace-wrap">
					<summary><?php esc_html_e( 'Replace with', 'consortia-quick-find-replace' ); ?></summary>
					<p>
						<span class="consortia-qfr-input-row consortia-qfr-replace-row">
							<input type="text" id="consortia-qfr-replace" class="widefat" autocomplete="off" aria-label="<?php esc_attr_e( 'Replace with', 'consortia-quick-find-replace' ); ?>">
							<button type="button" class="button-link consortia-qfr-clear-replace" id="consortia-qfr-clear-replace" aria-label="<?php esc_attr_e( 'Clear replacement', 'consortia-quick-find-replace' ); ?>">
								<span class="dashicons dashicons-no-alt" aria-hidden="true"></span>
							</button>
						</span>
					</p>
				</details>

				<p>
					<label class="consortia-qfr-toggle">
						<input type="checkbox" id="consortia-qfr-also-title" value="1">
						<span class="consortia-qfr-toggle-control" aria-hidden="true"></span>
						<span class="consortia-qfr-toggle-text"><?php esc_html_e( 'Also replace in title', 'consortia-quick-find-replace' ); ?></span>
					</label>
				</p>

				<p>
					<label class="consortia-qfr-toggle">
						<input type="checkbox" id="consortia-qfr-match-case" value="1">
						<span class="consortia-qfr-toggle-control" aria-hidden="true"></span>
						<span class="consortia-qfr-toggle-text"><?php esc_html_e( 'Match case', 'consortia-quick-find-replace' ); ?></span>
					</label>
				</p>

				<p>
					<label class="consortia-qfr-toggle">
						<input type="checkbox" id="consortia-qfr-include-urls" value="1">
						<span class="consortia-qfr-toggle-control" aria-hidden="true"></span>
						<span class="consortia-qfr-toggle-text"><?php esc_html_e( 'Include URLs and HTML', 'consortia-quick-find-replace' ); ?></span>
					</label>
				</p>

				<div class="consortia-qfr-match-row">
					<span id="consortia-qfr-match-summary" class="consortia-qfr-match-summary"><?php esc_html_e( 'Ready to search', 'consortia-quick-find-replace' ); ?></span>
					<span class="consortia-qfr-match-nav">
						<button type="button" class="button" id="consortia-qfr-prev" aria-label="<?php esc_attr_e( 'Previous match', 'consortia-quick-find-replace' ); ?>"><span class="dashicons dashicons-arrow-left-alt2" aria-hidden="true"></span></button>
						<button type="button" class="button" id="consortia-qfr-next" aria-label="<?php esc_attr_e( 'Next match', 'consortia-quick-find-replace' ); ?>"><span class="dashicons dashicons-arrow-right-alt2" aria-hidden="true"></span></button>
					</span>
				</div>

				<div class="consortia-qfr-buttons">
					<button type="button" class="button" id="consortia-qfr-replace-current"><span class="dashicons dashicons-update" aria-hidden="true"></span><?php esc_html_e( 'Replace', 'consortia-quick-find-replace' ); ?></button>
					<button type="button" class="button button-primary" id="consortia-qfr-replace-all"><span class="dashicons dashicons-controls-repeat" aria-hidden="true"></span><?php esc_html_e( 'Replace all', 'consortia-quick-find-replace' ); ?></button>
					<button type="button" class="button consortia-qfr-undo" id="consortia-qfr-undo" disabled><span class="dashicons dashicons-undo" aria-hidden="true"></span><?php esc_html_e( 'Undo last change', 'consortia-quick-find-replace' ); ?></button>
				</div>

				<p id="consortia-qfr-status" class="consortia-qfr-status"><?php esc_html_e( 'Type text in “Find” to search this document.', 'consortia-quick-find-replace' ); ?></p>
				<p class="consortia-qfr-help"><?php esc_html_e( 'Works on the current document only. Save/update the document normally after making changes.', 'consortia-quick-find-replace' ); ?></p>
			</div>
		<?php
	}

	/**
	 * Enqueue assets.
	 *
	 * @param string $hook_suffix Admin hook suffix.
	 * @return void
	 */
	public static function enqueue_assets( $hook_suffix ) {
		global $post;

		if ( 'tools_page_consortiaqfr-settings' === $hook_suffix ) {
			wp_enqueue_style(
				'consortia-qfr-admin',
				CONSORTIA_QFR_URL . 'assets/css/admin.css',
				array( 'dashicons' ),
				self::VERSION
			);
			return;
		}

		if ( ! in_array( $hook_suffix, array( 'post.php', 'post-new.php' ), true ) ) {
			return;
		}

		$screen = get_current_screen();
		if ( ! $screen || ! in_array( $screen->post_type, self::get_post_types(), true ) ) {
			return;
		}

		wp_enqueue_style(
			'consortia-qfr-admin',
			CONSORTIA_QFR_URL . 'assets/css/admin.css',
			array( 'dashicons' ),
			self::VERSION
		);

		wp_enqueue_script(
			'consortia-qfr-admin',
			CONSORTIA_QFR_URL . 'assets/js/admin.js',
			array( 'jquery' ),
			self::VERSION,
			true
		);

		wp_localize_script(
			'consortia-qfr-admin',
			'consortiaqfrAdmin',
			array(
				'isPro'    => false,
				'postId'   => $post instanceof WP_Post ? $post->ID : 0,
				'settings' => array(),
				'i18n'     => array(
					'typeToSearch'     => __( 'Type text in “Find” to search this document.', 'consortia-quick-find-replace' ),
					'noMatches'        => __( 'No matches found.', 'consortia-quick-find-replace' ),
					'readyToSearch'    => __( 'Ready to search', 'consortia-quick-find-replace' ),
					'oneMatchFound'    => __( 'Found 1 match', 'consortia-quick-find-replace' ),
					/* translators: %d: Number of matches found. */
					'matchesFound'     => __( 'Found %d matches', 'consortia-quick-find-replace' ),
					/* translators: 1: Current match number, 2: Total number of matches. */
					'matchStatus'      => __( 'Match %1$d of %2$d', 'consortia-quick-find-replace' ),
					'replacedOne'      => __( 'Replaced 1 match.', 'consortia-quick-find-replace' ),
					/* translators: %d: Number of matches replaced. */
					'replacedMany'     => __( 'Replaced %d matches.', 'consortia-quick-find-replace' ),
					'undoDone'         => __( 'Undid the last change.', 'consortia-quick-find-replace' ),
					'nothingToUndo'    => __( 'Nothing to undo.', 'consortia-quick-find-replace' ),
					'nothingToReplace' => __( 'Nothing to replace.', 'consortia-quick-find-replace' ),
					'gutenbergNote'    => __( 'Block editor support is basic in this first version: find/replace works, but visual highlighting is limited.', 'consortia-quick-find-replace' ),
				),
			)
		);
	}

	/**
	 * Get editor post types supported by the plugin.
	 *
	 * @return string[]
	 */
	private static function get_post_types() {
		/**
		 * Filter the post types where Quick Find & Replace should load.
		 *
		 * @param string[] $post_types Supported post types.
		 */
		return apply_filters( 'consortiaqfr_supported_post_types', self::$post_types );
	}
}
