<?php
/**
 * Slider 5
 *
 * @author  Jegstudio
 * @since   1.0.0
 * @package gutenverse-news
 */

namespace GUTENVERSE\NEWS\Block\Slider;

/**
 * Slider_5
 *
 * @package gutenverse-news
 * @author Jegstudio
 */
class Slider_5 extends Slider_View_Abstract {

	/**
	 * Method content
	 *
	 * @param array $results results.
	 *
	 * @return string
	 */
	public function content( $results ) {
		$content = '';
		foreach ( $results as $key => $post ) {
			$primary_category = $this->get_primary_category( $post->ID );
			if ( $this->manager->get_current_width() > 8 ) {
				$image = get_the_post_thumbnail_url( $post->ID, 'gvnews-1140x570' );
			} else {
				$image = get_the_post_thumbnail_url( $post->ID, 'gvnews-750x375' );
			}
			$hidden_image = $this->attribute['normal_image'] && 0 === $key ? '<img loading="eager" fetchpriority="high" class="thumbnail-prioritize" src="' . esc_url( $image ) . '" style="display: none" >' : '';

			$content .=
			'<div ' . gvnews_post_class( 'gvnews_slide_item', $post->ID ) . ' style="background-image: url(' . esc_url( $image ) . ')">
					' . $hidden_image . '
                    ' . gvnews_edit_post( $post->ID ) . "
                    <div class=\"gvnews_slide_caption\">
                        <div class=\"gvnews_caption_container\">
                            <div class=\"gvnews_post_category\">
                                {$primary_category}
                            </div>
                            <{$this->post_title_tag} class=\"gvnews_post_title\">
                                <a href=\"" . esc_url( get_the_permalink( $post ) ) . '" aria-label="' . esc_attr( get_the_title( $post ) ) . '">' . esc_attr( get_the_title( $post ) ) . "</a>
                            </{$this->post_title_tag}>
                            {$this->render_meta($post)}
                        </div>
                    </div>
                </div>";
		}

		return $content;
	}

	/**
	 * Method render_element
	 *
	 * @param array $result results.
	 * @param array $attr    attribute.
	 *
	 * @return string
	 */
	public function render_element( $result, $attr ) {
		if ( ! empty( $result ) ) {
			$content          = $this->content( $result );
			$column_class     = $this->get_module_column_class( $attr );
			$autoplay_delay   = isset( $attr['autoplay_delay']['size'] ) ? $attr['autoplay_delay']['size'] : $attr['autoplay_delay'];
			$additional_class = 'none' === $attr['overlay_option'] ? 'no-overlay' : '';

			$wrapper_classes = gvnews_build_html_classes(
				array(
					'gvnews_slider_wrapper',
					'gvnews_slider_type_5_wrapper',
					esc_attr( $this->unique_id ),
					esc_attr( $this->get_vc_class_name() ),
					esc_attr( $attr['el_class'] ),
				)
			);

			$container_classes = gvnews_build_html_classes(
				array(
					'gvnews_slider_type_5',
					'gvnews_slider',
					$column_class,
				)
			);

			$data_attr = gvnews_build_data_attr(
				array(
					'autoplay'        => esc_attr( $attr['enable_autoplay'] ),
					'delay'           => esc_attr( $autoplay_delay ),
					'class-next'      => esc_attr( $attr['nextButtonIcon'] ),
					'class-next-type' => esc_attr( $attr['next_button_icon_type'] ),
					'class-next-svg'  => esc_attr( $attr['next_button_icon_svg'] ),
					'class-prev'      => esc_attr( $attr['prevButtonIcon'] ),
					'class-prev-type' => esc_attr( $attr['prev_button_icon_type'] ),
					'class-prev-svg'  => esc_attr( $attr['prev_button_icon_svg'] ),
				)
			);

			$output =
			'<div ' . esc_attr( $this->element_id( $attr ) ) . " class=\"{$wrapper_classes}\">
                    <div class=\"{$container_classes}\" {$data_attr}>
                        {$content}
                    </div>
                </div>";

			return $output;
		} else {
			return $this->empty_content();
		}
	}
}
