<?php

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

declare(strict_types=1);

namespace LolaCore\PluginBuilder\Admin;

use LolaCore\PluginBuilder\PostType\PluginPostType;

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

/**
 * Enqueues list-table JS and edit-screen CSS only on lolacore_plugin admin screens.
 */
class AdminAssets {

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

	/**
	 * @param string $hook_suffix Current admin page hook.
	 */
	public function enqueue( string $hook_suffix ): void {
		if (! current_user_can('manage_options')) {
			return;
		}

		if (! $this->isPluginBuilderScreen($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-plugin-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-plugin-builder-admin.js';
		wp_enqueue_script(
			'lolacore-plugin-builder-admin',
			$plugin_url . 'assets/js/lolacore-plugin-builder-admin.js',
			array(),
			(string) @filemtime($js_file),
			true
		);

		wp_localize_script(
			'lolacore-plugin-builder-admin',
			'lolacore_pb',
			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 generated files into wp-content/plugins. The plugin will stay inactive until you activate it.', 'lolacore'),
					'overwrite_confirm_title' => __('Overwrite installed copy', 'lolacore'),
					'overwrite_confirm_message' => __('A version is already on this site. I will replace those files. Any active copy will be deactivated first.', 'lolacore'),
					'installed_title'         => __('Plugin installed', 'lolacore'),
					'activate_now'            => __('Activate now', 'lolacore'),
					'open_plugins'            => __('Open Plugins screen', 'lolacore'),
					'close'                   => __('Close', 'lolacore'),
					'confirm'                 => __('Install', 'lolacore'),
					'overwrite'               => __('Overwrite', 'lolacore'),
					'cancel'                  => __('Cancel', 'lolacore'),
					'error_title'             => __('Installation failed', 'lolacore'),
					'activate_failed_title'   => __('Activation failed', 'lolacore'),
					'activated_title'         => __('Plugin activated', 'lolacore'),
					'working'                 => __('Working…', 'lolacore'),
					'playground_attest_title'   => __('Check your plugin in Playground', 'lolacore'),
					'playground_attest_message' => __('Check the settings page and 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'),
				),
			)
		);
	}

	/**
	 * LolaCore-styled modals for install / activate flows (library list table).
	 */
	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-' . PluginPostType::POST_TYPE) {
			return;
		}

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

	private function isPluginBuilderScreen( 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 === PluginPostType::POST_TYPE) {
				return true;
			}
		}

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

}
