<?php
/**
 * Front-end and admin asset loading.
 *
 * @package Rsmspark_Content_Widgets
 */

defined( 'ABSPATH' ) || exit;

/**
 * Rsmspark_Content_Widgets_Assets
 *
 * Refactored in compat branch - Phase 0, Phase 3 per-widget enqueue
 */
class Rsmspark_Content_Widgets_Assets {

	/**
	 * Whether shared bundle was enqueued this request.
	 *
	 * @var bool
	 */
	private static $shared_enqueued = false;

	/**
	 * Widget ids that received widget-specific assets.
	 *
	 * @var array<string, bool>
	 */
	private static $widget_assets_enqueued = array();

	/**
	 * @return void
	 */
	public static function init() {
		add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_front' ), 20 );
		add_action( 'wp_footer', array( __CLASS__, 'enqueue_front_late' ), 1 );
		add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_admin' ) );
	}

	/**
	 * @return void
	 */
	public static function enqueue_front() {
		$settings = Rsmspark_Content_Widgets_Settings::get();
		$mode     = (string) ( $settings['load_assets'] ?? 'conditional' );

		if ( 'never' === $mode || ! empty( $settings['disable_all_styling'] ) ) {
			return;
		}

		if ( ! empty( $settings['per_widget_assets'] ) ) {
			return;
		}

		if ( 'conditional' === $mode && ! Rsmspark_Content_Widgets_Render::shared_assets_needed() ) {
			return;
		}

		self::enqueue_shared();
		self::enqueue_needed_widget_assets();
	}

	/**
	 * Enqueue when widgets rendered after wp_enqueue_scripts (e.g. block output).
	 *
	 * @return void
	 */
	public static function enqueue_front_late() {
		$settings = Rsmspark_Content_Widgets_Settings::get();
		if ( ! empty( $settings['disable_all_styling'] ) || ! Rsmspark_Content_Widgets_Render::shared_assets_needed() ) {
			return;
		}

		if ( empty( $settings['per_widget_assets'] ) ) {
			self::enqueue_shared();
		}
		self::enqueue_needed_widget_assets();
	}

	/**
	 * Enqueue shared + widget-specific assets for a registry id.
	 *
	 * @param string $registry_id Widget registry id.
	 * @return void
	 */
	public static function enqueue_for_widget( $registry_id ) {
		$settings = Rsmspark_Content_Widgets_Settings::get();
		$mode     = (string) ( $settings['load_assets'] ?? 'conditional' );

		if ( 'never' === $mode || ! empty( $settings['disable_all_styling'] ) ) {
			return;
		}

		if ( empty( $settings['per_widget_assets'] ) ) {
			self::enqueue_shared();
		}
		self::enqueue_widget_assets( $registry_id );
	}

	/**
	 * @param string $hook Admin hook.
	 * @return void
	 */
	public static function enqueue_admin( $hook ) {
		$rsm_hooks = array( 'post.php', 'post-new.php', 'widgets.php', 'toplevel_page_rsmspark-content-widgets' );
		if ( false !== strpos( (string) $hook, 'rsmspark-buttons' ) ) {
			$rsm_hooks[] = $hook;
		}
		if ( false !== strpos( (string) $hook, 'rsm-content-widgets' ) ) {
			$rsm_hooks[] = $hook;
		}
		if ( ! in_array( $hook, $rsm_hooks, true ) && false === strpos( (string) $hook, 'siteorigin' ) && false === strpos( (string) $hook, 'so_' ) ) {
			return;
		}

		Rsmspark_Content_Widgets_Assets::enqueue_export_json_ui_assets();

		wp_enqueue_style(
			'rsmspark-content-widgets-admin',
			RSMSPARK_CONTENT_WIDGETS_URL . 'assets/css/admin-widgets.css',
			array(),
			RSMSPARK_CONTENT_WIDGETS_VERSION
		);

		if ( false !== strpos( (string) $hook, 'siteorigin' ) || false !== strpos( (string) $hook, 'page-builder' ) || false !== strpos( (string) $hook, 'so_' ) || in_array( $hook, array( 'post.php', 'post-new.php' ), true ) ) {
			if ( class_exists( 'Rsmspark_Rich_Text_Assets', false ) ) {
				Rsmspark_Rich_Text_Assets::register_admin();
			}
		}

		if ( false !== strpos( (string) $hook, 'siteorigin' ) || false !== strpos( (string) $hook, 'page-builder' ) || false !== strpos( (string) $hook, 'so_' ) ) {
			wp_enqueue_style( 'dashicons' );
			self::enqueue_widget_assets( 'buttons' );
		}
	}

	/**
	 * @return void
	 */
	public static function enqueue_shared() {
		if ( self::$shared_enqueued ) {
			return;
		}

		$settings = Rsmspark_Content_Widgets_Settings::get();
		if ( ! empty( $settings['disable_all_styling'] ) ) {
			return;
		}

		self::$shared_enqueued = true;

		wp_enqueue_style(
			'rsmspark-content-widgets',
			RSMSPARK_CONTENT_WIDGETS_URL . 'assets/css/rsm-content-widgets.css',
			array(),
			RSMSPARK_CONTENT_WIDGETS_VERSION
		);

		wp_enqueue_script(
			'rsmspark-content-widgets',
			RSMSPARK_CONTENT_WIDGETS_URL . 'assets/js/rsm-content-widgets.js',
			array(),
			RSMSPARK_CONTENT_WIDGETS_VERSION,
			true
		);
	}

	/**
	 * Enqueue assets registered for widgets rendered on this request.
	 *
	 * @return void
	 */
	public static function enqueue_needed_widget_assets() {
		foreach ( array_keys( Rsmspark_Content_Widgets_Render::assets_needed() ) as $registry_id ) {
			self::enqueue_widget_assets( $registry_id );
		}
	}

	/**
	 * Widget-specific styles/scripts (features bundle, dashicons, etc.).
	 *
	 * @param string $registry_id Widget registry id.
	 * @return void
	 */
	public static function enqueue_widget_assets( $registry_id ) {
		$settings = Rsmspark_Content_Widgets_Settings::get();
		if ( ! empty( $settings['disable_all_styling'] ) ) {
			return;
		}

		$id = sanitize_key( (string) $registry_id );
		if ( '' === $id || ! empty( self::$widget_assets_enqueued[ $id ] ) ) {
			return;
		}

		self::$widget_assets_enqueued[ $id ] = true;

		switch ( $id ) {
			case 'features':
				wp_enqueue_style( 'dashicons' );
				wp_enqueue_style(
					'rsmspark-content-widgets-features-fontawesome',
					RSMSPARK_CONTENT_WIDGETS_URL . 'assets/vendor/fontawesome/css/all.min.css',
					array(),
					'6.5.2'
				);
				wp_enqueue_style(
					'rsmspark-content-widgets-features',
					RSMSPARK_CONTENT_WIDGETS_URL . 'assets/css/rsm-features.css',
					array( 'rsmspark-content-widgets-features-fontawesome', 'dashicons', 'rsmspark-content-widgets' ),
					RSMSPARK_CONTENT_WIDGETS_VERSION
				);
				wp_enqueue_script(
					'rsmspark-content-widgets-cw-modal',
					RSMSPARK_CONTENT_WIDGETS_URL . 'assets/js/rsm-cw-modal.js',
					array(),
					RSMSPARK_CONTENT_WIDGETS_VERSION,
					true
				);
				wp_enqueue_script(
					'rsmspark-content-widgets-features',
					RSMSPARK_CONTENT_WIDGETS_URL . 'assets/js/rsm-features.js',
					array( 'jquery', 'rsmspark-content-widgets-cw-modal' ),
					RSMSPARK_CONTENT_WIDGETS_VERSION,
					true
				);
				break;

			case 'footer':
				wp_enqueue_style( 'dashicons' );
				wp_enqueue_style(
					'rsmspark-content-widgets-footer-fontawesome',
					RSMSPARK_CONTENT_WIDGETS_URL . 'assets/vendor/fontawesome/css/all.min.css',
					array(),
					'6.5.2'
				);
				wp_enqueue_style(
					'rsmspark-content-widgets-footer',
					RSMSPARK_CONTENT_WIDGETS_URL . 'assets/css/rsm-footer.css',
					array( 'rsmspark-content-widgets-footer-fontawesome', 'dashicons', 'rsmspark-content-widgets' ),
					RSMSPARK_CONTENT_WIDGETS_VERSION
				);
				break;

			case 'icon_box_grid':
				wp_enqueue_style( 'dashicons' );
				break;

			case 'image':
				if ( class_exists( 'Rsmspark_Content_Widgets_Widget_Image', false )
					&& Rsmspark_Content_Widgets_Widget_Image::needs_lightbox_assets() ) {
					wp_enqueue_script(
						'rsmspark-content-widgets-cw-modal',
						RSMSPARK_CONTENT_WIDGETS_URL . 'assets/js/rsm-cw-modal.js',
						array(),
						RSMSPARK_CONTENT_WIDGETS_VERSION,
						true
					);
					wp_enqueue_script(
						'rsmspark-content-widgets-image-lightbox',
						RSMSPARK_CONTENT_WIDGETS_URL . 'assets/js/rsm-cw-image-lightbox.js',
						array( 'jquery', 'rsmspark-content-widgets-cw-modal' ),
						RSMSPARK_CONTENT_WIDGETS_VERSION,
						true
					);
				}
				break;

			case 'buttons':
				wp_enqueue_style(
					'rsmspark-buttons',
					RSMSPARK_CONTENT_WIDGETS_URL . 'assets/css/rsm-buttons.css',
					array(),
					RSMSPARK_CONTENT_WIDGETS_VERSION
				);
				break;

			case 'event_countdown':
				wp_enqueue_style(
					'rsmspark-content-widgets-event-countdown',
					RSMSPARK_CONTENT_WIDGETS_URL . 'assets/css/rsm-event-countdown.css',
					array(),
					RSMSPARK_CONTENT_WIDGETS_VERSION
				);
				if ( ! empty( $settings['per_widget_assets'] ) && ! self::$shared_enqueued ) {
					wp_enqueue_script(
						'rsmspark-content-widgets',
						RSMSPARK_CONTENT_WIDGETS_URL . 'assets/js/rsm-content-widgets.js',
						array(),
						RSMSPARK_CONTENT_WIDGETS_VERSION,
						true
					);
				}
				wp_enqueue_script(
					'rsmspark-content-widgets-event-countdown',
					RSMSPARK_CONTENT_WIDGETS_URL . 'assets/js/rsm-event-countdown.js',
					array( 'rsmspark-content-widgets' ),
					RSMSPARK_CONTENT_WIDGETS_VERSION,
					true
				);
				break;

			case 'headline':
				if ( class_exists( 'Rsmspark_Rich_Text_Assets', false ) ) {
					Rsmspark_Rich_Text_Assets::enqueue_front();
				}
				break;
		}

		do_action( 'rsmspark_content_widgets_enqueue_widget_assets', $id );
	}

	/**
	 * Shared floating JSON export panel (admin + block editor).
	 *
	 * @return void
	 */
	public static function register_export_json_ui_assets() {
		if ( wp_script_is( 'rsmspark-cw-export-json-ui', 'registered' ) ) {
			return;
		}

		wp_register_style(
			'rsmspark-cw-export-json-ui',
			RSMSPARK_CONTENT_WIDGETS_URL . 'assets/css/rsm-cw-export-json-ui.css',
			array(),
			RSMSPARK_CONTENT_WIDGETS_VERSION
		);

		wp_register_script(
			'rsmspark-cw-export-json-ui',
			RSMSPARK_CONTENT_WIDGETS_URL . 'assets/js/rsm-cw-export-json-ui.js',
			array(),
			RSMSPARK_CONTENT_WIDGETS_VERSION,
			true
		);

		wp_localize_script(
			'rsmspark-cw-export-json-ui',
			'rsmsparkCwExportJsonUi',
			array(
				'i18n' => array(
					'panelLabel' => __( 'RSM JSON export', 'rsm-content-widgets' ),
					'title'      => __( 'JSON export ready', 'rsm-content-widgets' ),
					'help'       => __( 'Copied to your clipboard. Select the JSON below to copy again or save as a file.', 'rsm-content-widgets' ),
					'widgetAreaNote' => __( 'Appearance → Widgets: export always copies to your clipboard. Paste into Import JSON on a page, post, or block editor widget — inline preview inside the footer widget form may not show when Gutenberg blocks are enabled for widget areas.', 'rsm-content-widgets' ),
					'close'      => __( 'Close', 'rsm-content-widgets' ),
				),
			)
		);
	}

	/**
	 * @return void
	 */
	public static function enqueue_export_json_ui_assets() {
		self::register_export_json_ui_assets();
		wp_enqueue_style( 'rsmspark-cw-export-json-ui' );
		wp_enqueue_script( 'rsmspark-cw-export-json-ui' );
	}
}
