<?php
/**
 * Admin menus.
 *
 * @package    EventKoi
 * @subpackage EventKoi\Admin
 */

namespace EventKoi\Admin;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Admin menu handler.
 */
class Menus {

	/**
	 * Init.
	 */
	public function __construct() {
		add_filter(
			'plugin_action_links_' . plugin_basename( EVENTKOI_PLUGIN_FILE ),
			array( static::class, 'add_plugin_action_links' )
		);

		add_action( 'in_admin_header', array( static::class, 'remove_admin_notices' ), 99 );
		add_filter( 'admin_body_class', array( static::class, 'admin_body_class' ), 99 );

		add_action( 'admin_menu', array( static::class, 'admin_menu' ), 10 );
		add_filter( 'custom_menu_order', array( static::class, 'custom_menu_order' ) );

		add_action( 'admin_menu', array( static::class, 'menu_order_fix' ), 999 );
		add_action( 'admin_menu_editor-menu_replaced', array( static::class, 'menu_order_fix' ), 999 );

		add_action( 'admin_head', array( static::class, 'add_menu_css' ) );

		add_action( 'wp_ajax_eventkoi_dismiss_rating', array( static::class, 'dismiss_rating' ) );
	}

	/**
	 * Add links before Deactivate in plugin row.
	 *
	 * @param array $links Existing action links.
	 * @return array Modified action links.
	 */
	public static function add_plugin_action_links( $links ) {
		// Prepend custom links.
		$custom_links = array(
			'<a href="' . esc_url( admin_url( 'admin.php?page=eventkoi#/dashboard/onboarding' ) ) . '">' . esc_html__( 'Launch Onboarding Wizard', 'eventkoi-lite' ) . '</a>',
			'<a href="' . esc_url( admin_url( 'admin.php?page=eventkoi#/dashboard' ) ) . '">' . esc_html__( 'Dashboard', 'eventkoi-lite' ) . '</a>',
			'<a href="' . esc_url( admin_url( 'admin.php?page=eventkoi#/settings' ) ) . '">' . esc_html__( 'Settings', 'eventkoi-lite' ) . '</a>',
		);

		// Put them before the default links (Activate/Deactivate/Edit).
		return array_merge( $custom_links, $links );
	}

	/**
	 * Remove all admin notices on EventKoi pages except EventKoi's own.
	 */
	public static function remove_admin_notices() {
		$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;

		if ( $screen && 'toplevel_page_eventkoi' === $screen->base ) {
			if ( apply_filters( 'eventkoi_remove_all_admin_notices', true ) ) {
				global $wp_filter;

				if ( isset( $wp_filter['admin_notices'] ) && ! empty( $wp_filter['admin_notices']->callbacks ) ) {
					foreach ( $wp_filter['admin_notices']->callbacks as $priority => $callbacks ) {
						foreach ( $callbacks as $id => $callback ) {
							unset( $wp_filter['admin_notices']->callbacks[ $priority ][ $id ] );
						}
					}
				}
			}
		}
	}

	/**
	 * Adds custom class to admin <body> tag.
	 *
	 * @param string $classes Existing admin body classes.
	 * @return string Modified classes.
	 */
	public static function admin_body_class( $classes ) {
		$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;

		if ( $screen && ( 'toplevel_page_eventkoi' === $screen->base || str_starts_with( $screen->id, 'eventkoi_' ) ) ) {
			$classes .= ' wp-eventkoi';
		}

		return $classes;
	}

	/**
	 * Register the main EventKoi admin menu and submenus.
	 */
	public static function admin_menu() {
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$page = isset( $_GET['page'] ) ? sanitize_key( $_GET['page'] ) : '';

		if ( 'eventkoi' === $page ) {
			add_filter(
				'admin_footer_text',
				function () {
					if ( get_user_meta( get_current_user_id(), 'eventkoi_dismissed_rating', true ) ) {
						return sprintf(
							__( 'Thank you for using %s!', 'eventkoi-lite' ),
							'<strong>EventKoi</strong>'
						);
					}

					$messages = array(
						__( 'Thank you for using %1$s! Please rate us %2$s to help us spread the word.', 'eventkoi-lite' ),
						__( 'Thank you for using %1$s! If you enjoy it, please leave us a %2$s rating.', 'eventkoi-lite' ),
						__( 'Thank you for using %1$s! Please consider leaving a %2$s review.', 'eventkoi-lite' ),
					);

					$index   = absint( gmdate( 'j' ) ) % count( $messages );
					$message = $messages[ $index ];
					$nonce   = wp_create_nonce( 'eventkoi_dismiss_rating' );

					return sprintf(
						$message,
						'<strong>EventKoi</strong>',
						'<a href="https://wordpress.org/support/plugin/eventkoi-lite/reviews/#new-post" target="_blank" rel="noopener noreferrer" id="eventkoi-rating-link">&#9733;&#9733;&#9733;&#9733;&#9733;</a>'
					) . "<script>document.getElementById('eventkoi-rating-link').addEventListener('click',function(){fetch(ajaxurl+'?action=eventkoi_dismiss_rating&_wpnonce={$nonce}');var p=this.closest('p');if(p)p.innerHTML='" . esc_js( sprintf( __( 'Thank you for using %s!', 'eventkoi-lite' ), '<strong>EventKoi</strong>' ) ) . "'});</script>";
				},
				11
			);
			remove_filter( 'update_footer', 'core_update_footer' );
		}

		$menu_cap = \EventKoi\Core\Permissions::ACCESS_CAP;

		add_menu_page(
			__( 'Events', 'eventkoi-lite' ),
			__( 'Events', 'eventkoi-lite' ),
			$menu_cap,
			'eventkoi',
			array( static::class, 'load_admin' ),
			static::get_admin_icon(),
			'25.8765'
		);

		$menu_items = apply_filters(
			'eventkoi_admin_menu_items',
			array(
				'dashboard' => __( 'Dashboard', 'eventkoi-lite' ),
				'events'    => __( 'Events', 'eventkoi-lite' ),
				'calendars' => __( 'Calendars', 'eventkoi-lite' ),
				'tickets'   => __( 'Ticket sales', 'eventkoi-lite' ),
				'settings'  => __( 'Settings', 'eventkoi-lite' ),
			)
		);

		foreach ( $menu_items as $slug => $label ) {
			add_submenu_page(
				'eventkoi',
				$label,
				$label,
				$menu_cap,
				"admin.php?page=eventkoi#/$slug"
			);
		}
	}

	/**
	 * AJAX handler to dismiss the rating prompt.
	 */
	public static function dismiss_rating() {
		check_ajax_referer( 'eventkoi_dismiss_rating' );
		update_user_meta( get_current_user_id(), 'eventkoi_dismissed_rating', 1 );
		wp_send_json_success();
	}

	/**
	 * Return base64 encoded SVG for the admin menu icon.
	 *
	 * @return string Data URI.
	 */
	public static function get_admin_icon() {
		$svg = 'PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxNi45MTgiIGhlaWdodD0iMjEuODkiIHZpZXdCb3g9IjAgMCAxNi45MTggMjEuODkiPjxnIHRyYW5zZm9ybT0idHJhbnNsYXRlKDAg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wwLDcuOS02OS43OCwzLjQyMiwzLjQyMiwwLDAsMCw4LjE0OS02Ny4wNDFaIiB0cmFuc2Zvcm09InRyYW5zbGF0ZSgtMC40NjcgNzYuMzU2KSIgZmlsbD0iI2ZmZmZmZiIgLz48L2c+PGcgdHJhbnNmb3JtPSJ0cmFuc2xhdGUoNy4zMDEgMjAuMDYxKSI+PHBhdGggZD0iTTM0LjU2NC0zMi41MTFhMi44MTYsMi44MTYsMCwwLDAtLjI2OSwxLjI0LDEuNDYxLDEuNDYxLDAsMCwwLC4yNjkuOTEzYy41MzUuODUyLjgxOCwxLjEzOSwxLjEsMS41OWExNS4wMDYsMTUuMDA2LDAsMCwwLDMuOCw0LjEyNXEyLjIyMywxLjYzNSwzLjU4Ljc3NGExLjU1NSwxLjU1NSwwLDAsMCwuODY1LTEuNDQ4LDMuMjM1LDMuMjM1LDAsMCwwLS42MTktMS42MjJBMTcuMTMxLDE3LjEzMSwwLDAsMCw0MS44NS0yOC42N2wtLjMzMi0uMzg2YTIwLjgwNSwyMC44MDUsMCwwLDAtMi41LTIuMjY1LDEwLjYsMTAuNiwwLDAsMC0yLjgtMS42NTZRMzQuOTM2LTMzLjQ0NywzNC41NjQtMzIuNTExWiIgdHJhbnNmb3JtPSJ0cmFuc2xhdGUoLTM0LjI5NSAzMy4xMzQpIiBmaWxsPSIjZmZmZmZmIiAvPjwvZz48L2c+PC9zdmc+';

		return 'data:image/svg+xml;base64,' . $svg;
	}

	/**
	 * Renders the EventKoi React app wrapper.
	 */
	public static function load_admin() {
		echo '<div id="eventkoi-admin" class="eventkoi-admin" style="margin-bottom:60px"></div>';
	}

	/**
	 * Allow custom menu ordering.
	 *
	 * @param bool $enabled Whether menu order is enabled.
	 * @return bool
	 */
	public static function custom_menu_order( $enabled ) {
		return $enabled || current_user_can( 'manage_options' );
	}

	/**
	 * Removes the parent EventKoi submenu item.
	 */
	public static function menu_order_fix() {
		global $submenu;

		if ( empty( $submenu['eventkoi'] ) || ! is_array( $submenu['eventkoi'] ) ) {
			return;
		}

		foreach ( $submenu['eventkoi'] as $index => $item ) {
			if ( isset( $item[2] ) && 'eventkoi' === $item[2] ) {
				unset( $submenu['eventkoi'][ $index ] );
			}
		}
	}

	/**
	 * Inline admin CSS for SVG menu icon.
	 */
	public static function add_menu_css() {
		if ( ! wp_style_is( 'eventkoi-admin-global', 'registered' ) ) {
			wp_register_style(
				'eventkoi-admin-global',
				false,
				array(),
				EVENTKOI_VERSION
			);
		}

		wp_enqueue_style( 'eventkoi-admin-global' );

		$css = '#adminmenu li.toplevel_page_eventkoi div.wp-menu-image.svg { background-size: 12px auto !important; }';

		wp_add_inline_style( 'eventkoi-admin-global', $css );
	}
}
