<?php
/**
 * Extended maintenance features (formerly maca Hold Pro).
 *
 * @package MacaHold
 */

defined( 'ABSPATH' ) || exit;

/**
 * Registers extended features via plugin hooks.
 */
class Maca_Hold_Extended_Features {

	/**
	 * Slug for the custom background from settings.
	 */
	private const CUSTOM_BACKGROUND_ID = 'custom';

	/**
	 * Slug for the hero template.
	 */
	private const HERO_TEMPLATE = 'hero';

	/**
	 * Register hooks.
	 *
	 * @return void
	 */
	public function register() {
		add_filter( 'maca_hold_max_links', array( $this, 'filter_max_links' ) );
		add_filter( 'maca_hold_backgrounds', array( $this, 'filter_backgrounds' ) );
		add_filter( 'maca_hold_templates', array( $this, 'filter_templates' ) );
		add_filter( 'maca_hold_template_path', array( $this, 'filter_template_path' ), 10, 2 );
		add_filter( 'maca_hold_maintenance_styles', array( $this, 'filter_maintenance_styles' ) );
		add_action( 'maca_hold_before_render', array( $this, 'enqueue_maintenance_assets' ) );
		add_action( 'maca_hold_maintenance_head', array( $this, 'print_maintenance_styles' ), 1 );
		add_action( 'maca_hold_maintenance_head', array( $this, 'render_head_extras' ) );
		add_action( 'maca_hold_after_links', array( $this, 'render_social_links' ), 10, 2 );
	}

	/**
	 * @param int $max_links Default link limit.
	 * @return int
	 */
	public function filter_max_links( $max_links ) {
		unset( $max_links );

		$settings = Maca_Hold_Settings::get();

		return max( 5, (int) ( $settings['max_links'] ?? 20 ) );
	}

	/**
	 * @param array<string, array{id: string, label: string, url: string}> $backgrounds Existing backgrounds.
	 * @return array<string, array{id: string, label: string, url: string}>
	 */
	public function filter_backgrounds( $backgrounds ) {
		if ( ! is_array( $backgrounds ) ) {
			$backgrounds = array();
		}

		$settings = Maca_Hold_Settings::get();
		$custom   = esc_url_raw( $settings['custom_background_url'] ?? '' );

		if ( '' !== $custom ) {
			$label = trim( (string) ( $settings['custom_background_label'] ?? '' ) );
			if ( '' === $label ) {
				$label = __( 'Custom background', 'maca-hold' );
			}

			$backgrounds[ self::CUSTOM_BACKGROUND_ID ] = array(
				'id'    => self::CUSTOM_BACKGROUND_ID,
				'label' => $label,
				'url'   => $custom,
			);
		}

		$files = glob( MACA_HOLD_PATH . 'assets/hold-pro*.png' );

		if ( ! is_array( $files ) ) {
			return $backgrounds;
		}

		foreach ( $files as $file ) {
			$filename = basename( $file );
			$slug     = basename( $file, '.png' );

			if ( ! preg_match( '/^hold-pro\d+$/', $slug ) ) {
				continue;
			}

			$backgrounds[ $slug ] = array(
				'id'    => $slug,
				'label' => sprintf(
					/* translators: %s: background image number */
					__( 'Background %s', 'maca-hold' ),
					str_replace( 'hold-pro', '', $slug )
				),
				'url'   => MACA_HOLD_URL . 'assets/' . $filename,
			);
		}

		return $backgrounds;
	}

	/**
	 * @param array<string, string> $templates Template slug => label.
	 * @return array<string, string>
	 */
	public function filter_templates( $templates ) {
		if ( ! is_array( $templates ) ) {
			$templates = array();
		}

		$templates[ self::HERO_TEMPLATE ] = __( 'Hero', 'maca-hold' );

		return $templates;
	}

	/**
	 * @param string $path     Default template path.
	 * @param string $template Template slug.
	 * @return string
	 */
	public function filter_template_path( $path, $template ) {
		if ( self::HERO_TEMPLATE !== sanitize_key( $template ) ) {
			return $path;
		}

		$hero_path = MACA_HOLD_PATH . 'public/templates/hero.php';

		return file_exists( $hero_path ) ? $hero_path : $path;
	}

	/**
	 * @param array<int, string> $styles Registered style handles.
	 * @return array<int, string>
	 */
	public function filter_maintenance_styles( $styles ) {
		if ( ! is_array( $styles ) ) {
			$styles = array( 'maca-hold-maintenance' );
		}

		$styles[] = 'maca-hold-extended-maintenance';

		$template = Maca_Hold_Settings::get()['template'] ?? 'maintenance';
		if ( self::HERO_TEMPLATE === $template ) {
			$styles[] = 'maca-hold-hero';
		}

		return array_values( array_unique( $styles ) );
	}

	/**
	 * @param array<string, mixed> $settings Plugin settings.
	 * @return void
	 */
	public function print_maintenance_styles( $settings ) {
		unset( $settings );

		$handles = array( 'maca-hold-extended-maintenance' );

		$template = Maca_Hold_Settings::get()['template'] ?? 'maintenance';
		if ( self::HERO_TEMPLATE === $template ) {
			$handles[] = 'maca-hold-hero';
		}

		$pending = array();

		foreach ( $handles as $handle ) {
			if ( wp_style_is( $handle, 'enqueued' ) && ! wp_style_is( $handle, 'done' ) ) {
				$pending[] = $handle;
			}
		}

		if ( ! empty( $pending ) ) {
			wp_print_styles( $pending );
		}
	}

	/**
	 * @param array<string, mixed> $settings Plugin settings.
	 * @return void
	 */
	public function enqueue_maintenance_assets( $settings ) {
		unset( $settings );

		$all_settings = Maca_Hold_Settings::get();
		$template     = $all_settings['template'] ?? 'maintenance';

		wp_register_style(
			'maca-hold-extended-maintenance',
			MACA_HOLD_URL . 'assets/css/pro-maintenance.css',
			array( 'maca-hold-maintenance' ),
			MACA_HOLD_VERSION
		);
		wp_enqueue_style( 'maca-hold-extended-maintenance' );

		if ( self::HERO_TEMPLATE === $template ) {
			wp_register_style(
				'maca-hold-hero',
				MACA_HOLD_URL . 'assets/css/hero.css',
				array( 'maca-hold-maintenance' ),
				MACA_HOLD_VERSION
			);
			wp_enqueue_style( 'maca-hold-hero' );
		}

		$custom_css = trim( (string) ( $all_settings['custom_css'] ?? '' ) );
		if ( '' !== $custom_css ) {
			wp_add_inline_style( 'maca-hold-maintenance', $custom_css );
		}
	}

	/**
	 * @param array<string, mixed> $settings Plugin settings.
	 * @return void
	 */
	public function render_head_extras( $settings ) {
		unset( $settings );

		$all_settings = Maca_Hold_Settings::get();
		$favicon      = esc_url( $all_settings['favicon_url'] ?? '' );

		if ( '' !== $favicon ) {
			echo '<link rel="icon" href="' . esc_url( $favicon ) . '" />' . "\n";
		}

		$head_code = trim( (string) ( $all_settings['head_code'] ?? '' ) );
		if ( '' !== $head_code ) {
			echo "\n" . $head_code . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- sanitized on save; script tags require unfiltered_html.
		}
	}

	/**
	 * @param array<int, array<string, mixed>> $links    Sanitized maintenance links.
	 * @param array<string, mixed>            $settings Plugin settings.
	 * @return void
	 */
	public function render_social_links( $links, $settings ) {
		unset( $links, $settings );

		$all_settings = Maca_Hold_Settings::get();
		$social_links = $all_settings['social_links'] ?? array();
		$display      = $all_settings['social_links_display'] ?? 'icons';
		$icons_only   = 'icons_text' !== $display;

		if ( ! is_array( $social_links ) || empty( $social_links ) ) {
			return;
		}

		$nav_class = $icons_only
			? 'maca-hold-social-links maca-hold-social-links--icons'
			: 'maca-hold-social-links maca-hold-social-links--icons-text';
		?>
		<nav class="<?php echo esc_attr( $nav_class ); ?>" aria-label="<?php echo esc_attr__( 'Social media', 'maca-hold' ); ?>">
			<?php foreach ( $social_links as $social_link ) : ?>
				<?php
				$network = sanitize_key( $social_link['network'] ?? 'custom' );
				if ( ! Maca_Hold_Social_Networks::is_valid( $network ) ) {
					$network = 'custom';
				}
				$label = trim( (string) ( $social_link['label'] ?? '' ) );
				if ( '' === $label ) {
					$label = Maca_Hold_Social_Networks::get_label( $network );
				}
				?>
				<a
					class="maca-hold-social-link maca-hold-social-link--<?php echo esc_attr( $network ); ?>"
					href="<?php echo esc_url( $social_link['url'] ); ?>"
					aria-label="<?php echo esc_attr( $label ); ?>"
					<?php echo ! empty( $social_link['new_tab'] ) ? 'target="_blank" rel="noopener noreferrer"' : ''; ?>
				>
					<?php echo Maca_Hold_Social_Networks::render_icon( $network, (string) ( $social_link['icon_url'] ?? '' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in render_icon(). ?>
					<?php if ( ! $icons_only ) : ?>
						<span class="maca-hold-social-label"><?php echo esc_html( $label ); ?></span>
					<?php endif; ?>
				</a>
			<?php endforeach; ?>
		</nav>
		<?php
	}
}
