<?php
/**
 * Shortcode: [capsquery_facial_authentication]
 *
 * Usage:
 *   [capsquery_facial_authentication]
 *   [capsquery_facial_authentication redirect="https://example.com/dashboard"]
 *   [capsquery_facial_authentication button_label="Sign in with Face"]
 */

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

class Capsq_Shortcode {

	private static $shortcode_present = false;
	private static $redirect_url = '';

	public function __construct() {
		add_shortcode( 'capsquery_facial_authentication', array( $this, 'render' ) );
		add_filter( 'the_posts', array( $this, 'detect_shortcode' ) );
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) );
		add_action( 'wp_footer', array( $this, 'render_modal_html' ) );
	}

	/**
	 * Detect shortcode before enqueue runs.
	 *
	 * @param WP_Post[] $posts Posts.
	 * @return WP_Post[]
	 */
	public function detect_shortcode( $posts ) {
		if ( empty( $posts ) ) {
			return $posts;
		}
		foreach ( $posts as $post ) {
			if ( is_singular() && has_shortcode( $post->post_content, 'capsquery_facial_authentication' ) ) {
				self::$shortcode_present = true;
				break;
			}
		}
		return $posts;
	}

	public function enqueue_assets() {
		if ( is_user_logged_in() ) {
			return;
		}

		if ( ! Capsq_Helpers::is_enabled() || ! Capsq_Login::has_enrolled_users() ) {
			return;
		}

		wp_enqueue_style(
			'capsq-style',
			CAPSQ_PLUGIN_URL . 'assets/css/capsq-style.css',
			array(),
			capsq_asset_version( 'assets/css/capsq-style.css' )
		);

		wp_enqueue_script(
			'capsq-api',
			CAPSQ_PLUGIN_URL . 'assets/vendor/face-api/face-api.min.js',
			array(),
			capsq_asset_version( 'assets/vendor/face-api/face-api.min.js' ),
			true
		);

		wp_enqueue_script(
			'capsq-login',
			CAPSQ_PLUGIN_URL . 'assets/js/capsq-login.js',
			array(),
			capsq_asset_version( 'assets/js/capsq-login.js' ),
			true
		);

		wp_enqueue_script(
			'capsq-shortcode',
			CAPSQ_PLUGIN_URL . 'assets/js/capsq-shortcode.js',
			array(),
			capsq_asset_version( 'assets/js/capsq-shortcode.js' ),
			true
		);

		wp_localize_script(
			'capsq-shortcode',
			'CAPSQ_DATA',
			array(
				'restUrl'   => esc_url_raw( rest_url( 'capsquery-facial-authentication/v1' ) ),
				'modelsUrl'  => esc_url_raw( rest_url( 'capsquery-facial-authentication/v1/models' ) ),
			)
		);
	}

	/**
	 * @param array $atts Shortcode attributes.
	 * @return string
	 */
	public function render( $atts ) {
		self::$shortcode_present = true;

		if ( is_user_logged_in() ) {
			return '';
		}

		if ( ! Capsq_Helpers::is_enabled() ) {
			return '';
		}

		if ( ! Capsq_Login::has_enrolled_users() ) {
			return '';
		}

		$atts = shortcode_atts(
			array(
				'button_label' => __( 'Login with Face', 'capsquery-facial-authentication' ),
				'redirect'     => admin_url(),
			),
			$atts,
			'capsquery_facial_authentication'
		);

		$redirect = esc_url( $atts['redirect'] );

		self::$redirect_url = $redirect;

		ob_start();
		?>
		<div class="capsq-shortcode-wrap">
			<div class="capsq-sc-loader" id="capsq-sc-model-loader">
				<span class="capsq-spinner"></span>
				<?php esc_html_e( 'Preparing Facial Authentication…', 'capsquery-facial-authentication' ); ?>
			</div>

			<button
				type="button"
				class="capsq-sc-btn"
				id="capsq-sc-open-login"
				style="display:none;"
				data-redirect="<?php echo esc_attr( $redirect ); ?>"
			>
				<img
					src="<?php echo esc_url( CAPSQ_PLUGIN_URL . 'assets/images/login-icon.png' ); ?>"
					width="24"
					height="24"
					alt=""
				>
				<?php echo esc_html( $atts['button_label'] ); ?>
			</button>
		</div>		
		<?php
		return ob_get_clean();
	}

	public function render_modal_html(  ) {

		if (  self::$shortcode_present == false ) {
            return;
        }
        $redirect = self::$redirect_url;      
        ?>
        	<div id="capsq-sc-login-modal" class="capsq-modal" style="display:none;" data-redirect="<?php echo esc_attr( $redirect ); ?>">
				<div class="capsq-modal-inner">
					<button type="button" id="capsq-sc-close-modal" class="capsq-modal-close" aria-label="<?php esc_attr_e( 'Close', 'capsquery-facial-authentication' ); ?>">&times;</button>

					<div id="capsq-sc-step-camera">
						<h3><?php esc_html_e( 'Look at the camera', 'capsquery-facial-authentication' ); ?></h3>
						<div class="capsq-camera-wrap">
							<div id="capsq-sc-camera-wrap">
								<video id="capsq-sc-video" width="300" height="300" autoplay muted playsinline></video>
								<svg viewBox="0 0 350 350" xmlns="http://www.w3.org/2000/svg" style="position:absolute;top:0;left:0;" aria-hidden="true">
									<defs>
										<mask id="capsq-sc-hole">
											<rect width="350" height="350" fill="white"/>
											<ellipse cx="180" cy="130" rx="95" ry="110" fill="black"/>
										</mask>
									</defs>
									<rect width="350" height="350" fill="rgba(0,0,0,0.5)" mask="url(#capsq-sc-hole)"/>
									<ellipse id="capsq-sc-guide" cx="180" cy="130" rx="95" ry="110" fill="none" stroke="#00ffcc" stroke-width="2" stroke-dasharray="8,4"/>
								</svg>
							</div>
						</div>
						<p id="capsq-sc-status" class="capsq-status-idle"><?php esc_html_e( 'Starting camera…', 'capsquery-facial-authentication' ); ?></p>
					</div>

					<div id="capsq-sc-step-confirm" style="display:none;">
						<h3><?php esc_html_e( 'Confirm identity', 'capsquery-facial-authentication' ); ?></h3>
						<p id="capsq-sc-confirm-user" class="capsq-confirm-name"></p>
						<button type="button" id="capsq-sc-confirm-yes" class="button button-primary"><?php esc_html_e( 'Yes, continue', 'capsquery-facial-authentication' ); ?></button>
						<button type="button" id="capsq-sc-confirm-no" class="button"><?php esc_html_e( 'Not me', 'capsquery-facial-authentication' ); ?></button>
					</div>

					<div id="capsq-sc-step-pin" style="display:none;">
						<h3><?php esc_html_e( 'Enter your PIN', 'capsquery-facial-authentication' ); ?></h3>
						<input type="password" id="capsq-sc-pin" inputmode="numeric" pattern="[0-9]*" maxlength="8" class="capsq-pin-input" autocomplete="one-time-code" />
						<p>
							<button type="button" id="capsq-sc-submit-pin" class="button button-primary"><?php esc_html_e( 'Unlock', 'capsquery-facial-authentication' ); ?></button>
						</p>
						<p id="capsq-sc-pin-error" class="caps-error"></p>
					</div>
				</div>
			</div>
        <?php
	}


}
