<?php
namespace SoftMultimediaPlayer;

defined( 'ABSPATH' ) || exit();

class Block {
	/**
	 * @var null
	 */
	protected static $instance = null;

	public function __construct() {
		add_action( 'init', [ $this, 'register_block' ] );
	}

	public function register_block() {
		register_block_type( SOFT_MULTIMEDIA_PLAYER_PATH . '/block/build/multimedia-player', [
			'render_callback' => [ $this, 'render_block' ],
		] );
	}

	public function render_block( $attributes ) {

		$id        = ! empty( $attributes['id'] ) ? $attributes['id'] : '';
		$alignment = ! empty( $attributes['align'] ) ? $attributes['align'] : 'center';

		return sprintf( '<div class="align%s">%s</div>', esc_attr( $alignment ), do_shortcode( "[multimedia_player id=$id]" ) );
	}

	/**
	 * @return Block|null
	 */
	public static function instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

}

Block::instance();