<?php
/**
 * Blocks parent class.
 *
 * @package Nfc\Events;
 * @version 1.0.0
 */

namespace Nfc\Events\Blocks;

use Nfc\Events\Singleton;

/**
 * Parent Class Blocks.
 */
class Blocks {
	use Singleton;

	//phpcs:disable Generic.Arrays.DisallowShortArraySyntax

	/**
	 * Base name
	 *
	 * @var string
	 */
	protected $basename = 'nfc-events';

	/**
	 * Front end script
	 *
	 * @var string
	 */
	protected $frontend_script = false;

	/**
	 * Render callback
	 *
	 * @var string
	 */
	protected $render_callback = false;

	/**
	 * With css
	 *
	 * @var string
	 */
	protected $withcss = false;

	/**
	 * With editor css
	 *
	 * @var string
	 */
	protected $witheditorcss = false;

	/**
	 * Block name
	 *
	 * @var string
	 */
	protected $blockname = '';

	/**
	 * Attributes
	 *
	 * @var string
	 */
	protected $attributes = [];

	/**
	 * Asset name
	 *
	 * @var string
	 */
	protected $assetname = '';

	/**
	 * Block type name
	 *
	 * @var string
	 */
	protected $blocktypename = '';

	/**
	 * Constructor.
	 */
	public function __construct() {}

	/**
	 * Register Block type
	 *
	 * @return void
	 */
	public function register_block_type() {
		$this->assetname     = $this->basename . '-' . $this->blockname;
		$this->blocktypename = $this->basename . '/' . $this->blockname;

		$this->register_block_editor_js();

		$args = [
			'editor_script' => $this->assetname,
		];

		if ( $this->withcss ) {
			$this->register_block_css();
			$args['style'] = $this->assetname;
		}

		if ( $this->witheditorcss ) {
			$this->register_block_editor_css();
			$args['editor_style'] = $this->assetname . '-editor';
		}

		if ( ! empty( $this->attributes ) ) {
			$args['attributes'] = $this->attributes;
		}

		if ( $this->frontend_script ) {
			$this->register_frontend_js();
		}

		if ( $this->render_callback ) {
			$args['render_callback'] = [ $this, 'render_callback' ];
		}

		$args['supports'] = [
			'align' => true,
		];

		register_block_type(
			$this->blocktypename,
			$args,
		);
	}

	/**
	 * Function aa
	 *
	 * @return bool
	 */
	public function aa() {
		return true;
	}

	/**
	 * Get the assets file generated by @wordpress/scripts
	 *
	 * @param string $filename File name.
	 *
	 * @return array
	 */
	public function assets_file( $filename = 'index' ) {
		$file = NFC_EVENTS_BLOCKS_BUILD_PATH . $this->blockname . '-' . $filename . '.asset.php';

		if ( ! file_exists( $file ) ) {
			return;
		}

		return include $file;
	}

	/**
	 * Register blocck editor JavaScript
	 *
	 * @return void
	 */
	public function register_block_editor_js() {
		$assetsfile = $this->assets_file( 'index' );
		$index_js   = NFC_EVENTS_BLOCKS_BUILD_URL . $this->blockname . '-index.js';

		if ( ! file_exists( NFC_EVENTS_BLOCKS_BUILD_PATH . $this->blockname . '-index.js' ) ) {
			return;
		}

		wp_register_script(
			$this->assetname,
			$index_js,
			$assetsfile['dependencies'],
			$assetsfile['version'],
			true
		);
	}

	/**
	 * Register Front-end JavaScript
	 *
	 * @return void
	 */
	public function register_frontend_js() {
		$assetsfile  = $this->assets_file( 'frontend' );
		$frontend_js = NFC_EVENTS_BLOCKS_BUILD_URL . $this->blockname . '-frontend.js';

		wp_enqueue_script(
			$this->assetname . '-frontend',
			$frontend_js,
			$assetsfile['dependencies'],
			$assetsfile['version'],
			true
		);
	}

	/**
	 * Register CSS for block editor AND front-end
	 *
	 * @return void
	 */
	public function register_block_css() {
		$css_index_url  = NFC_EVENTS_BLOCKS_BUILD_URL . $this->blockname . '-index.css';
		$css_index_path = NFC_EVENTS_BLOCKS_BUILD_PATH . $this->blockname . '-index.css';

		if ( ! file_exists( $css_index_path ) ) {
			return;
		}

		wp_register_style(
			$this->assetname,
			$css_index_url,
			[],
			filemtime( $css_index_path )
		);
	}

	/**
	 * Register CSS for block editor only
	 *
	 * @return void
	 */
	public function register_block_editor_css() {
		$css_index_url  = NFC_EVENTS_BLOCKS_BUILD_URL . $this->blockname . '-editor.css';
		$css_index_path = NFC_EVENTS_BLOCKS_BUILD_PATH . $this->blockname . '-editor.css';

		wp_register_style(
			$this->assetname . '-editor',
			$css_index_url,
			[],
			filemtime( $css_index_path )
		);
	}


	/**
	 * Register CSS for front-end only
	 *
	 * @return void
	 */
	public function register_frontend_css() {}

	/**
	 * Enqueue script for frontend, used in render_callback as a hacky way to only enqueue when block exists
	 *
	 * @return void
	 */
	public function enqueue_frontend_script() {
		wp_enqueue_script( $this->assetname . '-frontend' );
	}

	/**
	 * Render callback to enqueue frontend assets
	 *
	 * @param array $block_attributes Block attributes.
	 * @param mixed $content Content.
	 * @return mixed
	 */
	public function render_callback( $block_attributes, $content ) {
		if ( $this->frontend_script ) {
			$this->enqueue_frontend_script();
		}

		return $content;
	}
}
