<?php
/**
 * Admin asset enqueueing.
 *
 * @package Donatico\Admin
 */

declare(strict_types=1);

namespace Donatico\Admin;

/**
 * Enqueues CSS and JS only on Donatico admin pages to avoid polluting
 * the global admin asset stack.
 */
class Assets {

	/** Slug prefix shared by all plugin admin pages. */
	const PAGE_PREFIX = 'donatico';

	/**
	 * Enqueue all admin-side assets.
	 * Hooked to admin_enqueue_scripts — receives the current screen hook suffix.
	 *
	 * @param string $hook_suffix Current admin page hook suffix.
	 * @return void
	 */
	public function enqueue( string $hook_suffix ): void {
		if ( ! $this->isPluginPage( $hook_suffix ) ) {
			return;
		}

		$this->enqueueStyles( $hook_suffix );
		$this->enqueueScripts( $hook_suffix );
	}

	/**
	 * @return void
	 */
	private function enqueueStyles( string $hook_suffix = '' ): void {
		wp_enqueue_style(
			'donatico-admin',
			DONATICO_PLUGIN_URL . 'assets/css/admin.css',
			array(),
			DONATICO_VERSION
		);

		if ( 'donatico_page_donatico-upgrade' === $hook_suffix ) {
			wp_enqueue_style(
				'donatico-upgrade-page',
				DONATICO_PLUGIN_URL . 'assets/upgrade/upgrade.css',
				array(),
				DONATICO_VERSION
			);
		}

		if ( 'donatico_page_donatico-form-designer' === $hook_suffix ) {
			wp_enqueue_style(
				'donatico-upsell-form-designer',
				DONATICO_PLUGIN_URL . 'assets/upgrade/upsell-form-designer.css',
				array(),
				DONATICO_VERSION
			);
		}

		$teaser_screens = array(
			'donatico_page_donatico-campaigns-teaser',
			'donatico_page_donatico-tiers-teaser',
			'donatico_page_donatico-donors-teaser',
		);

		if ( in_array( $hook_suffix, $teaser_screens, true ) ) {
			wp_enqueue_style(
				'donatico-teasers',
				DONATICO_PLUGIN_URL . 'assets/upgrade/teasers.css',
				array(),
				DONATICO_VERSION
			);
		}
	}

	/**
	 * @return void
	 */
	private function enqueueScripts( string $hook_suffix = '' ): void {
		wp_enqueue_script(
			'donatico-admin',
			DONATICO_PLUGIN_URL . 'assets/js/admin.js',
			array( 'jquery' ),
			DONATICO_VERSION,
			true
		);

		wp_localize_script(
			'donatico-admin',
			'donaticoAdmin',
			array(
				'ajaxUrl' => admin_url( 'admin-ajax.php' ),
				'nonce'   => wp_create_nonce( 'donatico_admin' ),
				'i18n'    => array(
					'confirmDelete' => __( 'Delete this donation? This cannot be undone.', 'donatico' ),
					'copied'        => __( 'Copied!', 'donatico' ),
				),
			)
		);

		if ( 'donatico_page_donatico-upgrade' === $hook_suffix ) {
			// Freemius Checkout JS SDK (loaded from Freemius CDN).
			wp_enqueue_script(
				'freemius-checkout',
				'https://checkout.freemius.com/checkout.min.js',
				array(),
				DONATICO_VERSION,
				true
			);

			// Our handler that wires up button clicks.
			wp_enqueue_script(
				'donatico-upgrade-page',
				DONATICO_PLUGIN_URL . 'assets/upgrade/upgrade.js',
				array( 'freemius-checkout' ),
				DONATICO_VERSION,
				true
			);
		}

		if ( 'donatico_page_donatico-donations' === $hook_suffix ) {
			wp_enqueue_script(
				'donatico-upsell-prompts',
				DONATICO_PLUGIN_URL . 'assets/upgrade/upsell-prompts.js',
				array( 'jquery' ),
				DONATICO_VERSION,
				true
			);
			wp_localize_script(
				'donatico-upsell-prompts',
				'donaticoUpsell',
				array(
					'ajaxUrl' => admin_url( 'admin-ajax.php' ),
					'nonce'   => wp_create_nonce( 'donatico_upsell_dismiss' ),
				)
			);
		}
	}

	/**
	 * Return true when the current screen belongs to this plugin.
	 *
	 * WP appends "toplevel_page_" for top-level menus and
	 * "<parent>_page_<slug>" for submenus.
	 *
	 * @param string $hook_suffix
	 * @return bool
	 */
	private function isPluginPage( string $hook_suffix ): bool {
		return strpos( $hook_suffix, self::PAGE_PREFIX ) !== false;
	}
}
