<?php
/**
 * Disable Right Click Module
 *
 * @package DisableRightClick
 * @since 1.0.0
 */

namespace DisableRightClick;

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

class Module extends AbstractModule {

	private const MODULE_ID = 'disable-right-click';
	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 Right Click', 'wp-powersuite' );
	}

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

	public function getDescription(): string {
		return __( 'Turns off right-click for selected roles to gently discourage copying—lightweight, not bulletproof.', '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 );
	}

	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['disableRightClick'] = true;
		if ( ! isset( $config['messages'] ) || ! is_array( $config['messages'] ) ) {
			$config['messages'] = [];
		}
		$config['messages']['disableRightClick'] = $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;
	}
}
