<?php
declare( strict_types=1 );

namespace FiboFilters\Util;

/**
 * Class TemplateUtils
 */
class TemplateUtil {
	/**
	 * Generate inline style tag for skeleton.
	 *
	 * @param string $layout Layout type.
	 * @param int    $min Minimum screen width.
	 * @param int    $max Maximum screen width.
	 *
	 * @return string
	 */
	public static function generate_inline_style_tag( string $layout, int $min, int $max ): string {
		$selector = ".fibofilters-skeleton--{$layout}.fibofilters-skeleton--{$min}-{$max}";

		$css = "@media (min-width: {$min}px) and (max-width: {$max}px) { {$selector} { display: flex; } }\n";

		return '<style>' . $css . '</style>';
	}

	/**
	 * Render skeleton for filters.
	 *
	 * @param string $layout Layout type: 'vertical', 'horizontal', or 'mobile'.
	 * @param int    $min_screen Minimum screen width for the skeleton.
	 * @param int    $max_screen Maximum screen width for the skeleton.
	 *
	 * @return string
	 */
	public static function render_skeleton( string $layout, int $min_screen = 0, int $max_screen = 0 ): string {
		if ( ! apply_filters( 'fibofilters/front/display_skeleton', true ) ) {
			return '';
		}
		$selector = "fibofilters-skeleton--{$min_screen}-{$max_screen}";

		switch ( $layout ) {
			case 'vertical':
				$skeleton_html = self::get_vertical_filters_skeleton( $layout, $selector );
				break;
			case 'horizontal':
			case 'mobile':
				$skeleton_html = self::get_horizontal_filters_skeleton( $layout, $selector );
				break;
			default:
				return '';
		}

		$style_tag = self::generate_inline_style_tag( $layout, $min_screen, $max_screen );

		return preg_replace(
			'/<aside([^>]*)>/',
			'<aside$1>' . $style_tag,
			$skeleton_html
		);
	}

	/**
	 * Get skeleton for horizontal filters.
	 *
	 * @param string $layout Layout type.
	 * @param string $selector Selector for the skeleton.
	 *
	 * @return string
	 */
	public static function get_horizontal_filters_skeleton( string $layout, string $selector ): string {
		$class = 'fibofilters-skeleton fibofilters-skeleton--' . $layout . ' ' . $selector;
		ob_start();
		?>
		<aside class="<?php echo esc_attr( $class ); ?>" aria-hidden="true">
			<div class="fibofilters-skel fibofilters-skel-btn"></div>
			<div class="fibofilters-skel fibofilters-skel-btn"></div>
			<div class="fibofilters-skel fibofilters-skel-btn"></div>
		</aside>
		<?php
		return ob_get_clean();
	}

	/**
	 * Get skeleton for vertical filters.
	 *
	 * @param string $layout Layout type.
	 * @param string $selector Selector for the skeleton.
	 *
	 * @return string
	 */
	public static function get_vertical_filters_skeleton( string $layout, string $selector ): string {
		$class = 'fibofilters-skeleton fibofilters-skeleton--' . $layout . ' ' . $selector;
		ob_start();
		?>
		<aside class="<?php echo esc_attr( $class ); ?>" aria-hidden="true">
			<div class="fibofilters-skel fibofilters-skel-heading"></div>
			<?php for ( $i = 0; $i < 6; $i++ ) : ?>
				<div class="fibofilters-skel-row">
					<div class="fibofilters-skel fibofilters-skel-checkbox"></div>
					<div class="fibofilters-skel fibofilters-skel-text"></div>
					<div class="fibofilters-skel fibofilters-skel-count"></div>
				</div>
			<?php endfor; ?>
			<div class="fibofilters-skel-divider"></div>
			<div class="fibofilters-skel fibofilters-skel-heading"></div>
			<?php for ( $i = 0; $i < 3; $i++ ) : ?>
				<div class="fibofilters-skel-row">
					<div class="fibofilters-skel fibofilters-skel-radio"></div>
					<div class="fibofilters-skel fibofilters-skel-text"></div>
					<div class="fibofilters-skel fibofilters-skel-count"></div>
				</div>
			<?php endfor; ?>
		</aside>
		<?php
		return ob_get_clean();
	}

	/**
	 * Generate content for .htaccess file to prevent indexing.
	 *
	 * @return string
	 */
	public static function generate_htaccess_content(): string {
		$htaccess_lines = [
			'<IfModule mod_headers.c>',
			'    Header set X-Robots-Tag "noindex, nofollow"',
			'</IfModule>',
		];

		return implode( PHP_EOL, $htaccess_lines ) . PHP_EOL;
	}
}
