<?php
/**
 * RSM Editor admin UI — toolbar chrome, media row, calendar button scope.
 *
 * @package Rsmspark_Editor
 */

defined( 'ABSPATH' ) || exit;

/**
 * Rsmspark_Rich_Text_Editor_UI
 */
final class Rsmspark_Rich_Text_Editor_UI {

	/**
	 * @return void
	 */
	public static function init() {
		add_filter( 'wp_editor_settings', array( __CLASS__, 'filter_editor_settings' ), 25, 2 );
		add_filter( 'admin_body_class', array( __CLASS__, 'admin_body_class' ) );
		add_action( 'media_buttons', array( __CLASS__, 'filter_media_buttons' ), 1, 1 );
		add_action( 'media_buttons', array( __CLASS__, 'render_media_button' ), 15, 1 );
		add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ), 25 );
	}

	/**
	 * @param string $classes Admin body classes.
	 * @return string
	 */
	public static function admin_body_class( $classes ) {
		if ( self::media_row_enabled() ) {
			$classes .= ' rsmspark-rt-editor-media-row';
		}

		return $classes;
	}

	/**
	 * @param array<string, mixed> $settings  Editor settings.
	 * @param string               $editor_id Editor id.
	 * @return array<string, mixed>
	 */
	public static function filter_editor_settings( $settings, $editor_id ) {
		if ( ! self::is_profile_editor( (string) $editor_id, $settings ) ) {
			return $settings;
		}

		$settings['media_buttons'] = self::media_row_enabled() || self::use_rsm_media_picker();

		return $settings;
	}

	/**
	 * @param string $editor_id Editor id.
	 * @return void
	 */
	public static function filter_media_buttons( $editor_id ) {
		if ( ! self::is_profile_editor( (string) $editor_id ) ) {
			return;
		}

		remove_action( 'media_buttons', 'media_buttons' );

		if ( ! self::media_row_enabled() ) {
			self::remove_calendar_media_button();
			return;
		}

		if ( ! self::calendar_button_enabled() ) {
			self::remove_calendar_media_button();
		}

		if ( ! self::use_rsm_media_picker() ) {
			add_action( 'media_buttons', 'media_buttons', 10, 1 );
		}
	}

	/**
	 * @param string $editor_id Editor id.
	 * @return void
	 */
	public static function render_media_button( $editor_id ) {
		if ( ! self::is_profile_editor( (string) $editor_id ) ) {
			return;
		}

		if ( self::media_row_enabled() ) {
			return;
		}

		if ( ! self::use_rsm_media_picker() ) {
			return;
		}

		if ( ! current_user_can( 'upload_files' ) ) {
			return;
		}

		printf(
			'<button type="button" class="button rsmspark-rt-insert-media" data-editor-id="%1$s">%2$s</button>',
			esc_attr( (string) $editor_id ),
			esc_html__( 'Add Media', 'rsm-content-widgets' )
		);
	}

	/**
	 * @param string $hook Admin hook.
	 * @return void
	 */
	public static function enqueue_scripts( $hook = '' ) {
		if ( ! is_admin() || ! self::should_enqueue_editor_assets( $hook ) ) {
			return;
		}

		Rsmspark_Rich_Text_Assets::register_admin();
		Rsmspark_Rich_Text_Assets::enqueue_config_script();

		wp_enqueue_script(
			'rsmspark-rich-text-ui',
			Rsmspark_Rich_Text_Assets::ui_script_url(),
			array( 'jquery', Rsmspark_Rich_Text_Assets::CONFIG_HANDLE ),
			RSMSPARK_RICH_TEXT_VERSION,
			true
		);

		if ( self::use_rsm_media_picker() ) {
			if ( class_exists( 'RSM_Media_Picker', false ) ) {
				RSM_Media_Picker::enqueue( $hook );
				wp_enqueue_script( 'rsm-media-picker-bridge' );
			}

			wp_enqueue_script(
				'rsmspark-rich-text-media',
				Rsmspark_Rich_Text_Assets::media_script_url(),
				array( 'jquery', 'rsmspark-rich-text-ui' ),
				RSMSPARK_RICH_TEXT_VERSION,
				true
			);
		}
	}

	/**
	 * @param string               $editor_id Editor id.
	 * @param array<string, mixed> $settings  Optional editor settings context.
	 * @return bool
	 */
	public static function is_profile_editor( $editor_id, $settings = array() ) {
		$context = is_array( $settings ) ? $settings : array();
		if ( ! empty( $settings['tinymce'] ) && is_array( $settings['tinymce'] ) ) {
			$context = array_merge( $context, $settings['tinymce'] );
		}

		return '' !== Rsmspark_Rich_Text_TinyMCE::resolve_profile( $editor_id, $context );
	}

	/**
	 * @return bool
	 */
	public static function media_row_enabled() {
		if ( ! class_exists( 'Rsmspark_Content_Widgets_Settings', false ) ) {
			return false;
		}

		return Rsmspark_Content_Widgets_Settings::editor_media_row_enabled();
	}

	/**
	 * @return bool
	 */
	public static function use_rsm_media_picker() {
		if ( ! class_exists( 'Rsmspark_Content_Widgets_Settings', false ) ) {
			return false;
		}

		return Rsmspark_Content_Widgets_Settings::editor_use_rsm_media_picker();
	}

	/**
	 * @return bool
	 */
	public static function calendar_button_enabled() {
		if ( ! class_exists( 'Rsmspark_Content_Widgets_Settings', false ) ) {
			return false;
		}

		return Rsmspark_Content_Widgets_Settings::editor_calendar_button_enabled();
	}

	/**
	 * @return void
	 */
	private static function remove_calendar_media_button() {
		remove_action( 'media_buttons', array( 'RSM\Calendar\Admin', 'classic_button' ) );
	}

	/**
	 * Gutenberg: RSM Media picker bridge for block sidebar TinyMCE fields.
	 *
	 * @param string $hook Admin hook suffix.
	 * @return void
	 */
	public static function enqueue_gutenberg_media( $hook = '' ) {
		if ( ! class_exists( 'RSM_Media_Picker', false ) ) {
			if ( function_exists( 'wp_enqueue_media' ) ) {
				wp_enqueue_media();
			}
		} else {
			RSM_Media_Picker::enqueue( $hook ? $hook : 'post.php' );
		}

		Rsmspark_Rich_Text_Assets::register_admin();
		wp_enqueue_script(
			'rsmspark-rich-text-ui',
			Rsmspark_Rich_Text_Assets::ui_script_url(),
			array( 'jquery', Rsmspark_Rich_Text_Assets::CONFIG_HANDLE ),
			RSMSPARK_RICH_TEXT_VERSION,
			true
		);

		if ( self::use_rsm_media_picker() ) {
			wp_enqueue_style( 'rsm-media-picker' );
		} elseif ( function_exists( 'wp_enqueue_media' ) ) {
			wp_enqueue_media();
		}

		if ( ! self::use_rsm_media_picker() ) {
			return;
		}

		if ( class_exists( 'Rsmspark_Content_Widgets_Media_Picker', false )
			&& Rsmspark_Content_Widgets_Media_Picker::should_use() ) {
			wp_enqueue_script( 'rsm-media-picker-bridge' );
		}

		wp_enqueue_script(
			'rsmspark-rich-text-media',
			Rsmspark_Rich_Text_Assets::media_script_url(),
			array( 'jquery', 'rsmspark-rich-text-ui' ),
			RSMSPARK_RICH_TEXT_VERSION,
			true
		);
	}

	/**
	 * Block editor: RSM Media bridge only (TinyMCE stack loads on admin_enqueue_scripts).
	 *
	 * @return void
	 */
	public static function enqueue_block_editor_media_bridge() {
		if ( ! is_admin() ) {
			return;
		}

		self::enqueue_gutenberg_media( 'post.php' );
	}

	/**
	 * @param string $hook Admin hook suffix.
	 * @return bool
	 */
	private static function should_enqueue_editor_assets( $hook = '' ) {
		if ( Rsmspark_Rich_Text_TinyMCE::is_block_editor_screen() && ! wp_doing_ajax() ) {
			return false;
		}

		if ( Rsmspark_Rich_Text_TinyMCE::should_enqueue( $hook ) ) {
			return true;
		}

		if ( function_exists( 'get_current_screen' ) ) {
			$screen = get_current_screen();
			if ( $screen && in_array( $screen->base, array( 'post', 'site-editor', 'widgets' ), true ) ) {
				return true;
			}
		}

		return false;
	}
}
