<?php

/**
 * Class SG_Blocks
 */
class SG_Blocks {

	/**
	 * Blocks constructor.
	 */
	public function __construct() {
		add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue' ) );
		add_filter( 'block_categories', array( $this, 'add_block_category' ), 10 );
		add_action( 'init', array( $this, 'register_blocks' ) );
	}

	/**
	 * Return the Package HTML.
	 *
	 * @param array $args Array of arguments.
	 * @return string
	 */
	public function get_giveaway( $args ) {
		$block = giveasap_shortcode( $args );

		return $block;
	}

	/**
	 * Registering the dynamic blocks.
	 */
	public function register_blocks() {
		if ( ! function_exists( 'register_block_type' ) ) {
			return;
		}

		register_block_type( 'simple-giveaways/giveaway', [
			'render_callback' => array( $this, 'get_giveaway' ),
			'attributes'      => [
				'id' => [
					'default' => '0',
					'type'    => 'string',
				],
				'hide_link'  => [
					'type'    => 'string',
					'default' => '0',
				],
				'title' => [
					'type'    => 'string',
					'default' => '0',
				],
				'prizes' => [
					'type'    => 'string',
					'default' => '1',
				],
				'show_total_entries' => [
					'type'    => 'string',
					'default' => '0',
				],
				'show_entries' => [
					'type'    => 'string',
					'default' => '0',
				],
				'countdown_in_header' => [
					'type'    => 'string',
					'default' => '0',
				],
				'hide_winners_number' => [
					'type'    => 'string',
					'default' => '0',
				],
				'hide_prize_value' => [
					'type'    => 'string',
					'default' => '0',
				],
				'hide_prize_title' => [
					'type'    => 'string',
					'default' => '0',
				],
				'content' => [
					'type'    => 'string',
					'default' => '0',
				],
				'rules' => [
					'type'    => 'string',
					'default' => '0',
				],
			]
		] );
	}

	/**
	 * Adding Simple Giveaways category.
	 *
	 * @param array $categories Array of categories.
	 *
	 * @return array
	 */
	public function add_block_category( $categories ) {
		return array_merge(
			$categories,
			array(
				array(
					'slug' => 'simple-giveaways',
					'title' => __( 'Simple Giveaways', 'simple-sponsorships' ),
				),
			)
		);
	}

	/**
	 * Enqueue Editor Assets for Blocks.
	 */
	public function enqueue() {
		wp_enqueue_script(
			'ss-block-js',
			GASAP_URI . 'assets/dist/js/gutenberg.js',
			array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-components', 'wp-editor', 'wp-compose' )
		);

		// Styles.
		wp_enqueue_style(
			'ss-block-css',
			GASAP_URI . 'assets/dist/css/gutenberg.css',
			array( 'wp-edit-blocks' )
		);
	}
}