<?php

/**
 * Conditional admin assets for the Block Builder library screens.
 *
 * @package LolaCore\BlockBuilder\Admin
 * @license GPL-2.0-or-later
 */

declare(strict_types=1);

namespace LolaCore\BlockBuilder\Admin;

use LolaCore\BlockBuilder\PostType\BlockPostType;

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

/**
 * Enqueues list-table JS on lolacore_block admin screens.
 */
class AdminAssets {

	public function register(): void {
		add_action('admin_enqueue_scripts', array($this, 'enqueue'));
		add_action('admin_footer', array($this, 'renderModals'));
	}

	public function enqueue( string $hook_suffix ): void {
		if (! current_user_can('manage_options')) {
			return;
		}

		if (! $this->isBlockBuilderScreen($hook_suffix)) {
			return;
		}

		if (! defined('LOLACORE_PLUGIN_DIR') || ! defined('LOLACORE_PLUGIN_URL')) {
			return;
		}

		$plugin_url = LOLACORE_PLUGIN_URL;
		$plugin_dir = LOLACORE_PLUGIN_DIR;

		$css_file = $plugin_dir . 'assets/css/lolacore-plugin-builder-admin.css';
		wp_enqueue_style(
			'lolacore-block-builder-admin',
			$plugin_url . 'assets/css/lolacore-plugin-builder-admin.css',
			array('lolacore-admin-style'),
			(string) @filemtime($css_file)
		);

		$js_file = $plugin_dir . 'assets/js/lolacore-block-builder-admin.js';
		wp_enqueue_script(
			'lolacore-block-builder-admin',
			$plugin_url . 'assets/js/lolacore-block-builder-admin.js',
			array(),
			(string) @filemtime($js_file),
			true
		);

		wp_localize_script(
			'lolacore-block-builder-admin',
			'lolacore_bb',
			array(
				'ajax_url' => admin_url('admin-ajax.php', 'relative'),
				'nonce'    => wp_create_nonce('lolacore_admin_nonce'),
				'i18n'     => array(
					'install_confirm_title'     => __('Install on this site', 'lolacore'),
					'install_confirm_message'   => __('I will copy the block files into wp-content/lolacore-blocks. The block will appear in the inserter right away.', 'lolacore'),
					'overwrite_confirm_title'   => __('Overwrite installed copy', 'lolacore'),
					'overwrite_confirm_message' => __('A version is already on this site. I will replace those files.', 'lolacore'),
					'installed_title'           => __('Block installed', 'lolacore'),
					'uninstall_confirm_title'   => __('Uninstall from this site', 'lolacore'),
					'uninstall_confirm_message' => __('I will remove the block files from wp-content/lolacore-blocks. The block will disappear from the inserter. The library entry stays in the catalog.', 'lolacore'),
					'uninstalled_title'         => __('Block uninstalled', 'lolacore'),
					'uninstall_confirm'         => __('Uninstall', 'lolacore'),
					'close'                     => __('Close', 'lolacore'),
					'confirm'                   => __('Install', 'lolacore'),
					'overwrite'                 => __('Overwrite', 'lolacore'),
					'cancel'                    => __('Cancel', 'lolacore'),
					'error_title'               => __('Installation failed', 'lolacore'),
					'working'                   => __('Working…', 'lolacore'),
					'playground_attest_title'   => __('Check your block in Playground', 'lolacore'),
					'playground_attest_message' => __('Insert the block in the editor and check the front end. When it looks right, install it on your site.', 'lolacore'),
					'playground_attest_ready'     => __('Install on my site', 'lolacore'),
					'playground_attest_issue'     => __("Something's not right", 'lolacore'),
				),
			)
		);
	}

	public function renderModals(): void {
		if (! current_user_can('manage_options')) {
			return;
		}

		$screen = function_exists('get_current_screen') ? get_current_screen() : null;
		if (! $screen instanceof \WP_Screen || $screen->id !== 'edit-' . BlockPostType::POST_TYPE) {
			return;
		}

		?>
		<div id="lc-bb-modal" class="lc-modal-overlay" style="display: none;" role="dialog" aria-modal="true" aria-labelledby="lc-bb-modal-title">
			<div class="lc-modal-card">
				<div class="lc-modal-header lc-pb-modal-header" id="lc-bb-modal-header">
					<h3 id="lc-bb-modal-title"><?php esc_html_e('Confirm', 'lolacore'); ?></h3>
				</div>
				<div class="lc-modal-body">
					<p id="lc-bb-modal-message"></p>
				</div>
				<div class="lc-modal-footer" id="lc-bb-modal-footer">
					<button type="button" class="lc-action-btn ghost" id="lc-bb-modal-cancel"><?php esc_html_e('Cancel', 'lolacore'); ?></button>
					<button type="button" class="lc-action-btn primary" id="lc-bb-modal-primary"><?php esc_html_e('Confirm', 'lolacore'); ?></button>
				</div>
			</div>
		</div>
		<?php
	}

	private function isBlockBuilderScreen( string $hook_suffix ): bool {
		if ($hook_suffix === 'edit.php' || $hook_suffix === 'post.php' || $hook_suffix === 'post-new.php') {
			$screen = function_exists('get_current_screen') ? get_current_screen() : null;
			if ($screen instanceof \WP_Screen && $screen->post_type === BlockPostType::POST_TYPE) {
				return true;
			}
		}

		return $hook_suffix === 'edit-' . BlockPostType::POST_TYPE;
	}
}
