<?php
/**
 * Register Admin page and features.
 *
 * @package WPPluginCrazyDomains
 */

namespace CrazyDomains;

use CrazyDomains\Data;
use function NewfoldLabs\WP\Module\Features\isEnabled;

/**
 * \CrazyDomains\Admin
 */
final class Admin {

	/**
	 * Register functionality using WordPress Actions.
	 */
	public function __construct() {
		/* Add Page to WordPress Admin Menu. */
		\add_action( 'admin_menu', array( __CLASS__, 'page' ) );
		/* Load Page Scripts & Styles. */
		\add_action( 'admin_enqueue_scripts', array( __CLASS__, 'assets' ), 1 );
		/* Add Links to WordPress Plugins list item. */
		\add_filter( 'plugin_action_links_wp-plugin-crazy-domains/wp-plugin-crazy-domains.php', array( __CLASS__, 'actions' ) );
		/* Add inline style to hide subnav link */
		\add_action( 'admin_head', array( __CLASS__, 'admin_nav_style' ) );
		/* Add runtime for data store */
		\add_filter( 'newfold_runtime', array( __CLASS__, 'add_to_runtime' ) );
		\add_action( 'update_option_WPLANG', array( __CLASS__, 'clear_transient_on_language_change' ), 10, 2 );
	}

	/**
	 * Add to runtime
	 *
	 * @param array $sdk - runtime properties from module
	 *
	 * @return array
	 */
	public static function add_to_runtime( $sdk ) {
		include_once CRAZYDOMAINS_PLUGIN_DIR . '/inc/Data.php';
		return array_merge( $sdk, Data::runtime() );
	}

	/**
	 * Subpages to register with add_submenu_page().
	 * Modules should use `nfd_plugin_subnav` filter to add their own subnav items
	 *
	 * Order or array items determines menu order.
	 *
	 * @return array
	 */
	public static function plugin_subpages() {

		return array(
			'crazy-domains#/home'        => __( 'Home', 'wp-plugin-crazy-domains' ),
			'crazy-domains#/marketplace' => __( 'Marketplace', 'wp-plugin-crazy-domains' ),
			'crazy-domains#/settings'    => __( 'Settings', 'wp-plugin-crazy-domains' ),
			'crazy-domains#/help'        => __( 'Help', 'wp-plugin-crazy-domains' ),
		);
	}

	/**
	 * Add inline script to admin screens
	 *  - hide extra link in subnav
	 */
	public static function admin_nav_style() {
		echo '<style>';
		echo 'li#toplevel_page_crazy-domains a.toplevel_page_crazy-domains div.wp-menu-image.svg { transition: fill 0.15s; background-size: 24px auto !important; }';
		echo 'ul#adminmenu a.toplevel_page_crazy-domains.wp-has-current-submenu:after, ul#adminmenu>li#toplevel_page_crazy-domains.current>a.current:after { border-right-color: #fff !important; }';
		echo 'li#toplevel_page_crazy-domains > ul > li.wp-first-item { display: none !important; }';
		echo '#wp-toolbar #wp-admin-bar-crazydomains-coming_soon .ab-item { padding: 0; }';
		echo '</style>';
	}

	/**
	 * Add WordPress Page to Appearance submenu.
	 *
	 * @return void
	 */
	public static function page() {
		$crazydomainscom = 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjQiIGhlaWdodD0iMjQiIHZpZXdCb3g9IjAgMCAyNCAyNCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHBhdGggZD0iTTIxLjMzNzEgNy41MzkzM0MyMC42OTY2IDcuMTM0ODMgMTkuODUzOSA2LjkzMjU5IDE4LjgwOSA2LjkzMjU5SDE0LjM1OTZMMTAuNjUxNyAxOC45MzI2SDE1LjAzMzdDMTYuMDc4NyAxOC45MzI2IDE3LjA1NjIgMTguNzMwMyAxNy45NjYzIDE4LjM5MzNDMTguODc2NCAxOC4wMjI1IDE5LjY4NTQgMTcuNTE2OSAyMC40MjcgMTYuODQyN0MyMS4xMzQ4IDE2LjIwMjIgMjEuNzA3OSAxNS4zOTMzIDIyLjE3OTggMTQuNTE2OUMyMi42NTE3IDEzLjY0MDUgMjIuODg3NiAxMi42NjI5IDIyLjk4ODggMTEuNTg0M0MyMy4wODk5IDEwLjcwNzkgMjIuOTg4OCA5Ljg5ODg4IDIyLjY4NTQgOS4yMjQ3MkMyMi40NDk0IDguNDgzMTUgMjIuMDExMiA3Ljk0MzgyIDIxLjMzNzEgNy41MzkzM1pNMTkuNjg1NCAxMi4wNTYyQzE5LjY1MTcgMTIuNTk1NSAxOS40ODMxIDEzLjEwMTEgMTkuMzE0NiAxMy42MDY3QzE5L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A0Ljk2NjI5QzQuMzgyMDMgNS4zMDMzNyAxLjU4NDI3IDguMTAxMTMgMC45Nzc1MzEgMTEuNjQwNUMwLjcwNzg2OCAxMy4xMjM2IDEuMDExMjQgMTQuNDcxOSAxLjg1Mzk0IDE1LjQ4MzFDMi42MjkyMiAxNi40MjcgMy44NzY0MSAxNi45NjYzIDUuMjU4NDMgMTYuOTY2M0M1LjQ5NDM5IDE2Ljk2NjMgNS43MzAzNCAxNi45MzI2IDUuOTY2MyAxNi45MzI2QzguNzY0MDUgMTYuNTk1NSAxMC40MTU3IDE1LjI0NzIgMTEuMTIzNiAxMi44MjAyTDcuOTg4NzcgMTIuNjUxN0M3Ljg1Mzk0IDEzLjE1NzMgNy4yODA5IDE0LjEwMTEgNS44OTg4OCAxNC4yMDIyWiIgZmlsbD0id2hpdGUiLz4KPC9zdmc+';

		\add_menu_page(
			__( 'Crazy Domains', 'wp-plugin-crazy-domains' ),
			__( 'Crazy Domains', 'wp-plugin-crazy-domains' ),
			'manage_options',
			'crazy-domains',
			array( __CLASS__, 'render' ),
			$crazydomainscom,
			0
		);

		// Add subpages to menu
		foreach ( self::plugin_subpages() as $route => $title ) {
			\add_submenu_page(
				'crazy-domains',
				$title,
				$title,
				'manage_options',
				$route,
				array( __CLASS__, 'render' )
			);
		}
	}

	/**
	 * Render DOM element for React to load onto.
	 *
	 * @return void
	 */
	public static function render() {
		global $wp_version;
		if ( ! function_exists( 'get_plugin_data' ) ) {
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
		}
		$plugin_data = get_plugin_data( CRAZYDOMAINS_PLUGIN_FILE );

		echo '<!-- Crazy Domains -->' . PHP_EOL;

		if ( version_compare( $wp_version, '5.4', '>=' ) ) {
			echo '<div id="wppcd-app" class="wppcd wppcd_app"></div>' . PHP_EOL;
			echo '<div id="nfd-next-steps-portal" style="display:none"></div>' . PHP_EOL;
			// Render bootstrap containers for modules that need portals
			// Only enabled features get their containers rendered
			$features_with_portals = array( 'performance', 'coming-soon', 'staging' );
			foreach ( $features_with_portals as $feature ) {
				if ( function_exists( 'NewfoldLabs\WP\Module\Features\isEnabled' ) &&
					\NewfoldLabs\WP\Module\Features\isEnabled( $feature ) ) {
					$portal_id = 'nfd-' . $feature . '-portal';
					echo '<div id="' . esc_attr( $portal_id ) . '" style="display:none"></div>' . PHP_EOL;
				}
			}
		} else {
			// fallback messaging for WordPress older than 5.4.
			echo '<div id="wppcd-app" class="wppcd wppcd_app">' . PHP_EOL;
			echo '<header class="wppcd-header" style="min-height: 90px; padding: 1rem; margin-bottom: 1.5rem;"><div class="wppcd-header-inner"><div class="wppcd-logo-wrap">' . PHP_EOL;
			echo '<img src="' . esc_url( CRAZYDOMAINS_PLUGIN_URL . 'assets/svg/crazydomains-logo.svg' ) . '" alt="Crazy Domains logo" />' . PHP_EOL;
			echo '</div></div></header>' . PHP_EOL;
			echo '<div class="wrap">' . PHP_EOL;
			echo '<div class="card" style="margin-left: 20px;"><h2 class="title">' . esc_html__( 'Please update to a newer WordPress version.', 'wp-plugin-crazy-domains' ) . '</h2>' . PHP_EOL;
			echo '<p>' . esc_html__( 'There are new WordPress components which this plugin requires in order to render the interface.', 'wp-plugin-crazy-domains' ) . '</p>' . PHP_EOL;
			echo '<p><a href="' . esc_url( admin_url( 'update-core.php' ) ) . '" class="button component-button is-primary button-primary" variant="primary">' . esc_html__( 'Please update now', 'wp-plugin-crazy-domains' ) . '</a></p>' . PHP_EOL;
			echo '</div></div></div>' . PHP_EOL;
		}

		echo '<!-- /Crazy Domains -->' . PHP_EOL;
	}

	/**
	 * Load Page Scripts & Styles.
	 *
	 * @return void
	 */
	public static function assets() {
		$asset_file = CRAZYDOMAINS_BUILD_DIR . '/index.asset.php';

		if ( is_readable( $asset_file ) ) {
			$asset = include_once $asset_file;
		} else {
			return;
		}

		\wp_register_script(
			'nfd-portal-registry',
			CRAZYDOMAINS_BUILD_URL . '/portal-registry.js',
			array( 'wp-components', 'wp-element' ),
			$asset['version'],
			true
		);

		\wp_register_script(
			'crazy-domains-script',
			CRAZYDOMAINS_BUILD_URL . '/index.js',
			array_merge( $asset['dependencies'], array( 'newfold-features', 'nfd-runtime', 'nfd-portal-registry' ) ),
			$asset['version'],
			true
		);

		\wp_register_style(
			'crazy-domains-style',
			CRAZYDOMAINS_BUILD_URL . '/index.css',
			array( 'wp-components' ),
			$asset['version']
		);

		$screen = get_current_screen();
		if ( isset( $screen->id ) && false !== strpos( $screen->id, 'crazy-domains' ) ) {
			\wp_enqueue_script( 'crazy-domains-script' );
			\wp_enqueue_style( 'crazy-domains-style' );
		}

		\add_action( 'admin_footer_text', array( __CLASS__, 'add_brand_to_admin_footer' ) );
	}

	/**
	 * Add Links to WordPress Plugins list item for Crazy Domains.
	 *
	 * @param  array $actions - array of action links for Plugin row item.
	 * @return array
	 */
	public static function actions( $actions ) {
		return array_merge(
			array(
				'overview' => '<a href="' . \apply_filters( 'nfd_build_url', admin_url( 'admin.php?page=crazy-domains#/home' ) ) . '">' . __( 'Home', 'wp-plugin-crazy-domains' ) . '</a>',
				'settings' => '<a href="' . \apply_filters( 'nfd_build_url', admin_url( 'admin.php?page=crazy-domains#/settings' ) ) . '">' . __( 'Settings', 'wp-plugin-crazy-domains' ) . '</a>',
			),
			$actions
		);
	}

	/**
	 * Filter WordPress Admin Footer Text "Thank you for creating with..."
	 *
	 * @param string $footer_text footer text
	 * @return string
	 */
	public static function add_brand_to_admin_footer( $footer_text ) {

		$wordpress_url = '<a href="' . apply_filters( 'nfd_build_url', 'https://wordpress.org/', array( 'source' => 'crazy_admin_footer' ) ) . '">WordPress</a>';
		$crazy_url     = '<a href="' . apply_filters( 'nfd_build_url', 'https://crazydomains.com/about', array( 'source' => 'crazy_admin_footer' ) ) . '">Crazy Domains</a>';

		// translators: %1$s is the WordPress URL, %2$s is the Crazy domains URL.
		$footer_text = sprintf( \__( 'Thank you for creating with %1$s and %2$s.', 'wp-plugin-crazy-domains' ), $wordpress_url, $crazy_url );
		return $footer_text;
	}

	/**
	 * Clears a specific transient when the WordPress admin language setting is changed.
	 *
	 * This function hooks into the `update_option_WPLANG` action to detect when
	 * the site language is updated in the WordPress settings. If a change is detected,
	 * it deletes the specified transient to ensure fresh data is retrieved.
	 *
	 * @param string $old_value The previous language setting (e.g., 'en_US').
	 * @param string $new_value The new language setting (e.g., 'fr_FR').
	 */
	public static function clear_transient_on_language_change( $old_value, $new_value ) {
		// Check if the language has actually changed
		if ( $old_value !== $new_value ) {
			// Delete the transients to refresh cached data
			delete_transient( 'newfold_marketplace' );
			delete_transient( 'newfold_notifications' );
			delete_transient( 'newfold_solutions' );
		}
	}
} // END \CrazyDomains\Admin
