<?php

namespace ThimPress\ThimBlocks\Gutenberg\Blocks\IconItem;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

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

class IconItemBlockType extends AbstractBlockType {


	public $block_name = 'icon-item';

	public function get_supports(): array {
		return array(
			'align'                => true,
			'html'                 => false,
			'spacing'              => array(
				'margin'          => true,
				'padding'         => true,
				'defaultControls' => array(
					'margin'  => true,
					'padding' => true,
				),
			),
			'typography'           => array(
				'fontSize'        => true,
				'fontWeight'      => true,
				'fontStyle'       => true,
				'textTransform'   => true,
				'defaultControls' => array(
					'fontSize' => true,
				),
			),
			'color'                => array(
				'text'            => true,
				'link'            => true,
				'background'      => true,
				'defaultControls' => array(
					'text'       => true,
					'background' => true,
					'link'       => false,
				),
			),
			'__experimentalBorder' => array(
				'color'           => true,
				'radius'          => true,
				'style'           => true,
				'width'           => true,
				'defaultControls' => array(
					'radius' => true,
					'width'  => true,
				),
			),
		);
	}

	public function render_content_block_template( array $attributes, $content, $block ): string {
		$html = '';

		try {
			$iconSVG      = $attributes['iconSVG'] ?? '';
			$iconClass    = esc_attr( $attributes['iconClass'] ?? 'tb-icon-list-check' );
			$itemLink     = esc_url( $attributes['itemLink'] ?? '' );
			$enableLink   = $attributes['enableLink'] ?? false;
			$openInNewTab = $attributes['opensInNewTab'] ?? false;
			$iconColor    = $attributes['iconColor'] ?? '';
			$iconFontSize = $attributes['iconFontSize'] ?? '';
			$svg_path     = '';

			ob_start();

			$wrapper_attrs = get_block_wrapper_attributes( array( 'class' => 'thim-blocks-icon-item' ) );
			echo '<div ' . wp_kses_post( $wrapper_attrs ) . '>';

			if ( $iconSVG || $iconClass ) {
				if ( $enableLink && $itemLink ) {
					echo '<a href="' . esc_url( $itemLink ) . '" class="thim-icon-item__link"' .
						( $openInNewTab ? ' target="_blank" rel="noopener noreferrer"' : '' ) .
						'>';
				}

				$icon_style = '';
				if ( $iconColor ) {
					$icon_style .= 'color:' . esc_attr( $iconColor ) . ';';
				}
				if ( $iconFontSize ) {
					$icon_style .= 'font-size:' . esc_attr( $iconFontSize ) . 'px;';
				}

				echo '<div class="thim-icon-item__icon" style="' . esc_attr( $icon_style ) . '">';

				$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 '</div>';

				if ( $enableLink && $itemLink ) {
					echo '</a>';
				}
			}

			$content = trim( $attributes['content'] ?? '' );
			if ( $content ) {
				echo '<div class="thim-icon-item__content">';
				echo wp_kses_post( $content );
				echo '</div>';
			}

			echo '</div>';

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

		return $html;
	}

}
