<?php

/**
 * Blocks Initializer.
 *
 * @link    https://aiopodcasting.com
 * @since   1.0.0
 *
 * @package AiO_Podcast_Studio
 */

// Exit if accessed directly
if ( ! defined( 'WPINC' ) ) {
	die;
}

/**
 * AIOPOD_Blocks class.
 *
 * @since 1.0.0
 */
class AIOPOD_Blocks {

	/**
     * Block fields.
     *
     * @since  1.0.0
	 * @access private
     * @var    array
     */
	private $fields;

	/**
	 * Register our custom Gutenberg block category.
	 *
	 * @since  1.0.0
	 * @param  array $categories Default Gutenberg block categories.
	 * @return array             Modified Gutenberg block categories.
	 */
	public function block_categories( $categories ) {		
		return array_merge(
			$categories,
			array(
				array(
					'slug'  => 'aio-podcast-studio',
					'title' => __( 'AiO Podcast Studio', 'aio-podcast-studio' ),
				)
			)
		);		
	}

	/**
	 * Enqueue Gutenberg block assets for backend editor.
	 *
	 * @since 1.0.0
	 */
	public function enqueue_block_editor_assets() {	
		$podcasts_settings     = aiopod_get_option( 'aiopod_podcasts_settings' );
		$episodes_settings     = aiopod_get_option( 'aiopod_episodes_settings' );
		$categories_settings   = aiopod_get_option( 'aiopod_categories_settings' );
		$tags_settings         = aiopod_get_option( 'aiopod_tags_settings' );
		$participants_settings = aiopod_get_option( 'aiopod_participants_settings' );

		$fields = $this->get_fields();

		// Podcasts
		$podcasts = $fields['podcasts']['sections'];

		foreach ( $podcasts as $key => $section ) {
			foreach ( $section['fields'] as $index => $field ) {
				if ( isset( $field['description'] ) ) {
					$podcasts[ $key ]['fields'][ $index ]['description'] = strip_tags( $field['description'] );
				}
			}
		}

		// Episodes
		$episodes = $fields['episodes']['sections'];

		foreach ( $episodes as $key => $section ) {
			foreach ( $section['fields'] as $index => $field ) {
				if ( isset( $field['description'] ) ) {
					$episodes[ $key ]['fields'][ $index ]['description'] = strip_tags( $field['description'] );
				}
			}
		}

		// Categories
		$categories = $fields['categories']['sections'];

		foreach ( $categories as $key => $section ) {
			foreach ( $section['fields'] as $index => $field ) {
				if ( isset( $field['description'] ) ) {
					$categories[ $key ]['fields'][ $index ]['description'] = strip_tags( $field['description'] );
				}
			}
		}

		// Tags
		$tags = $fields['tags']['sections'];

		foreach ( $tags as $key => $section ) {
			foreach ( $section['fields'] as $index => $field ) {
				if ( isset( $field['description'] ) ) {
					$tags[ $key ]['fields'][ $index ]['description'] = strip_tags( $field['description'] );
				}
			}
		}

		// Participants
		$participants = $fields['participants']['sections'];

		foreach ( $participants as $key => $section ) {
			foreach ( $section['fields'] as $index => $field ) {
				if ( isset( $field['description'] ) ) {
					$participants[ $key ]['fields'][ $index ]['description'] = strip_tags( $field['description'] );
				}
			}
		}

		$editor_properties = array(
			'podcasts'     => $podcasts,
			'episodes'     => $episodes,
			'categories'   => $categories,
			'tags'         => $tags,
			'participants' => $participants,
			'i18n'         => array(
				'podcasts_title'           => __( 'AIOPOD - Podcast List', 'aio-podcast-studio' ),
				'podcasts_description'     => __( 'Display a customizable list of your podcasts.', 'aio-podcast-studio' ),
				'episodes_title'           => __( 'Episode Player & List', 'aio-podcast-studio' ),
				'episodes_description'     => __( 'Display episodes from an existing podcast or a custom RSS feed URL.', 'aio-podcast-studio' ),
				'categories_title'         => __( 'AIOPOD - Category List', 'aio-podcast-studio' ),
				'categories_description'   => __( 'Display a list of your podcast categories.', 'aio-podcast-studio' ),
				'tags_title'               => __( 'AIOPOD - Tag List', 'aio-podcast-studio' ),
				'tags_description'         => __( 'Display a list of your episode tags.', 'aio-podcast-studio' ),
				'participants_title'       => __( 'AIOPOD - Participant List', 'aio-podcast-studio' ),
				'participants_description' => __( 'Display a list of your participants.', 'aio-podcast-studio' ),
				'select_podcast'           => __( 'Select Podcast', 'aio-podcast-studio' ),
				'select_color'             => __( 'Selected Color', 'aio-podcast-studio' )
			)
		);

		wp_localize_script( 'wp-block-editor', 'aiopod_blocks', $editor_properties );	

		do_action( 'aiopod_enqueue_block_editor_assets' );
	}		

	/**
	 * Register our custom blocks.
	 * 
	 * @since 1.0.0
	 */
	public function register_block_types() {
		if ( ! function_exists( 'register_block_type' ) ) {
			return false;
		}

		$this->register_podcasts_block();
		$this->register_episodes_block();
		$this->register_categories_block();
		$this->register_tags_block();	
		$this->register_participants_block();			
	}

	/**
	 * Register the podcasts block.
	 *
	 * @since 1.0.0
	 */
	private function register_podcasts_block() {
		$attributes = array();
		$fields     = $this->get_fields();

		foreach ( $fields['podcasts']['sections'] as $key => $section ) {
			foreach ( $section['fields'] as $field ) {
				if ( 'categories' == $field['type'] || 'participants' == $field['type'] ) {
					$attributes[ $field['name'] ] = array(
						'type'  => 'array',
						'items' => array(
							'type' => 'integer'
						)
					);
				} else {
					$type = 'string';

					if ( 'number' == $field['type'] ) {
						$type = 'number';
					} elseif ( 'checkbox' == $field['type'] ) {
						$type = 'boolean';
					}

					$attributes[ $field['name'] ] = array(
						'type' => $type
					);
				}
			}
		}

		register_block_type( __DIR__ . '/build/podcasts', array(
			'attributes'      => $attributes,
			'render_callback' => array( $this, 'render_podcasts_block' ),
		) );
	}

	/**
	 * Register the episodes block.
	 *
	 * @since 1.0.0
	 */
	private function register_episodes_block() {
		$attributes = array();
		$fields     = $this->get_fields();

		foreach ( $fields['episodes']['sections'] as $key => $section ) {
			foreach ( $section['fields'] as $field ) {
				if ( 'tags' == $field['type'] || 'participants' == $field['type'] ) {
					$attributes[ $field['name'] ] = array(
						'type'  => 'array',
						'items' => array(
							'type' => 'integer'
						)
					);
				} else {
					$type = 'string';

					if ( 'number' == $field['type'] ) {
						$type = 'number';
					} elseif ( 'checkbox' == $field['type'] ) {
						$type = 'boolean';
					}

					$attributes[ $field['name'] ] = array(
						'type' => $type
					);
				}
			}
		}

		register_block_type( __DIR__ . '/build/episodes', array(
			'attributes'      => $attributes,
			'render_callback' => array( $this, 'render_episodes_block' ),
		) );
	}

	/**
	 * Register the categories block.
	 *
	 * @since 1.0.0
	 */
	private function register_categories_block() {
		$attributes = array();
		$fields     = $this->get_fields();

		foreach ( $fields['categories']['sections'] as $key => $section ) {
			foreach ( $section['fields'] as $field ) {
				$type = 'string';

				if ( 'number' == $field['type'] ) {
					$type = 'number';
				} elseif ( 'checkbox' == $field['type'] ) {
					$type = 'boolean';
				}

				$attributes[ $field['name'] ] = array(
					'type' => $type
				);
			}
		}

		register_block_type( __DIR__ . '/build/categories', array(
			'attributes'      => $attributes,
			'render_callback' => array( $this, 'render_categories_block' ),
		) );
	}

	/**
	 * Register the tags block.
	 *
	 * @since 1.0.0
	 */
	private function register_tags_block() {
		$attributes = array();
		$fields     = $this->get_fields();

		foreach ( $fields['tags']['sections'] as $key => $section ) {
			foreach ( $section['fields'] as $field ) {
				$type = 'string';

				if ( 'number' == $field['type'] ) {
					$type = 'number';
				} elseif ( 'checkbox' == $field['type'] ) {
					$type = 'boolean';
				}

				$attributes[ $field['name'] ] = array(
					'type' => $type
				);
			}
		}

		register_block_type( __DIR__ . '/build/tags', array(
			'attributes'      => $attributes,
			'render_callback' => array( $this, 'render_tags_block' ),
		) );
	}

	/**
	 * Register the participants block.
	 *
	 * @since 1.0.0
	 */
	private function register_participants_block() {
		$attributes = array();
		$fields     = $this->get_fields();

		foreach ( $fields['participants']['sections'] as $key => $section ) {
			foreach ( $section['fields'] as $field ) {
				$type = 'string';

				if ( 'number' == $field['type'] ) {
					$type = 'number';
				} elseif ( 'checkbox' == $field['type'] ) {
					$type = 'boolean';
				}

				$attributes[ $field['name'] ] = array(
					'type' => $type
				);
			}
		}

		register_block_type( __DIR__ . '/build/participants', array(
			'attributes'      => $attributes,
			'render_callback' => array( $this, 'render_participants_block' ),
		) );
	}

	/**
	 * Render the podcasts block frontend.
	 *
	 * @since  1.0.0
	 * @param  array  $atts An associative array of attributes.
	 * @return string       HTML output.
	 */
	public function render_podcasts_block( $atts ) {	
		$output  = '<div ' . get_block_wrapper_attributes() . '>';
		$output .= do_shortcode( '[aiopod_podcasts ' . $this->build_shortcode_attributes( $atts ) . ']' );
		$output .= '</div>';

		return $output;
	}

	/**
	 * Render the episodes block frontend.
	 *
	 * @since  1.0.0
	 * @param  array  $atts An associative array of attributes.
	 * @return string       HTML output.
	 */
	public function render_episodes_block( $atts ) {	
		$output  = '<div ' . get_block_wrapper_attributes() . '>';
		$output .= do_shortcode( '[aiopod_episodes ' . $this->build_shortcode_attributes( $atts ) . ']' );
		$output .= '</div>';

		return $output;
	}

	/**
	 * Render the categories block frontend.
	 *
	 * @since  1.0.0
	 * @param  array  $atts An associative array of attributes.
	 * @return string       HTML output.
	 */
	public function render_categories_block( $atts ) {	
		$output  = '<div ' . get_block_wrapper_attributes() . '>';
		$output .= do_shortcode( '[aiopod_categories ' . $this->build_shortcode_attributes( $atts ) . ']' );
		$output .= '</div>';

		return $output;
	}

	/**
	 * Render the tags block frontend.
	 *
	 * @since  1.0.0
	 * @param  array  $atts An associative array of attributes.
	 * @return string       HTML output.
	 */
	public function render_tags_block( $atts ) {	
		$output  = '<div ' . get_block_wrapper_attributes() . '>';
		$output .= do_shortcode( '[aiopod_tags ' . $this->build_shortcode_attributes( $atts ) . ']' );
		$output .= '</div>';

		return $output;
	}

	/**
	 * Render the participants block frontend.
	 *
	 * @since  1.0.0
	 * @param  array  $atts An associative array of attributes.
	 * @return string       HTML output.
	 */
	public function render_participants_block( $atts ) {	
		$output  = '<div ' . get_block_wrapper_attributes() . '>';
		$output .= do_shortcode( '[aiopod_participants ' . $this->build_shortcode_attributes( $atts ) . ']' );
		$output .= '</div>';

		return $output;
	}

	/**
	 * Build shortcode attributes string.
	 * 
	 * @since  1.0.0
	 * @access private
	 * @param  array   $atts Array of attributes.
	 * @return string        Shortcode attributes string.
	 */
	private function build_shortcode_attributes( $atts ) {
		$attributes = array();
		
		foreach ( $atts as $key => $value ) {
			if ( is_null( $value ) || is_object( $value ) ) {
				continue;
			}

			if ( is_bool( $value ) ) {
				$value = ( true === $value ) ? 1 : 0;
			}

			if ( is_array( $value ) ) {
				$value = implode( ',', $value );
			}

			$attributes[] = sprintf( '%s="%s"', $key, $value );
		}
		
		return implode( ' ', $attributes );
	}

	/**
	 * Get block fields.
	 * 
	 * @since  1.0.0
	 * @access private
	 * @return array
	 */
	private function get_fields() {
		if ( empty( $this->fields ) ) {
			$this->fields = aiopod_get_shortcode_fields();
		}

		return $this->fields;
	}

}
