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

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

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

/**
 * Class VideoPopupBlockType
 *
 * Handle register, render block template
 */
class VideoPopupBlockType extends AbstractBlockType {
	public $block_name = 'video-popup';

	public function get_supports(): array {
		return array(
			'align'      => array( 'wide', 'full', 'left', 'right', 'center' ),
			'typography' => array(
				'fontSize'                      => true,
				'lineHeight'                    => false,
				'fontWeight'                    => true,
				'__experimentalFontFamily'      => false,
				'__experimentalTextDecoration'  => false,
				'__experimentalFontStyle'       => false,
				'__experimentalFontWeight'      => true,
				'__experimentalLetterSpacing'   => false,
				'__experimentalTextTransform'   => true,
				'__experimentalDefaultControls' => array( 'fontSize' => true ),
			),
			'color'      => array(
				'background'                    => false,
				'text'                          => true,
				'link'                          => false,
				'gradients'                     => false,
				'__experimentalDefaultControls' => array(
					'text' => true,
				),
			),
			'spacing'    => array(
				'padding'                       => true,
				'margin'                        => true,
				'__experimentalDefaultControls' => array(
					'margin'  => false,
					'padding' => false,
				),
			),
		);
	}

	/**
	 * 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 );

			$video_url  = esc_url( $attributes['videoURL'] ?? '' );
			$video_type = esc_attr( $attributes['videoType'] ?? 'youtube' );
			$iconSVG    = $attributes['iconSVG'] ?? '';
			$iconClass  = esc_attr( $attributes['iconClass'] ?? 'tb-icon-play-circle-o' );
			$iconColor  = $attributes['iconColor'] ?? '';

			$style = array();

			$iconFontSize = $attributes['iconFontSize'] ?? array();
			foreach ( array( 'desktop', 'tablet', 'mobile' ) as $device ) {
				if ( ! empty( $iconFontSize[ $device ] ) ) {
					$style[ "--icon-font-size-$device" ] = $iconFontSize[ $device ];
				}
			}

			$inline_style = '';
			foreach ( $style as $prop => $value ) {
				$inline_style .= $prop . ':' . esc_attr( $value ) . ';';
			}
			if ( $iconColor ) {
				$inline_style .= '--icon-color:' . esc_attr( $iconColor ) . ';';
			}

			if ( empty( $attributes['videoURL'] ) ) {
				return '<div class="video-popup-block error">' . esc_html__( 'No video URL provided.', 'thim-blocks' ) . '</div>';
			}

			ob_start();
			echo '<div class="thim-blocks-video-popup">';

			echo '<button ' . wp_kses_post(
				get_block_wrapper_attributes(
					array(
						'class' => 'video-popup-button',
						'style' => $inline_style ?: null,
					)
				)
			) . ' ';
			echo ' data-video-url="' . esc_attr( $video_url ) . '"';
			echo ' data-video-type="' . esc_attr( $video_type ) . '"';
			echo '>';
			$svg_output = SVGHelper::render_svg( $iconSVG );

			if ( $svg_output ) {
				echo wp_kses( $svg_output, SVGHelper::allowed_tags() );
			} else {
				echo '<i class="' . esc_attr( $iconClass ) . '"></i>';
			}
			echo '</button>';
			echo '</div>';
			$html = ob_get_clean();
		} catch ( Throwable $e ) {
			return $html;
		}

		return $html;
	}

}
