<?php
/**
 * Adds a "Login with Face" button to wp-login.php.
 */

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

class Capsq_Login {

	public function __construct() {
		add_action( 'login_enqueue_scripts', array( $this, 'enqueue_assets' ) );
		add_action( 'login_form', array( $this, 'render_button' ) );
		add_action( 'login_footer', array( $this, 'render_modal' ) );
	}

	public function enqueue_assets() {
		if ( ! $this->should_show_login_ui() ) {
			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(
			'face-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( 'face-api' ),
			capsq_asset_version( 'assets/js/capsq-login.js' ),
			false
		);

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

	public function render_button() {
		if ( ! $this->should_show_login_ui() ) {
			return;
		}
		?>
		<p style="margin-top:16px;text-align:center;">
			<button type="button" id="capsq-open-login" class="button button-secondary" style="display:none; width:100%;justify-content: center;align-items: center;gap: 5px;">
				<img src="<?php echo esc_url( CAPSQ_PLUGIN_URL . 'assets/images/login-icon.png' ); ?>" width="30" height="30" alt=""> <?php esc_html_e( 'Login with Face', 'capsquery-facial-authentication' ); ?>
			</button>
			<div id="capsq-model-loader" style="display:flex; justify-content: center; align-items: center; gap: 5px;">
				<span class="capsq-spinner"></span>
				<?php esc_html_e( 'Preparing Facial Authentication…', 'capsquery-facial-authentication' ); ?>
			</div>
		</p>
		<?php
	}

	public function render_modal(){
		?>
			<div id="capsq-login-modal" class="capsq-modal" style="display:none;">
				<div class="capsq-modal-inner">
					<button type="button" id="capsq-close-modal" class="capsq-modal-close" aria-label="<?php esc_attr_e( 'Close', 'capsquery-facial-authentication' ); ?>">&times;</button>

					<div id="capsq-step-camera">
						<h3><?php esc_html_e( 'Look at the camera', 'capsquery-facial-authentication' ); ?></h3>
						<div class="capsq-camera-wrap">
							<div id="capsq-login-camera-wrap">
								<video id="capsq-login-video" width="350" height="350" 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="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(#hole)"/>
									<ellipse 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-login-status"><?php esc_html_e( 'Starting camera…', 'capsquery-facial-authentication' ); ?></p>
					</div>

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

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

	/**
	 * Show login UI only when enabled and at least one user is enrolled.
	 */
	private function should_show_login_ui() {
		if ( ! Capsq_Helpers::is_enabled() ) {
			return false;
		}
		return self::has_enrolled_users();
	}

	public static function has_enrolled_users() {
		foreach ( Capsq_Helpers::capsquery_get_user_ids() as $user_id ) {
			if (get_user_meta( $user_id, '_capsq_face_descriptor', true )) {
				return true;
			}
		}
		return false;
	}
}
