<?php
/**
 * The plugin gutenberg block Initializer.
 *
 * @link       https://shapedplugin.com/
 * @since      2.0.0
 *
 * @package    Smart_Post_Show_Pro
 * @subpackage Smart_Post_Show_Pro/Admin
 * @author     ShapedPlugin <support@shapedplugin.com>
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! class_exists( 'Smart_Post_Show_Pro_Gutenberg_Block_Init' ) ) {
	/**
	 * Smart_Post_Show_Pro_Gutenberg_Block_Init class.
	 */
	class Smart_Post_Show_Pro_Gutenberg_Block_Init {
		/**
		 * Script and style suffix
		 *
		 * @since 2.0.0
		 * @access protected
		 * @var string
		 */
		protected $suffix;
		/**
		 * Custom Gutenberg Block Initializer.
		 */
		public function __construct() {
			$this->suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || defined( 'WP_DEBUG' ) && WP_DEBUG ? '' : '.min';
			add_action( 'init', array( $this, 'spsp_gutenberg_shortcode_block' ) );
		}

		/**
		 * Shortcode list.
		 *
		 * @return array
		 */
		public function spsp_post_list() {
			$shortcodes = get_posts(
				array(
					'post_type'      => 'sp_post_carousel',
					'post_status'    => 'publish',
					'posts_per_page' => 9999,
				)
			);
			if ( count( $shortcodes ) < 1 ) {
				return array();
			}
			return array_map(
				function ( $shortcode ) {
						return (object) array(
							'id'    => absint( $shortcode->ID ),
							'title' => esc_html( $shortcode->post_title ),
						);
				},
				$shortcodes
			);
		}

		/**
		 * Register Gutenberg shortcode block.
		 */
		public function spsp_gutenberg_shortcode_block() {
			/**
			 * Register block editor js file enqueue for backend.
			 */
			wp_register_script( 'pcp_gb_script', SP_PCP_URL . 'public/assets/js/scripts.min.js', array( 'jquery' ), SP_PCP_VERSION, true );
			wp_localize_script(
				'pcp_gb_script',
				'sppcp',
				array(
					'ajaxurl'       => admin_url( 'admin-ajax.php' ),
					'nonce'         => wp_create_nonce( 'sppcp_nonce' ),
					'url'           => esc_url( SP_PCP_URL ),
					'loadScript'    => esc_url( SP_PCP_URL . 'public/assets/js/scripts.min.js' ),
					'link'          => esc_url( admin_url( 'post-new.php?post_type=sp_post_carousel' ) ),
					'shortCodeList' => is_admin() ? $this->spsp_post_list() : '',
					'pcp_swiper_js' => true,

				)
			);
			/**
			 * Register Gutenberg block on server-side.
			 */
			register_block_type(
				'smart-post-show-pro/shortcode',
				array(
					'attributes'      => array(
						'shortcode'          => array(
							'type'    => 'string',
							'default' => '',
						),
						'showInputShortcode' => array(
							'type'    => 'boolean',
							'default' => true,
						),
						'preview'            => array(
							'type'    => 'boolean',
							'default' => false,
						),
						'is_admin'           => array(
							'type'    => 'boolean',
							'default' => is_admin(),
						),
					),
					'example'         => array(
						'attributes' => array(
							'preview' => true,
						),
					),
					// Enqueue blocks.editor.build.js in the editor only.
					'editor_script'   => array(
						'pcp_swiper',
						'pcp_isotope',
						'pcp_bxslider',
						'pcp_popup',
						'pcp_lazy',
						'jquery-ui-slider',
						'jquery-ui-accordion',
						'pcp_gb_script',
					),
					// Enqueue blocks.editor.build.css in the editor only.
					'editor_style'    => array(
						'pcp-font-awesome',
						// 'pcp_swiper',
						'pcp-bxslider',
						'pcp-likes',
						'pcp-popup',
						'pcp_fonttello_icon',
						'pcp-style',
						'smart-post-show-pro-shortcode-block-style',
					),
					'render_callback' => array( $this, 'smart_post_show_pro_render_shortcode' ),
				)
			);
		}

		/**
		 * Render callback.
		 *
		 * @param string $attributes Shortcode.
		 * @return string
		 */
		public function smart_post_show_pro_render_shortcode( $attributes ) {
			$align       = $attributes['align'] ?? '';
			$align_class = ! empty( $align ) ? ( 'align' . $align ) : '';
			$class_name  = '';
			if ( ! empty( $attributes['className'] ) ) {
				$class_name = $attributes['className'];
			}
			if ( empty( $attributes['shortcode'] ) || ! get_post_status( $attributes['shortcode'] ) ) {
				return ' ';
			}
			if ( ! $attributes['is_admin'] ) {
				return '<div class="' . esc_attr( $class_name ) . ' ' . esc_attr( $align_class ) . '">' . do_shortcode( '[smart_post_show id="' . sanitize_text_field( $attributes['shortcode'] ) . '"]' ) . '</div>';
			}
			$smart_show_id = (int) $attributes['shortcode'];
			$layouts       = get_post_meta( $smart_show_id, 'sp_pcp_layouts', true );
			// All the visible options for the Shortcode like – Global, Filter, Display, Popup, Typography etc.
			$view_options = get_post_meta( $smart_show_id, 'sp_pcp_view_options', true );
			// Load dynamic style.
			$dynamic_style = Smart_Post_Show_Pro_Public::load_dynamic_style( $smart_show_id, $view_options, $layouts );
			$enqueue_fonts = Smart_Post_Show_Pro_Public::load_google_fonts( $dynamic_style['typography'] );
			$style         = '';
			// Enqueue Google fonts.
			if ( ! empty( $enqueue_fonts ) ) {
				$style =  '<link rel="stylesheet" href="' . esc_url( add_query_arg( 'family', rawurlencode( implode( '|', $enqueue_fonts ) ), '//fonts.googleapis.com/css' ) ) . '" media="all">'; //phpcs:ignore
			}
			$edit_page_link = get_edit_post_link( sanitize_text_field( $attributes['shortcode'] ) );
			return '<div id="' . uniqid() . '" class="' . esc_attr( $class_name ) . '"><a href="' . esc_url( $edit_page_link ) . '" target="_blank" class="sp-pcp-pagination-block-edit-button">' . esc_html__( 'Edit Show', 'smart-post-show-pro' ) . '</a>' . do_shortcode( '[smart_post_show id="' . sanitize_text_field( $attributes['shortcode'] ) . '" is_admin="' . esc_attr( $attributes['is_admin'] ) . '"]' ) . '</div> ' . $style;
		}
	}
}
