<?php
namespace UserProfileClean;

use WP_PowerSuite\Core\AbstractModule;

class Module extends AbstractModule {

	private const MODULE_ID = 'user-profile-clean';
	private const MODULE_VERSION = '1.3.0';

	private ?array $settings_cache = null;

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

	public function getName(): string {
		return __( 'User Profile Clean', 'wp-powersuite' );
	}

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

	public function getDescription(): string {
		return __( 'Hides clutter on user profile screens so people see only what matters.', 'wp-powersuite' );
	}

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

	public function getDependencies(): array {
		return [];
	}

	protected function registerHooks(): void {
		if ( ! is_admin() ) {
			return;
		}

		$settings = $this->getSettings();

		if ( ! empty( $settings['remove_color_scheme'] ) ) {
			add_action( 'admin_init', [ $this, 'removeColorSchemePicker' ], 1 );
		}

		if ( ! empty( $settings['remove_website'] ) ) {
			add_filter( 'user_contactmethods', [ $this, 'removeWebsiteFromContactMethods' ], 10, 1 );
		}

		if ( $this->hasActiveHides( $settings ) ) {
			add_action( 'admin_head', [ $this, 'addProfileStyles' ] );
		}
	}

	/**
	 * Prevent the Administration Color Scheme row from rendering on profile screens.
	 */
	public function removeColorSchemePicker(): void {
		remove_action( 'admin_color_scheme_picker', 'admin_color_scheme_picker' );
	}

	/**
	 * Remove Website from contact methods so the field is not rendered at all.
	 *
	 * @param array<string, string> $methods Contact methods.
	 * @return array<string, string>
	 */
	public function removeWebsiteFromContactMethods( array $methods ): array {
		unset( $methods['url'] );
		return $methods;
	}

	public function addProfileStyles(): void {
		$screen = get_current_screen();
		if ( ! $screen || ( $screen->id !== 'profile' && $screen->id !== 'user-edit' ) ) {
			return;
		}

		$settings = $this->getSettings();
		$rules    = [];

		if ( ! empty( $settings['remove_color_scheme'] ) ) {
			$rules[] = 'tr.user-admin-color-wrap { display: none !important; }';
		}
		if ( ! empty( $settings['remove_toolbar'] ) ) {
			$rules[] = 'tr.user-admin-bar-front-wrap { display: none !important; }';
		}
		if ( ! empty( $settings['remove_biographical_info'] ) ) {
			$rules[] = 'tr.user-description-wrap { display: none !important; }';
		}
		if ( ! empty( $settings['remove_keyboard_shortcuts'] ) ) {
			$rules[] = 'tr.user-comment-shortcuts-wrap { display: none !important; }';
		}
		if ( ! empty( $settings['remove_visual_editor'] ) ) {
			$rules[] = 'tr.user-rich-editing-wrap { display: none !important; }';
		}
		if ( ! empty( $settings['remove_syntax_highlighting'] ) ) {
			$rules[] = 'tr.user-syntax-highlighting-wrap { display: none !important; }';
		}
		if ( ! empty( $settings['remove_website'] ) ) {
			$rules[] = 'tr.user-url-wrap { display: none !important; }';
		}
		if ( ! empty( $settings['remove_language'] ) ) {
			$rules[] = 'tr.user-language-wrap { display: none !important; }';
		}

		if ( $rules === [] ) {
			return;
		}

		echo '<style type="text/css">' . wp_strip_all_tags( implode( ' ', $rules ) ) . '</style>';
	}

	/**
	 * @param array<string, mixed> $settings
	 */
	private function hasActiveHides( array $settings ): bool {
		foreach ( array_keys( self::getDefaultSettings() ) as $key ) {
			if ( ! empty( $settings[ $key ] ) ) {
				return true;
			}
		}

		return false;
	}

	/**
	 * @return array<string, bool>
	 */
	public static function getDefaultSettings(): array {
		return [
			'remove_color_scheme'        => false,
			'remove_toolbar'             => false,
			'remove_biographical_info'   => false,
			'remove_keyboard_shortcuts'  => false,
			'remove_visual_editor'       => false,
			'remove_syntax_highlighting' => false,
			'remove_website'             => false,
			'remove_language'            => false,
		];
	}

	/**
	 * @return array<string, bool>
	 */
	private function getSettings(): array {
		if ( $this->settings_cache !== null ) {
			return $this->settings_cache;
		}

		$raw      = $this->moduleSettings()->all( [] );
		$raw      = is_array( $raw ) ? $raw : [];
		$defaults = self::getDefaultSettings();

		$this->settings_cache = array_merge( $defaults, array_intersect_key( $raw, $defaults ) );

		return $this->settings_cache;
	}
}
