<?php
/**
 * A class for generating the personalisation dialogue in an iframe.
 *
 * @since 1.0
 */

declare( strict_types=1 );
namespace FeData\FastEvents\Hooks\Shortcodes;

defined( 'ABSPATH' ) || exit;

use FeData\FastEvents\Logging\LoggerFactory;
use FeData\FastEvents\Utilities\PluginUtil;


/**
 * A class for generating the personalisation dialogue in an iframe.
 */
final class PersonaliseShortcode {

	/**
	 * OrderPageShortcode constructor.
	 *
	 * @since 3.0.0
	 *
	 * @param LoggerFactory $logger Logger.
	 */
	public function __construct( private readonly LoggerFactory $logger ) {
	}

	/**
	 * Generate the personalisation dialogue in an iframe
	 *
	 * @param mixed $attr Array of attributes.
	 *
	 * @return string
	 * @since 2.1.0
	 * @since 2.3.0 - Bootstrap 5.3.3
	 *              - iFrame resizer 5.3.3
	 * @since 2.4.0 - Bootstrap 5.3.8
	 *              - iFrame resizer 5.5.7
	 */
	public function personalise( mixed $attr ): string {
		global $wp_query;

		// Is the url-parameter 'redirect_uid' or 'ticket_ids' present or not?
		if ( ( ! isset( $wp_query->query_vars['redirect_uid'] ) && ! isset( $wp_query->query_vars['ticket_ids'] ) && ! array_key_exists( 'uid', $attr ) ) ||
			( isset( $wp_query->query_vars['ticket_ids'] ) && ! isset( $wp_query->query_vars['hash'] ) ) ) {
			return '';
		}

		// Check the hash for ids.
		if ( isset( $wp_query->query_vars['ticket_ids'] ) ) {
			$ids = implode(
				'-',
				array_map(
					'absint',
					explode( '-', sanitize_text_field( $wp_query->query_vars['ticket_ids'] ) )
				)
			);
			if ( ! hash_equals( hash_hmac( 'sha1', "update$ids", wp_salt() ), sanitize_text_field( $wp_query->query_vars['hash'] ) ) ) {
				return '';
			}
		}

		// Get a script from cdn?
		if ( ! empty( $attr ) && in_array( 'cdn', $attr, true ) ) {
			$url = '<script src="https://cdn.jsdelivr.net/npm/@iframe-resizer/parent@5.5.7"></script>'; // phpcs:ignore
		} else {
			$url = '<script src="' . PluginUtil::plugin_url() . '/assets/js/iframeResizer.min.js"></script>'; // phpcs:ignore
		}

		// Set html/javascript for iFrame resizer.
		include_once PluginUtil::plugin_path() . '/templates/personalise.php';

		/**
		 * Load the styles and scripts for the order page.
		 *
		 * @since 2.1.0
		 * @var $iframe_resizer
		 */
		return $url .
			'<p><iframe ' .
			'id="fast-events-personalisation-iframe" ' .
			'style="width:100%; height: 100vh;" ' .
			'sandbox="allow-modals allow-same-origin allow-scripts allow-downloads" ' .
			'srcdoc="' .
			htmlspecialchars( $this->load_iframe( $attr ), ENT_QUOTES ) .
			'">' .
			'</iframe></p>' .
			$iframe_resizer;
	}

	/**
	 * Load the personalisation iframe.
	 * The passed $attr variable is used in the require_once!
	 *
	 * @param mixed $attr Shortcode attributes.
	 *
	 * @since 2.1.0
	 *
	 * @return string
	 */
	private function load_iframe( mixed $attr ): string { // phpcs:ignore
		ob_start();
		include_once PluginUtil::plugin_path() . '/templates/personalise-iframe.php';
		return ob_get_clean();
	}
}
