<?php

namespace NewfoldLabs\WP\Module\Patterns\Library;

use NewfoldLabs\WP\Module\Patterns\Data\Brands;
use NewfoldLabs\WP\Module\Patterns\Services\PluginService;

/**
 * Admin library class
 */
final class Admin {
	/**
	 * Admin pages that require WonderBlock assets.
	 *
	 * @var array
	 */
	private static $admin_pages = array( 'page', 'post', 'page-new', 'post-new', 'site-editor' );

	/**
	 * Constructor.
	 */
	public function __construct() {
		foreach ( self::$admin_pages as $admin_page ) {
			\add_action( "load-{$admin_page}.php", array( __CLASS__, 'load_wonder_blocks' ) );
		}

		\add_action( 'init', array( PluginService::class, 'setup' ) );
		\add_action( 'init', array( __CLASS__, 'load_text_domain' ), 100 );

		\add_filter( 'load_script_translation_file', array( __CLASS__, 'load_script_translation_file' ), 10, 3 );
	}

	/**
	 * Load wonder block assets into the respective admin editor page and suppress the core patterns modal.
	 *
	 * @return void
	 */
	public static function load_wonder_blocks() {
		\add_action( 'enqueue_block_assets', array( __CLASS__, 'enqueue_block_assets' ) );
		self::register_block_patterns();
		\add_filter( 'admin_body_class', array( __CLASS__, 'add_admin_body_class' ) );
	}

	/**
	 * Enqueue block assets for iframe editor compatibility.
	 * This ensures scripts and styles are loaded in both parent window and iframe.
	 *
	 * @return void
	 */
	public static function enqueue_block_assets() {
		// Only enqueue in block editor context
		$current_screen = \get_current_screen();
		if ( ! $current_screen || ! method_exists( $current_screen, 'is_block_editor' ) || ! $current_screen->is_block_editor() ) {
			return;
		}

		$asset_file = NFD_WONDER_BLOCKS_BUILD_DIR . '/wonder-blocks.asset.php';

		if ( is_readable( $asset_file ) ) {
			$asset = include $asset_file;

			// Validate asset file structure
			if ( ! is_array( $asset ) || ! isset( $asset['dependencies'] ) || ! isset( $asset['version'] ) ) {
				return;
			}

			// Register script if not already registered
			if ( ! \wp_script_is( 'nfd-wonder-blocks', 'registered' ) ) {
				\wp_register_script(
					'nfd-wonder-blocks',
					NFD_WONDER_BLOCKS_BUILD_URL . '/wonder-blocks.js',
					array_merge( $asset['dependencies'], array() ),
					$asset['version'],
					true
				);

				\wp_localize_script(
					'nfd-wonder-blocks',
					'nfdWonderBlocks',
					array(
						'nonce'        => \wp_create_nonce( 'wp_rest' ),
						'nfdRestURL'   => \get_home_url() . '/index.php?rest_route=/nfd-wonder-blocks/v1',
						'assets'       => \esc_url( NFD_WONDER_BLOCKS_URL . '/assets' ),
						'wpVer'        => \esc_html( get_bloginfo( 'version' ) ),
						'nfdWBVersion' => \esc_html( NFD_WONDER_BLOCKS_VERSION ),
						'brand'        => Brands::get_current_brand(),
					)
				);

				\wp_set_script_translations(
					'nfd-wonder-blocks',
					'nfd-wonder-blocks',
					NFD_WONDER_BLOCKS_DIR . '/languages'
				);
			}

			// Register style if not already registered
			if ( ! \wp_style_is( 'nfd-wonder-blocks', 'registered' ) ) {
				\wp_register_style(
					'nfd-wonder-blocks',
					NFD_WONDER_BLOCKS_BUILD_URL . '/wonder-blocks.css',
					array(),
					$asset['version']
				);
			}

			\wp_enqueue_script( 'nfd-wonder-blocks' );
			\wp_enqueue_style( 'nfd-wonder-blocks' );
		}
	}

	/**
	 * Filter default WP script translations file to load the correct one
	 *
	 * @param string $file The translations file.
	 * @param string $handle Script handle.
	 * @param string $domain The strings textdomain.
	 * @return string
	 */
	public static function load_script_translation_file( $file, $handle, $domain ) {

		if ( 'nfd-wonder-blocks' === $handle ) {
			$locale = determine_locale();
			$key    = md5( 'build/' . NFD_WONDER_BLOCKS_VERSION . '/wonder-blocks.js' );
			$file   = NFD_WONDER_BLOCKS_DIR . "/languages/{$domain}-{$locale}-{$key}.json";
		}

		return $file;
	}

	/**
	 * Register Block Patterns
	 */
	public static function register_block_patterns() {

		// Disable opening default WP Patterns modal on empty pages.
		$patterns = \WP_Block_Patterns_Registry::get_instance()->get_all_registered();

		foreach ( $patterns as $pattern ) {
			if ( ! empty( $pattern['blockTypes'] ) && in_array( 'core/post-content', $pattern['blockTypes'], true ) ) {
				\unregister_block_pattern( $pattern['name'] );
				$pattern['blockTypes'] = array_diff( $pattern['blockTypes'], array( 'core/post-content' ) );
				\register_block_pattern( $pattern['name'], $pattern );
			}
		}

		// Add WonderBlocks patterns.
		$wb_patterns = Items::get( 'patterns' );

		if ( is_array( $wb_patterns ) && ! empty( $wb_patterns ) ) {

			$wb_pattern_categories = Categories::get( 'patterns' );

			// Register WonderBlocks pattern categories.
			if ( is_array( $wb_pattern_categories ) && ! empty( $wb_pattern_categories ) ) {
				foreach ( $wb_pattern_categories as $category ) {
					register_block_pattern_category(
						'wonder-blocks-' . $category['title'],
						array( 'label' => 'WonderBlocks - ' . $category['label'] )
					);
				}
			}

			foreach ( $wb_patterns as $pattern ) {

				$categories = array();

				// Build categories array.
				if ( is_array( $pattern['categories'] ) && ! empty( $pattern['categories'] ) ) {
					foreach ( $pattern['categories'] as $category ) {
						$categories[] = 'wonder-blocks-' . $category;
					}
				} elseif ( is_string( $pattern['categories'] ) ) {
					$categories[] = 'wonder-blocks-' . $pattern['categories'];
				}

				\register_block_pattern(
					'wonder-blocks/' . $pattern['title'],
					array(
						'title'       => $pattern['title'],
						'content'     => $pattern['content'],
						'description' => $pattern['title'],
						'categories'  => $categories,
					)
				);
			}
		}
	}

	/**
	 * Add custom admin class on block editor pages.
	 *
	 * @param string $classes Body classes.
	 * @return string
	 */
	public static function add_admin_body_class( $classes ) {
		$current_screen = get_current_screen();

		if ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() ) {
			$classes .= ' nfd-wb--hide-theme-patterns';
		}

		return $classes;
	}

	/**
	 * Load text domain for Module
	 *
	 * @return void
	 */
	public static function load_text_domain() {

		\load_plugin_textdomain(
			'nfd-wonder-blocks',
			false,
			NFD_WONDER_BLOCKS_DIR . '/languages'
		);

		\load_script_textdomain(
			'nfd-wonder-blocks',
			'nfd-wonder-blocks',
			NFD_WONDER_BLOCKS_DIR . '/languages'
		);
	}
}
