<?php
/**
 * Disable Text Selection Module
 *
 * Prevents text selection for selected user roles (content protection).
 *
 * @package DisableTextSelection
 * @since 1.0.0
 */

namespace DisableTextSelection;

use WP_PowerSuite\Core\AbstractModule;
use WP_PowerSuite\Core\ContentProtection;

class Module extends AbstractModule {

	private const MODULE_ID = 'disable-text-selection';
	private const MODULE_VERSION = '1.0.2';
	private const DEFAULT_DISABLED_MESSAGE = 'This function is disabled';

	private ?array $settings_cache = null;

	public function getId(): string {
		return self::MODULE_ID;
	}

	public function getName(): string {
		return __( 'Disable Text Selection', 'wp-powersuite' );
	}

	public function getVersion(): string {
		return self::MODULE_VERSION;
	}

	public function getDescription(): string {
		return __( 'Discourages text selection and copying for chosen roles—gentle content protection, not airtight security.', 'wp-powersuite' );
	}

	public function getContext(): string|array {
		return 'frontend';
	}

	public function getDefaultSettings(): array {
		return [
			'roles'              => [],
			'apply_to_visitors'   => true,
			'message'            => self::DEFAULT_DISABLED_MESSAGE,
			'message_position'   => 'top-right',
			'message_duration'   => 3000,
		];
	}

	protected function registerHooks(): void {
		if ( is_admin() || ! ContentProtection::couldApplyForConfiguredAudience( $this->getSettings() ) ) {
			return;
		}
		add_action( 'wp', [ $this, 'maybeRegisterContentProtection' ], 1 );
	}

	/**
	 * Register shared content-protection hooks only on normal frontend document requests.
	 */
	public function maybeRegisterContentProtection(): void {
		if ( ! ContentProtection::isBrowsableFrontendRequest() || ! $this->shouldApplyToCurrentUser() ) {
			return;
		}
		add_filter( 'wp_powersuite_content_protection_config', [ $this, 'addConfig' ], 10, 1 );
		ContentProtection::register_enqueue_callback();
	}

	private function resolveDisabledMessage( string $stored ): string {
		$trimmed = trim( $stored );
		if ( $trimmed === '' || $trimmed === self::DEFAULT_DISABLED_MESSAGE ) {
			return __( 'This function is disabled', 'wp-powersuite' );
		}
		return $stored;
	}

	public function addConfig( array $config ): array {
		$settings = $this->getSettings();
		$config['disableTextSelection'] = true;
		if ( ! isset( $config['messages'] ) || ! is_array( $config['messages'] ) ) {
			$config['messages'] = [];
		}
		$config['messages']['disableTextSelection'] = $this->resolveDisabledMessage( (string) ( $settings['message'] ?? self::DEFAULT_DISABLED_MESSAGE ) );
		if ( ! isset( $config['position'] ) ) {
			$config['position'] = $settings['message_position'] ?? 'top-right';
		}
		if ( ! isset( $config['duration'] ) ) {
			$config['duration'] = (int) ( $settings['message_duration'] ?? 3000 );
		}
		return $config;
	}

	private function shouldApplyToCurrentUser(): bool {
		$settings = $this->getSettings();
		if ( ! is_user_logged_in() ) {
			return ! empty( $settings['apply_to_visitors'] );
		}
		$user  = wp_get_current_user();
		$roles = $settings['roles'] ?? [];
		if ( ! is_array( $roles ) || empty( $roles ) ) {
			return false;
		}
		foreach ( $user->roles as $role ) {
			if ( in_array( $role, $roles, true ) ) {
				return true;
			}
		}
		return false;
	}

	private function getSettings(): array {
		if ( $this->settings_cache !== null ) {
			return $this->settings_cache;
		}
		$saved = $this->moduleSettings()->all( [] );
		$this->settings_cache = array_merge( $this->getDefaultSettings(), is_array( $saved ) ? $saved : [] );
		return $this->settings_cache;
	}
}
