<?php
defined( 'ABSPATH' ) || exit;

/**
 * Shared slider HTML renderer.
 */
class AWP_Renderer {

	/**
	 * Unique instance counter.
	 *
	 * @var int
	 */
	private static $counter = 0;

	/**
	 * Render a slider from slide arrays and settings.
	 *
	 * @param array $slides   Slides: image, title, subtitle, content, button_text, button_link.
	 * @param array $settings Settings overrides merged with globals.
	 * @param array $attrs    Extra wrapper attributes.
	 * @return string
	 */
	public static function render( $slides, $settings = array(), $attrs = array() ) {
		$slides = array_values( array_filter( (array) $slides, array( __CLASS__, 'is_valid_slide' ) ) );

		if ( empty( $slides ) ) {
			return '';
		}

		AWP_Assets::enqueue();

		$settings = wp_parse_args( $settings, AWP_Settings::get() );
		$config   = AWP_Settings::swiper_config( $settings );
		$css_vars = AWP_Settings::css_variables( $settings );
		self::$counter++;
		$uid = 'awp-slider-' . self::$counter;

		$class = 'awp-slider';
		if ( ! empty( $attrs['class'] ) ) {
			$class .= ' ' . $attrs['class'];
		}

		ob_start();
		?>
		<div
			id="<?php echo esc_attr( $uid ); ?>"
			class="<?php echo esc_attr( $class ); ?>"
			style="<?php echo esc_attr( $css_vars ); ?>"
			data-awp-settings="<?php echo esc_attr( wp_json_encode( $config ) ); ?>"
		>
			<div class="swiper awp-swiper">
				<div class="swiper-wrapper">
					<?php foreach ( $slides as $slide ) : ?>
						<div class="swiper-slide">
							<div class="awp-slide">
								<div class="awp-slide__media">
									<img src="<?php echo esc_url( $slide['image'] ); ?>" alt="<?php echo esc_attr( $slide['title'] ); ?>" loading="lazy" />
									<span class="awp-slide__overlay" aria-hidden="true"></span>
								</div>
								<div class="awp-slide__content awp-align-<?php echo esc_attr( $settings['content_align'] ); ?>">
									<?php if ( ! empty( $slide['subtitle'] ) ) : ?>
										<p class="awp-slide__subtitle"><?php echo esc_html( $slide['subtitle'] ); ?></p>
									<?php endif; ?>
									<?php if ( ! empty( $slide['title'] ) ) : ?>
										<h2 class="awp-slide__title"><?php echo esc_html( $slide['title'] ); ?></h2>
									<?php endif; ?>
									<?php if ( ! empty( $slide['content'] ) ) : ?>
										<p class="awp-slide__text"><?php echo esc_html( $slide['content'] ); ?></p>
									<?php endif; ?>
									<?php if ( ! empty( $slide['button_text'] ) && ! empty( $slide['button_link'] ) ) : ?>
										<a class="awp-slide__button" href="<?php echo esc_url( $slide['button_link'] ); ?>">
											<?php echo esc_html( $slide['button_text'] ); ?>
										</a>
									<?php endif; ?>
								</div>
							</div>
						</div>
					<?php endforeach; ?>
				</div>

				<?php if ( ! empty( $settings['show_pagination'] ) ) : ?>
					<div class="swiper-pagination awp-pagination"></div>
				<?php endif; ?>

				<?php if ( ! empty( $settings['show_arrows'] ) ) : ?>
					<button type="button" class="swiper-button-prev awp-nav awp-nav--prev" aria-label="<?php esc_attr_e( 'Previous slide', 'advance-wp-slider' ); ?>"></button>
					<button type="button" class="swiper-button-next awp-nav awp-nav--next" aria-label="<?php esc_attr_e( 'Next slide', 'advance-wp-slider' ); ?>"></button>
				<?php endif; ?>
			</div>
		</div>
		<?php
		return ob_get_clean();
	}

	/**
	 * Validate slide has an image.
	 *
	 * @param array $slide Slide data.
	 * @return bool
	 */
	public static function is_valid_slide( $slide ) {
		return is_array( $slide ) && ! empty( $slide['image'] );
	}

	/**
	 * Build slides from CPT posts.
	 *
	 * @param array $args WP_Query args overrides.
	 * @return array
	 */
	public static function slides_from_cpt( $args = array() ) {
		$query_args = wp_parse_args(
			$args,
			array(
				'post_type'              => 'awp-slider',
				'post_status'            => 'publish',
				'posts_per_page'         => -1,
				'order'                  => 'ASC',
				'orderby'                => 'menu_order date',
				'no_found_rows'          => true,
				'update_post_meta_cache' => true,
				'update_post_term_cache' => false,
			)
		);

		$query  = new WP_Query( $query_args );
		$slides = array();

		if ( $query->have_posts() ) {
			while ( $query->have_posts() ) {
				$query->the_post();
				$image = get_the_post_thumbnail_url( get_the_ID(), 'full' );
				if ( ! $image ) {
					continue;
				}

				$slides[] = array(
					'image'       => $image,
					'title'       => get_the_title(),
					'subtitle'    => get_post_meta( get_the_ID(), 'slider_subtitle', true ),
					'content'     => wp_trim_words( wp_strip_all_tags( get_the_content() ), 40, '' ),
					'button_text' => get_post_meta( get_the_ID(), 'slider_button_text', true ),
					'button_link' => get_post_meta( get_the_ID(), 'slider_button_link', true ),
				);
			}
			wp_reset_postdata();
		}

		return $slides;
	}
}
