<?php
namespace ThimPress\ThimBlocks\Gutenberg\Blocks\Slider;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

use ThimPress\ThimBlocks\Gutenberg\Blocks\AbstractBlockType;
use Throwable;

/**
 * Class SliderBlockType
 *
 * Handle register, render block template
 */
class SliderBlockType extends AbstractBlockType {
	public $block_name = 'slider';

	public function get_supports(): array {
		return array(
			'align'   => array( 'wide', 'full' ),
			'spacing' => array(
				'margin'  => true,
				'padding' => true,
			),
		);
	}

	/**
	 * Render content of block tag
	 *
	 * @param array $attributes | Attributes of block tag.
	 *
	 * @return false|string
	 */
	public function render_content_block_template( array $attributes, $content, $block ): string {
		$html = '';

		try {

			$handle = 'thim-blocks-view-' . $this->block_name;
			wp_enqueue_script( $handle );

			$autoplay   = array_key_exists( 'autoplay', $attributes ) ? $attributes['autoplay'] : false;
			$navigation = array_key_exists( 'navigation', $attributes ) ? $attributes['navigation'] : true;
			$pagination = array_key_exists( 'pagination', $attributes ) ? $attributes['pagination'] : true;
			$centerSlides = array_key_exists( 'centerSlides', $attributes ) ? $attributes['centerSlides'] : false;

			$speed        = ! empty( $attributes['speed'] ) ? $attributes['speed'] : 6000;
			$spaceBetween = ! empty( $attributes['spaceBetween'] ) && is_array( $attributes['spaceBetween'] )
				? $attributes['spaceBetween']
				: array(
					'desktop' => 30,
					'tablet'  => 30,
					'mobile'  => 20,
				);
			$slidesToShow = ! empty( $attributes['slidesToShow'] ) && is_array( $attributes['slidesToShow'] )
				? $attributes['slidesToShow']
				: array(
					'desktop' => 3,
					'tablet'  => 2,
					'mobile'  => 1,
				);

			$swiper_attr = array(
				'autoplay'     => $autoplay,
				'navigation'   => $navigation,
				'pagination'   => $pagination,
				'speed'        => $speed,
				'centerSlides' => $centerSlides,
				'slidesToShow' => array(
					'desktop' =>  ( $slidesToShow['desktop'] ?? 3 ),
					'tablet'  =>  ( $slidesToShow['tablet'] ?? 2 ),
					'mobile'  =>  ( $slidesToShow['mobile'] ?? 1 ),
				),
				'spaceBetween' => array(
					'desktop' => (int) ( $spaceBetween['desktop'] ?? 30 ),
					'tablet'  => (int) ( $spaceBetween['tablet'] ?? 30 ),
					'mobile'  => (int) ( $spaceBetween['mobile'] ?? 20 ),
				),
			);
			$swiper_attr = htmlspecialchars( wp_json_encode( $swiper_attr ) );

			$wrapper_attributes = get_block_wrapper_attributes(
				array(
					'class' => 'swiper',
				)
			);

			ob_start();
			echo '<div class="swiper-container-wrap">';

			echo '<div ' . wp_kses_post( $wrapper_attributes ) . ' data-swiper="' . esc_attr( $swiper_attr ) . '">';
			echo '<div class="swiper-wrapper">';
			echo $content;
			echo '</div>';
			echo '</div>';

			if ( $navigation ) {
				echo '<div class="swiper-button-prev"></div>';
				echo '<div class="swiper-button-next"></div>';
			}

			if ( $pagination ) {
				echo '<div class="swiper-pagination"></div>';
			}

			echo '</div>';

			$html = ob_get_clean();

		} catch ( Throwable $e ) {
			return $html;
		}

		return $html;
	}

}
