<?php
/**
 * Theme setup class
 *
 * @package Teydea_Studio\Careers
 */

namespace Teydea_Studio\Careers\Modules\Setup;

use Teydea_Studio\Careers\Dependencies\Utils;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // @codeCoverageIgnore
}

/**
 * The "Module_Setup" class
 */
final class Module_Setup extends Utils\Module {
	/**
	 * Register hooks
	 *
	 * @return void
	 */
	public function register(): void {
		// Register theme features.
		add_action( 'after_setup_theme', [ $this, 'setup_theme' ] );

		// Register block pattern categories.
		add_action( 'init', [ $this, 'register_block_pattern_categories' ] );

		// Register custom block styles.
		add_action( 'init', [ $this, 'register_block_styles' ] );

		// Enqueue the CSS files.
		add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_styles' ] );

		// In case if custom logo is not set, default to the theme logo.
		add_filter( 'get_custom_logo', [ $this, 'filter_get_custom_logo' ] );
	}

	/**
	 * Register theme features
	 *
	 * @return void
	 */
	public function setup_theme(): void {
		// Load theme styles into the editor.
		add_editor_style( 'style.css' );

		/**
		 * Load additional block styles
		 */
		$styled_blocks = [
			'core/comment-template',
			'core/comments-pagination-numbers',
			'core/comments-pagination',
			'core/post-author',
			'core/post-comments-form',
			'core/post-terms',
			'core/pullquote',
			'core/query-pagination-numbers',
			'core/query-pagination',
			'hiring-center/job-specification-characteristics',
			'hiring-center/job-specification-list',
			'hiring-center/query-loop-filtering-container',
			'hiring-center/query-loop-filtering-filter',
		];

		foreach ( $styled_blocks as $block_name ) {
			$block_slug = str_replace( '/', '-', $block_name );

			wp_enqueue_block_style(
				$block_name,
				[
					'handle' => "careers-$block_slug-block-style",
					'src'    => get_theme_file_uri( "assets/css/$block_slug.css" ),
					'path'   => get_theme_file_path( "assets/css/$block_slug.css" ),
				],
			);
		}
	}

	/**
	 * Register block pattern categories
	 *
	 * @return void
	 */
	public function register_block_pattern_categories(): void {
		register_block_pattern_category(
			'careers-patterns',
			[ 'label' => __( 'Careers', 'careers' ) ],
		);
	}

	/**
	 * Register custom block styles
	 *
	 * @return void
	 */
	public function register_block_styles(): void {
		register_block_style(
			'core/pullquote',
			[
				'name'  => 'with-icon',
				'label' => __( 'With icon', 'careers' ),
			],
		);

		register_block_style(
			'core/read-more',
			[
				'name'  => 'button',
				'label' => __( 'Button', 'careers' ),
			],
		);

		register_block_style(
			'hiring-center/query-loop-filtering-container',
			[
				'name'  => 'boxed',
				'label' => __( 'Boxed', 'careers' ),
			],
		);
	}

	/**
	 * Enqueue the CSS files
	 *
	 * @return void
	 */
	public function enqueue_styles(): void {
		wp_enqueue_style( 'careers-style', get_stylesheet_uri(), [], $this->container->get_version() );
	}

	/**
	 * In case if custom logo is not set, default to the theme logo
	 *
	 * @param string $html Custom logo HTML output.
	 *
	 * @return string Logo HTML output.
	 */
	public function filter_get_custom_logo( string $html ): string {
		if ( empty( $html ) ) {
			$html = sprintf(
				'<a href="%1$s" class="custom-logo-link" rel="home"><img width="149" height="32" src="%2$s/assets/images/logo.png" class="custom-logo" alt="%3$s" decoding="async" /></a>',
				esc_url( home_url() ),
				esc_url( get_template_directory_uri() ),
				__( 'Logo', 'careers' ),
			);
		}

		return $html;
	}
}
