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

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

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

class CountdownBlockType extends AbstractBlockType {
	public $block_name = 'countdown';

	public function get_supports(): array {
		return array(
			'align'      => array( 'wide', 'full', 'center' ),
			'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,
				),
			),
		);
	}

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

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

			$target         = esc_attr( $attributes['targetDate'] ?? '' );
			$label_position = esc_attr( $attributes['labelPosition'] ?? 'right' );
			$custom_labels  = $attributes['customLabels'] ?? array();
			$label_font     = $attributes['labelFontSize'] ?? '';
			$label_color    = $attributes['labelColor'] ?? '';

			// Default labels fallback
			$labels = array(
				'days'    => $custom_labels['days'] ?? __( 'Days', 'thim-blocks' ),
				'hours'   => $custom_labels['hours'] ?? __( 'Hours', 'thim-blocks' ),
				'minutes' => $custom_labels['minutes'] ?? __( 'Minutes', 'thim-blocks' ),
				'seconds' => $custom_labels['seconds'] ?? __( 'Seconds', 'thim-blocks' ),
			);

			// Inline style cho label
			$label_style = '';
			if ( $label_font ) {
				$label_style .= 'font-size:' . esc_attr( is_numeric( $label_font ) ? $label_font . 'px' : $label_font ) . ';';
			}
			if ( $label_color ) {
				$label_style .= 'color:' . esc_attr( $label_color ) . ';';
			}

			ob_start();
			?>
			<div 
			<?php
			echo wp_kses_post(
				get_block_wrapper_attributes(
					array(
						'class' => 'thim-blocks-countdown label-' . $label_position,
					)
				)
			);
			?>
			>
				<div class="countdown-timer" data-target="<?php echo esc_attr( $target ); ?>">
					<?php foreach ( $labels as $key => $text ) : ?>
						<div class="countdown-part countdown-<?php echo esc_attr( $key ); ?>">
							<?php if ( in_array( $label_position, array( 'top', 'left' ), true ) ) : ?>
								<span class="countdown-label" style="<?php echo esc_attr( $label_style ); ?>"><?php echo esc_html( $text ); ?></span>
								<span class="countdown-value">00</span>
							<?php else : ?>
								<span class="countdown-value">00</span>
								<span class="countdown-label" style="<?php echo esc_attr( $label_style ); ?>"><?php echo esc_html( $text ); ?></span>
							<?php endif; ?>
						</div>
					<?php endforeach; ?>
				</div>
			</div>
			<?php
			$html = ob_get_clean();

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

		return $html;
	}

}
