<?php

namespace ThimPress\ThimBlocks\Gutenberg\Blocks\Counter;

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

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

/**
 * Class CounterBlockType
 *
 * Handle register, render block template
 */
class CounterBlockType extends AbstractBlockType {

	public $block_name = 'counter';

	public function get_supports(): array {
		return array(
			'align'                => array( 'wide', 'full', 'left', 'right', 'center' ),
			'spacing'              => array(
				'padding'                       => true,
				'margin'                        => true,
				'__experimentalDefaultControls' => array(
					'padding' => false,
					'margin'  => false,
				),
			),
			'__experimentalBorder' => array(
				'color'  => true,
				'radius' => true,
				'style'  => true,
				'width'  => true,
			),
			'typography'           => array(
				'textAlign' => true,
			),
			'color'                => array(
				'background' => true,
				'text'       => false,
				'gradients'  => true,
			),
		);
	}

	/**
	 * Render content of counter block.
	 *
	 * @param array $attributes | Attributes of block tag.
	 * @param string $content
	 * @param \WP_Block $block
	 *
	 * @return 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 );

			$number       = isset( $attributes['number'] ) ? intval( $attributes['number'] ) : 100;
			$prefix       = isset( $attributes['prefix'] ) ? esc_html( $attributes['prefix'] ) : '';
			$suffix       = isset( $attributes['suffix'] ) ? esc_html( $attributes['suffix'] ) : '';
			$label        = isset( $attributes['label'] ) ? esc_html( $attributes['label'] ) : esc_html__( 'Projects', 'thim-blocks' );
			$number_color = ! empty( $attributes['numberColor'] ) ? esc_attr( $attributes['numberColor'] ) : '';
			$number_size  = ! empty( $attributes['numberFontSize'] ) ? esc_attr( $attributes['numberFontSize'] ) : '';
			$label_color  = ! empty( $attributes['labelColor'] ) ? esc_attr( $attributes['labelColor'] ) : '';
			$label_size   = ! empty( $attributes['labelFontSize'] ) ? esc_attr( $attributes['labelFontSize'] ) : '';

			$number_style = '';
			if ( $number_color ) {
				$number_style .= 'color:' . $number_color . ';';
			}
			if ( $number_size ) {
				if ( is_numeric( $number_size ) ) {
					$number_style .= 'font-size:' . $number_size . 'px;';
				} else {
					$number_style .= 'font-size:' . $number_size . ';';
				}
			}

			$label_style = '';
			if ( $label_color ) {
				$label_style .= 'color:' . $label_color . ';';
			}
			if ( $label_size ) {
				if ( is_numeric( $label_size ) ) {
					$label_style .= 'font-size:' . $label_size . 'px;';
				} else {
					$label_style .= 'font-size:' . $label_size . ';';
				}
			}

			ob_start();
			?>
			<?php
			printf(
				'<div %s>',
				wp_kses_post( get_block_wrapper_attributes( array( 'class' => 'thim-blocks-counter' ) ) )
			);
			?>
			<div class="counter-number" data-number="<?php echo esc_attr( $number ); ?>" style="<?php echo esc_attr( $number_style ); ?>">
				<?php echo esc_html( $prefix ); ?><span class="counter-value"><?php echo esc_html( $number ); ?></span><?php echo esc_html( $suffix ); ?>
			</div>
			<?php if ( $label ) : ?>
				<div class="counter-label" style="<?php echo esc_attr( $label_style ); ?>">
					<?php echo esc_html( $label ); ?>
				</div>
			<?php endif; ?>
			</div>
			<?php
			$html = ob_get_clean();
		} catch ( Throwable $e ) {
			return $html;
		}

		return $html;
	}

}
