<?php
/**
 * Abstract block handler class.
 *
 * @since 2.21.0
 * @package Masteriyo\Abstracts
 */

namespace Masteriyo\Abstracts;

defined( 'ABSPATH' ) || exit;

use Masteriyo\Enums\PostStatus;
use Masteriyo\PostType\PostType;

/**
 * Abstract block handler class.
 *
 * @since 2.21.0
 */
abstract class BlockHandler {

	/**
	 * Block namespace.
	 *
	 * @since 2.21.0
	 * @var string
	 */
	protected $namespace = 'masteriyo';

	/**
	 * Block name.
	 *
	 * @since 2.21.0
	 * @var string
	 */
	protected $block_name = '';

	/**
	 * Attributes.
	 *
	 * @since 2.21.0
	 * @var array
	 */
	protected $attributes = array();

	/**
	 * Block content.
	 *
	 * @since 2.21.0
	 * @var string
	 */
	protected $content = '';

	/**
	 * Block instance.
	 *
	 * @since 2.21.0
	 * @var \WP_Block
	 */
	protected $block;

	/**
	 * Constructor.
	 *
	 * @since 2.21.0
	 *
	 * @param string $block_name Block name.
	 */
	public function __construct( $block_name = '' ) {
		$this->block_name = empty( $block_name ) ? $this->block_name : $block_name;
		$this->register();
	}

	/**
	 * Register the block.
	 *
	 * @since 2.21.0
	 *
	 * @return void
	 */
	protected function register() {
		if ( empty( $this->block_name ) ) {
			_doing_it_wrong( __CLASS__, esc_html__( 'Block name is not set.', 'learning-management-system' ), '3.1.5' );
			return;
		}

		$metadata = $this->get_metadata_base_dir() . "/$this->block_name/block.json";

		if ( ! file_exists( $metadata ) ) {
			_doing_it_wrong(
				__CLASS__,
				/* Translators: 1: Block name */
				esc_html( sprintf( __( 'Metadata file for %s block does not exist.', 'learning-management-system' ), $this->block_name ) ),
				'3.1.5'
			);
			return;
		}
		register_block_type_from_metadata(
			$metadata,
			array(
				'render_callback' => array( $this, 'render' ),
			)
		);
	}

	/**
	 * Get base metadata path.
	 *
	 * @since 2.21.0
	 *
	 * @return string
	 */
	protected function get_metadata_base_dir() {
		return dirname( MASTERIYO_PLUGIN_FILE ) . '/assets/js/build';
	}

	/**
	 * Get block type.
	 *
	 * @since 2.21.0
	 *
	 * @return string
	 */
	protected function get_block_type() {
		return "$this->namespace/$this->block_name";
	}

	/**
	 * Render callback.
	 *
	 * @since 2.21.0
	 *
	 * @param array     $attributes Block attributes.
	 * @param string    $content    Block content.
	 * @param \WP_Block $block      Block object.
	 *
	 * @return string
	 */
	public function render( $attributes, $content, $block ) {
		$this->attributes = $attributes;
		$this->block      = $block;
		$this->content    = $content;
		$content          = apply_filters(
			"masteriyo_{$this->block_name}_content",
			$this->build_html( $this->content ),
			$this
		);
		return $content;
	}

	/**
	 * Build html.
	 *
	 * @since 2.21.0
	 *
	 * @param string $content HTML content.
	 * @return string
	 */
	protected function build_html( $content ) {
		return $content;
	}

	/**
	 * Get a course to use for preview in block editor.
	 *
	 * @since 2.21.0 [Free]
	 *
	 * @param int $course_id Optional course ID.
	 * @return \Masteriyo\Models\Course|null
	 */
	public function get_block_preview_course( $course_id ) {
		global $course;
		if ( empty( $course ) ) {
			if ( 0 !== $course_id ) {
				$course = masteriyo_get_course( $course_id );
			}

			if ( ! $course_id ) {
				$args   = array(
					'posts_per_page' => 1,
					'post_type'      => PostType::COURSE,
					'author'         => get_current_user_id(),
					'post_status'    => array( PostStatus::PUBLISH, PostStatus::DRAFT ),
				);
				$posts  = get_posts( $args );
				$course = empty( $posts ) ? null : masteriyo_get_course( $posts[0] );
			}
		}
		return $course;
	}
}
