<?php
/**
 * Theme settings page
 *
 * @package Teydea_Studio\Careers
 */

namespace Teydea_Studio\Careers\Modules;

use Teydea_Studio\Careers\Dependencies\Universal_Modules;
use Teydea_Studio\Careers\Dependencies\Utils;

/**
 * The "Module_Settings_Page" class
 */
final class Module_Settings_Page extends Universal_Modules\Module_Settings_Page {
	/**
	 * Register hooks
	 *
	 * @return void
	 */
	public function register(): void {
		parent::register();

		// Filter inline data passed to the settings page script.
		add_filter( 'careers__settings_page_script_inline_data', [ $this, 'filter_inline_data' ] );

		// Filter the list of additional dependencies of the settings page script.
		add_filter( 'careers__settings_page_script_additional_dependencies', [ $this, 'filter_script_additional_dependencies' ] );

		// Filter the list of additional dependencies of the settings page stylesheet.
		add_filter( 'careers__settings_page_stylesheet_additional_dependencies', [ $this, 'filter_stylesheet_additional_dependencies' ] );
	}

	/**
	 * Setup the values of the class properties
	 *
	 * @return void
	 */
	public function setup_class_properties(): void {
		// Define the settings page parent slug for single site installations.
		$this->parent_slug = 'themes.php';

		// Define the page title.
		$this->page_title = __( '"Careers" theme', 'careers' );

		// Define the menu title.
		$this->menu_title = __( 'About the theme', 'careers' );

		// Define the list of help & support links.
		$this->help_links = [
			[
				'url'   => sprintf( 'https://wordpress.org/support/theme/%s/', $this->container->get_slug() ),
				'title' => __( 'Support forum', 'careers' ),
			],
			[
				'url'   => 'https://teydeastudio.com/contact/',
				'title' => __( 'Contact theme author', 'careers' ),
			],
			[
				'url'   => sprintf( 'https://wordpress.org/themes/%s/', $this->container->get_slug() ),
				'title' => __( 'Theme on WordPress.org directory', 'careers' ),
			],
			[
				'url'   => 'https://teydeastudio.com/?utm_source=Careers',
				'title' => __( 'Teydea Studio\'s website', 'careers' ),
			],
		];

		parent::setup_class_properties();
	}

	/**
	 * Filter inline data passed to the settings page script
	 *
	 * @param array<string,mixed> $data Inline data array.
	 *
	 * @return array<string,mixed> Updated inline data array.
	 */
	public function filter_inline_data( array $data ): array {
		if ( ! function_exists( 'get_plugins' ) ) {
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
		}

		// Define the recommended plugin data.
		$slug        = 'hiring-center';
		$plugin      = sprintf( '%1$s/%1$s.php', $slug );
		$details_url = is_multisite()
			? network_admin_url( 'plugin-install.php' )
			: admin_url( 'plugin-install.php' );

		$details_url = add_query_arg(
			[
				'tab'       => 'plugin-information',
				'plugin'    => $slug,
				'TB_iframe' => 'true',
				'width'     => 600,
				'height'    => 800,
			],
			$details_url,
		);

		// Get the list of all plugins.
		$all_plugins = get_plugins();

		// Pass the plugin info into the data object.
		$data['recommendedPlugins'] = [
			[
				'name'        => __( 'Hiring Center', 'careers' ),
				'description' => __( 'Create a powerful job portal and professional career page directly within WordPress. Simplify your recruitment workflow and manage job listings.', 'careers' ),
				'slug'        => $slug,
				'detailsUrl'  => $details_url,
				'status'      => [
					'isActive'    => is_plugin_active( $plugin ),
					'isInstalled' => isset( $all_plugins[ $plugin ] ),
				],
			],
		];

		return $data;
	}

	/**
	 * Filter the list of additional dependencies of the settings page script
	 *
	 * @param string[] $dependencies Additional script dependencies.
	 *
	 * @return string[] Updated list of additional script dependencies.
	 */
	public function filter_script_additional_dependencies( array $dependencies ): array {
		$dependencies[] = 'plugin-install';
		return $dependencies;
	}

	/**
	 * Filter the list of additional dependencies of the settings page stylesheet
	 *
	 * @param string[] $dependencies Additional stylesheet dependencies.
	 *
	 * @return string[] Updated list of additional stylesheet dependencies.
	 */
	public function filter_stylesheet_additional_dependencies( array $dependencies ): array {
		$dependencies[] = 'thickbox';
		return $dependencies;
	}

	/**
	 * Check if the page requested is a settings page
	 *
	 * @return bool Whether the page requested is a settings page or not.
	 */
	public function is_settings_page(): bool {
		$screen = new Utils\Screen( $this->container );
		return $screen->is( $this->get_page_slug(), 'appearance_page' );
	}
}
