<?php

namespace Simple_Giveaways;

/**
 * Class SG_Blocks
 */
class SG_Blocks {

	/**
	 * Blocks constructor.
	 */
	public function __construct() {
		global $wp_version;
		add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue' ) );

		if ( version_compare( $wp_version, '5.8.0', '<' ) ) {
			add_filter( 'block_categories', array( $this, 'add_block_category' ), 10 );
		} else {
			add_filter( 'block_categories_all', 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;
	}

	/**
	 * Return the Package HTML.
	 *
	 * @param array $args Array of arguments.
	 * @return string
	 */
	public function get_giveaway_countdown( $args ) {
		ob_start();

		if ( $args['id'] ) {
			$post     = get_post( $args['id'] );
			$giveaway = giveasap_get_giveaway( $post );
			$timezone = get_option( 'gmt_offset' );
			?>
                <div class="sg-shortcode sg-block">
                    <div data-timezone="<?php echo esc_attr( $timezone ); ?>"
                         data-end="<?php echo giveasap_get_end_time( $giveaway->get_id() ); ?>"
                         class="giveasap_countdown">
                    </div>
                </div>
			<?php
		} else {
			echo esc_html__( 'Please select a giveaway', 'giveasap' );
		}

		return ob_get_clean();
	}

	/**
	 * Return the Package HTML.
	 *
	 * @param array $args Array of arguments.
	 * @return string
	 */
	public function get_giveaway_winners( $args ) {
		ob_start();

		if ( $args['id'] ) {
			giveasap_show_winners( absint( $args['id'] ), true );
		} else {
			echo esc_html__( 'Please select a giveaway', 'giveasap' );
		}

		return ob_get_clean();
	}

	/**
	 * 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',
					],
				],
			]
		);

		register_block_type(
			'simple-giveaways/countdown',
			[
				'render_callback' => array( $this, 'get_giveaway_countdown' ),
				'attributes'      => [
					'id' => [
						'default' => '0',
						'type'    => 'string',
					],
				],
			]
		);

		register_block_type(
			'simple-giveaways/winners',
			[
				'render_callback' => array( $this, 'get_giveaway_winners' ),
				'attributes'      => [
					'id' => [
						'default' => '0',
						'type'    => 'string',
					],
				],
			]
		);
	}

	/**
	 * 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' )
		);
	}
}
