<?php
namespace Jet_Engine_Divi_Views\Components\Jet_Engine\Listing;

// Prevent direct access to the file.
if ( ! defined( 'WPINC' ) ) {
	die;
}

/**
 * Listing manager
 */
class Manager {

	protected $view_id = 'divi';

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

		// Listing items Editor-related hooks
		add_filter( 'jet-engine/templates/listing-views', [ $this, 'register_listing_view' ], 99 );
		add_filter( 'jet-engine/templates/create/data', array( $this, 'inject_listing_settings' ) );
		add_filter( 'jet-engine/templates/edit-url/' . $this->get_view_id(), [ $this, 'listing_edit_url' ], 0, 2 );

		// Listing items rendering-related hooks
		add_filter( 'jet-engine/listing/content/' . $this->get_view_id(), array( $this, 'get_listing_content_cb' ), 10, 2 );
		add_filter( 'jet-engine/listings/ajax/settings-by-id/blocks', array( $this, 'get_ajax_blocks_settings' ), 10, 3 );

		// Misc
		add_action( 'init', [ $this, 'ensure_listings_builder_support' ] );

		new Preview( $this->view_id );
	}

	/**
	 * Get listing content callback
	 *
	 * @param string $result     Result content.
	 * @param int    $listing_id Listing ID.
	 *
	 * @return string
	 */
	public function get_listing_content_cb( $result, $listing_id ) {
		$renderer = new Renderer( $listing_id );
		return $renderer->get_content();
	}

	/**
	 * Provide listing settings for JetEngine AJAX handler in blocks mode.
	 * JetEngine expects settings by `element_id`, but Divi wrapper passes them in request payload.
	 *
	 * @param array  $settings   Current settings.
	 * @param string $element_id Element id from request.
	 * @param int    $post_id    Current page id.
	 *
	 * @return array
	 */
	public function get_ajax_blocks_settings( $settings, $element_id, $post_id ) {

		if ( ! empty( $settings ) ) {
			return $settings;
		}

		if ( empty( $_REQUEST['widget_settings'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
			return $settings;
		}

		$widget_settings = $_REQUEST['widget_settings']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended

		if ( is_string( $widget_settings ) ) {
			$widget_settings = wp_unslash( $widget_settings );
			$decoded         = json_decode( $widget_settings, true );
			$widget_settings = is_array( $decoded ) ? $decoded : [];
		}

		if ( ! is_array( $widget_settings ) ) {
			return $settings;
		}

		// Hard requirement for Listing Grid AJAX rendering.
		if ( empty( $widget_settings['lisitng_id'] ) ) {
			return $settings;
		}

		return $widget_settings;
	}

	/**
	 * Get listing edit URL
	 *
	 * @param string $redirect_url Default redirect URL.
	 * @param int    $template_id  Listing item template ID.
	 *
	 * @return string
	 */
	public function listing_edit_url( $redirect_url, $template_id ) {

		if ( function_exists( 'et_fb_get_builder_url' ) ) {
			$redirect_url = et_fb_get_builder_url( get_permalink( $template_id ) );
		} else {
			$redirect_url = jet_engine()->blocks_views->get_redirect_url( $template_id );
		}

		return $redirect_url;
	}

	/**
	 * Inject Divi-related listing settings on listing template creation.
	 *
	 * @param array $template_data Template data.
	 *
	 * @return array
	 */
	public function inject_listing_settings( $template_data ) {

		if ( empty( $_REQUEST['listing_view_type'] )
			|| $this->get_view_id() !== $_REQUEST['listing_view_type']
		) {
			return $template_data;
		}

		$template_data['meta_input']['_et_pb_use_builder'] = 'on';
		$template_data['meta_input']['_et_pb_show_page_creation'] = 'on';
		$template_data['meta_input']['_et_pb_use_divi_5'] = 'on';

		return $template_data;
	}

	/**
	 * Ensure listings builder support
	 *
	 * @return void
	 */
	public function ensure_listings_builder_support() {

		global $et_theme_options;

		if ( ! empty( $et_theme_options ) ) {
			if ( empty( $et_theme_options['et_pb_post_type_integration'] ) ) {
				$et_theme_options['et_pb_post_type_integration'] = [];
			}

			$et_theme_options['et_pb_post_type_integration'][ jet_engine()->post_type->slug() ] = 'on';
		}
	}

	/**
	 * Register listing view
	 *
	 * @param  array $views Views list.
	 * @return array
	 */
	public function register_listing_view( $views ) {
		$views[ $this->get_view_id() ] = 'Divi';
		return $views;
	}

	/**
	 * Get view ID
	 *
	 * @return string
	 */
	public function get_view_id() {
		return $this->view_id;
	}
}
